Maybe I should provide an example to make it more clear what I mean. Consider this simple Qt App with 2 Widgets. The left widget should only ever be as big as it needs to be while the right Widget should fill the rest of the space (without having to manually screw around with minimum sizes and sizeHint() implementations etc.).
MyQtApp
::MyQtApp( QWidget *parent, Qt
::WFlags flags
){
// create central widget
this->setCentralWidget( cw );
// create left widget
Ui::MyWidgetForm ui; ui.setupUi( left );
// create right widget
right->setStyleSheet( "background-color:#FF00FF;" );
// create layout in central widget
mainLayout->addWidget( left );
mainLayout->addWidget( right );
}
MyQtApp::MyQtApp( QWidget *parent, Qt::WFlags flags )
: QMainWindow(parent, flags)
{
// create central widget
QWidget* cw = new QWidget( this );
this->setCentralWidget( cw );
// create left widget
QWidget* left = new QWidget( cw );
Ui::MyWidgetForm ui; ui.setupUi( left );
// create right widget
QWidget* right = new QWidget( cw );
right->setStyleSheet( "background-color:#FF00FF;" );
// create layout in central widget
QHBoxLayout* mainLayout = new QHBoxLayout( cw );
mainLayout->addWidget( left );
mainLayout->addWidget( right );
}
To copy to clipboard, switch view to plain text mode
The .ui file (Ui::MyWidgetForm) has only 2 changes: its horizontal Size Policy changed to Maximum and an added QTabWidget: MyWidgetForm.png
This results in the right widget taking up all of the space, since the sizeHint of the left Widget is invalid or zero: MyQtApp1.png
If, on the other hand, I create the UI manually and make a Layout for the left Widget, in which the other Widgets will be stored (in this case just a bare QTabWidget):
MyQtApp
::MyQtApp( QWidget *parent, Qt
::WFlags flags
){
// create central widget
this->setCentralWidget( cw );
// create left widget
tabWidget
->addTab
( new QWidget,
"Tab1" );
tabWidget
->addTab
( new QWidget,
"Tab2" );
leftLayout->addWidget( tabWidget );
// create right widget
right->setStyleSheet( "background-color:#FF00FF;" );
// create layout in central widget
mainLayout->addWidget( left );
mainLayout->addWidget( right );
}
MyQtApp::MyQtApp( QWidget *parent, Qt::WFlags flags )
: QMainWindow(parent, flags)
{
// create central widget
QWidget* cw = new QWidget( this );
this->setCentralWidget( cw );
// create left widget
QWidget* left = new QWidget( cw );
left->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
QTabWidget* tabWidget = new QTabWidget( left );
tabWidget->addTab( new QWidget, "Tab1" );
tabWidget->addTab( new QWidget, "Tab2" );
QHBoxLayout* leftLayout = new QHBoxLayout( left );
leftLayout->addWidget( tabWidget );
// create right widget
QWidget* right = new QWidget( cw );
right->setStyleSheet( "background-color:#FF00FF;" );
// create layout in central widget
QHBoxLayout* mainLayout = new QHBoxLayout( cw );
mainLayout->addWidget( left );
mainLayout->addWidget( right );
}
To copy to clipboard, switch view to plain text mode
the sizeHint of the left Widget will be correct and the left Widget will be visible: MyQtApp2.png
and not expand when its parent Widget gets more space (horizontally): MyQtApp3.png
Without the added Layout it would be the same behavior as with the .ui version, naturally.
So, my question is: how can I achieve this with .ui files too? Adding a Layout in the .ui file does not work, since the uic generates a separate QWidget that acts as a parent for the Layout.
Bookmarks