Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to logically group widgets in QT for easy show/hide?

Tags:

c++

qt

I'm grouping a set of widgets in a parent and then I control the visibility/flow of these widgets by hiding/showing the parent. Is this a good way to achieve what I'm trying to do? Here is the code:

QVBoxLayout* l = new QVBoxLayout(this);
// .....

QWidget* toolset_frame = new QWidget(this);

{
   QVBoxLayout* l = new QVBoxLayout(toolset_frame);

   l->addWidget(new QLabel(tr("Stuff")));
   this->Toolset = new QLineEdit(toolset_frame);
   l->addWidget(this->Toolset);
}

l->addWidget(toolset_frame);

// Call toolset_frame->hide() and this hides everything inside the parent

The problem with this solution is that the children shrink in size slightly, I think this is due to some padding or border in the parent. Ideally the children should appear as if they are not contained in an intermediate object, but rather flow with the parent. In this case the horizontal size of the children should not be affected.

like image 278
void.pointer Avatar asked Nov 29 '25 13:11

void.pointer


1 Answers

http://doc.qt.io/qt-5/qtwidgets-dialogs-extension-example.html

This example shows that your approach is correct. Using a widget to contain the elements you want to hide, and so on.

If you want the margins/content margins/padding to be less, then change it.

// in finddialog.cpp 
extensionLayout->setMargin(0);

To quickly prototype what properties to change to get it to look right, try laying it out in the Qt Designer, and modify the property editor to get the look and feel you want.

Hope that helps.

like image 184
phyatt Avatar answered Dec 01 '25 06:12

phyatt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!