简体   繁体   English

Qt:将滚动条自动添加到QGroupbox

[英]Qt: Adding a scrollbar automatically to the QGroupbox

I want to add a scrollbar automatically to the a groupbox of labels when resizing the dialog window that has this groupbox (make it smaller) so to keep the same view of the content of the groupbox and view it by scrolling when that dialog is small. 我想在调整具有该组框的对话框窗口的大小时(将其变小)自动将滚动条添加到标签的组框,以保持该组框内容的相同视图,并在该对话框较小时通过滚动来查看它。

QGroupBox* GroupBox = new QGroupBox;
QVBoxLayout *Layout = new QVBoxLayout;   
Layout->addWidget(Label1);
Layout->addWidget(Label2);
Layout->addWidget(Label3);
Layout->addWidget(Label4);
GroupBox ->setLayout(Layout);

I have tried the following but it does not work. 我已经尝试了以下方法,但是它不起作用。

QScrollArea* scrollArea = new QScrollArea(this);
scrollArea->setWidget(GroupBox);

I think the "scrollArea->setWidgetResizable( true );" 我认为“ scrollArea-> setWidgetResizable(true);” did the trick, not the double groupbox 做到了,而不是双重groupbox

I want to share the answer to my question that I have found: the answer is to add 2 groupboxes with 2 layouts and adding the scrollarea as a widget to the second layout. 我想分享我所发现问题的答案:答案是添加2个具有2种布局的分组框,并将scrollarea作为小部件添加到第二种布局。 The code will be: 代码将是:

QGroupBox* GroupBoxIn = new QGroupBox;
QVBoxLayout *LayoutIn = new QVBoxLayout;  
QGroupBox *GroupBoxOut = new QGroupBox;   
QVBoxLayout *LayoutOut = new QVBoxLayout;  
QScrollArea* scrollArea = new QScrollArea();

LayoutIn ->addWidget(Label1);
LayoutIn ->addWidget(Label2);
LayoutIn ->addWidget(Label3);
LayoutIn ->addWidget(Label4);

GroupBoxIn ->setLayout(LayoutIn ); 
scrollArea->setWidget(GroupBoxIn );  
scrollArea->setWidgetResizable( true );  
LayoutOut ->addWidget(scrollArea);      
GroupBoxOut ->setLayout(LayoutOut ); 

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

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