PDA

View Full Version : Layouts in Dialog automatically resizing on hiding/unhiding widgets



greatgatsby
27th February 2010, 23:43
Hi,

I am a relative newcomer to Qt - and am generally very impressed with it. I am using Qt v4.5.2 with VC++2008 plugin.

But I have been battling with a layout resizing issue for more than a week now.

I have designed a Dialog in Qt Designer (Ui file attached 4356 - I had to simplify it a bit (too big!) to allow uploading ), with the intention to show or hide some widgets (contained in a Groupbox) on the Dialog depending on the active item in a Combobox. In both cases (widgets hidden or shown), the Dialog should resize to the minimum vertical size necessary - only the horizontal size needs to be expandable.

I therefore modified the Vertical Size policy of all the widgets that wasn't 'fixed' to 'minimum' (inlcuding the outer Dialog).

I instantiate the dialog as such:



try {
//dialog and createVariable are class data members
dialog = new QDialog(this);
createVariable = new Ui::CreateVariableDialog;
} catch (...) {
if(dialog )
delete dialog;
QMessageBox::critical(this, tr("Error"), tr("Critical error
allocating memory. Please close application and try again"));
return;
}

createVariable->setupUi(dialog);

createVariable->varFuncGroupBox->hide();
dialog->adjustSize();

createVariable->okButton->setEnabled(false);

QRegExp rx("^(\\w+)$");
QRegExpValidator regXValidator(rx, this);
createVariable->varNameEdit->setValidator(&regXValidator);

//Initialise dialog widgets
//Note: variable to be edited in editVariable()
setVariableDialogItems();

//Var Type Combo Box
connect(createVariable->varTypeComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(varTypeComboBoxIndexChanged(int)));
//Var Function Class Combo Box
connect(createVariable->varFuncClassComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(varFunctionClassIndexChanged(int)));
//Var Name
connect(createVariable->varNameEdit, SIGNAL(textChanged(const QString&)),
this, SLOT(checkIfVarEditText(const QString&)));


if(dialog->exec() == QDialog::Accepted){
//Do some stuff...
}
if(dialog )
delete dialog;
if(createVariable)
delete createVariable;
dialog = NULL;
createVariable = NULL;


This works fine, and the Dialog is correctly sized to minimum when opened. When the appropriate Combobox item is selected to show the
hidden widgets, the Dialog again resizes (expands) correctly. But when a Combobox item is then selected that should hide the widgets, the dialog is not resized back to 'minimum', and some widgets are spaced to take up the excess space. But now, if another Combobox item is selected that should also hide (or maintain the hidden) widgets, the dialog now resizes correctly (to minimum size).

The Slot handling the Combobox selections does the resizing as such:



void MainWindow::varTypeComboBoxIndexChanged(int index)
{
//Default settings
createVariable->varFuncGroupBox->hide();
//Do stuff...
//Modify based on selection
switch(index){
case enHexArrayVector: //Do stuff...
break;
case enStringVector: //Do stuff..
break;
case enFunction: //Do some stuff, then..
createVariable->varFuncGroupBox->show();
}
createVariable->varDefGroupBox->adjustSize();
dialog->adjustSize();
}


Any ideas on what I am doing wrong or not doing?
greatgatsby

mbernasocchi
22nd October 2012, 11:42
Hi, I know this is old, but I've the exact same problem, did you ever figure it out?
ciao

ChrisW67
23rd October 2012, 01:10
Take a look at the Extension example. The magic sauce is:


mainLayout->setSizeConstraint(QLayout::SetFixedSize);