Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

c++ - add scrollbar in qt window?

i have been learning qt from video tutorials here. i have completed 35 videos. So i wanted to create a sample application just to understand qt. i am using the latest version of qt creator.

i added some push-buttons & deleted the menu bar & tool bar in main window.

enter image description here

after running i found that if i resize the window scroll-bars don't appear.

enter image description here

i don't want the push-buttons to resize or move. i am fine with them becoming invisible but i want the scroll-bars so that the user can scroll to the invisible parts. i added a scroll area widget but it doesn't work. i did some coding by searching on internet but that didn't work either. when i increase the size of scroll area upto the window size; after running the window appears blank. maybe the push-buttons go 'below' the scroll-area. how do i change which object should be on 'top'?

also since i am just a beginner, i don't know whats the best approach in designing using qt. should i just put the required widgets using designer & then give their characteristics by coding? or should i do complete designing using designer? or should i abandon designer & do the complete coding? THANK YOU!

edit @Synxis, that didnt help much. I even used the secentralwidget(ui->scrollareacontentswidget); i tried deleting the centralwidget QWidget, but i cant. Any solution?

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A QWidget does not have scrollbars, you need to add a zone which will have scrollbars.

Just add a QScrollArea to your window, and put all the widgets in it (buttons, etc...). Don't forget to layout your scroll area to the whole main window.

More precisely, the hierarchy is something like that:

main window central widget (with a layout)
    scroll area
        scroll area widget (with a layout, layout scroll area if you're in the designer)
            your container widget (should be layouted also)
                your buttons, layouts, etc...

((with a layout) = layout the widget with a layout, but the type of the layout doesn't matter, since only one widget needs to be layouted).

Your container widget should be layouted as you want, spacers can be useful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...