简体   繁体   English

将滚动条添加到包含 QT C++ 中的布局的小部件

[英]Adding scroll bar to widget containing a layout in QT C++

I am new to QT and I am creating a widget that has a gridlayout.我是 QT 的新手,我正在创建一个具有网格布局的小部件。 The gridlayout contains a matrix of QLineEdit widgets. gridlayout 包含 QLineEdit 小部件的矩阵。 The window resizes to fit the layout but when layout is large it goes off screen. window 调整大小以适应布局,但当布局较大时,它会离开屏幕。 When I maximize the screen, the QLineEdit widgets are resized to fit the screen and for large layouts they become extremely small.当我最大化屏幕时,QLineEdit 小部件会调整大小以适应屏幕,对于大型布局,它们会变得非常小。

I want to be able to resize the window without resizing the QLineEdit widgets and add scroll bars to navigate.我希望能够调整 window 的大小而不调整 QLineEdit 小部件的大小并添加滚动条进行导航。

I tried the following with no luck:我尝试了以下没有运气:

Window->resize(QSize(500,500));  
QScrollArea *scrollArea = new QScrollArea;  
scrollArea->setWidget(Window);  

where window is the widget containing the layout.其中 window 是包含布局的小部件。 Also, the window closes when after executing "scrollArea->setWidget(Window);"此外,执行“scrollArea->setWidget(Window);”后,window 将关闭; and I dont why.我不知道为什么。

If someone can help me out I would really appreciate it.如果有人可以帮助我,我将不胜感激。
Thank You!谢谢你!

For disabling the vertical resize on the widgets, why don't you just use the setFixedHeight() method on the widgets?要禁用小部件的垂直调整大小,为什么不直接在小部件上使用 setFixedHeight() 方法?

For the menu bar, why don't you take it out of the widget that is scrollable.对于菜单栏,你为什么不把它从可滚动的小部件中拿出来。 You can have a layout for the window that contains the menu bar and then the widget that contains everything else (scrollable part).您可以为 window 设置一个布局,其中包含菜单栏,然后是包含其他所有内容(可滚动部分)的小部件。 Is that what you are looking for?那是你要找的吗?

I fixed my problem by creating a QMainWindow with the menu bar.我通过使用菜单栏创建 QMainWindow 解决了我的问题。 Then created a widget which includes the layout, set the Scroll Area to the widget.然后创建一个包含布局的小部件,将滚动区域设置为小部件。 Finally set the central widget of the main widow to the scroll area.最后将主寡妇的中央小部件设置为滚动区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM