Results 1 to 3 of 3

Thread: Layouts in Dialog automatically resizing on hiding/unhiding widgets

  1. #1
    Join Date
    Aug 2009
    Location
    Ireland
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question Layouts in Dialog automatically resizing on hiding/unhiding widgets

    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 createvariable.ui - 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:

    Qt Code:
    1. try {
    2. //dialog and createVariable are class data members
    3. dialog = new QDialog(this);
    4. createVariable = new Ui::CreateVariableDialog;
    5. } catch (...) {
    6. if(dialog )
    7. delete dialog;
    8. QMessageBox::critical(this, tr("Error"), tr("Critical error
    9. allocating memory. Please close application and try again"));
    10. return;
    11. }
    12.  
    13. createVariable->setupUi(dialog);
    14.  
    15. createVariable->varFuncGroupBox->hide();
    16. dialog->adjustSize();
    17.  
    18. createVariable->okButton->setEnabled(false);
    19.  
    20. QRegExp rx("^(\\w+)$");
    21. QRegExpValidator regXValidator(rx, this);
    22. createVariable->varNameEdit->setValidator(&regXValidator);
    23.  
    24. //Initialise dialog widgets
    25. //Note: variable to be edited in editVariable()
    26. setVariableDialogItems();
    27.  
    28. //Var Type Combo Box
    29. connect(createVariable->varTypeComboBox, SIGNAL(currentIndexChanged(int)),
    30. this, SLOT(varTypeComboBoxIndexChanged(int)));
    31. //Var Function Class Combo Box
    32. connect(createVariable->varFuncClassComboBox, SIGNAL(currentIndexChanged(int)),
    33. this, SLOT(varFunctionClassIndexChanged(int)));
    34. //Var Name
    35. connect(createVariable->varNameEdit, SIGNAL(textChanged(const QString&)),
    36. this, SLOT(checkIfVarEditText(const QString&)));
    37.  
    38.  
    39. if(dialog->exec() == QDialog::Accepted){
    40. //Do some stuff...
    41. }
    42. if(dialog )
    43. delete dialog;
    44. if(createVariable)
    45. delete createVariable;
    46. dialog = NULL;
    47. createVariable = NULL;
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. void MainWindow::varTypeComboBoxIndexChanged(int index)
    2. {
    3. //Default settings
    4. createVariable->varFuncGroupBox->hide();
    5. //Do stuff...
    6. //Modify based on selection
    7. switch(index){
    8. case enHexArrayVector: //Do stuff...
    9. break;
    10. case enStringVector: //Do stuff..
    11. break;
    12. case enFunction: //Do some stuff, then..
    13. createVariable->varFuncGroupBox->show();
    14. }
    15. createVariable->varDefGroupBox->adjustSize();
    16. dialog->adjustSize();
    17. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Layouts in Dialog automatically resizing on hiding/unhiding widgets

    Hi, I know this is old, but I've the exact same problem, did you ever figure it out?
    ciao

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Layouts in Dialog automatically resizing on hiding/unhiding widgets

    Take a look at the Extension example. The magic sauce is:
    Qt Code:
    1. mainLayout->setSizeConstraint(QLayout::SetFixedSize);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 25th August 2009, 13:26
  2. Layouts and form resizing
    By Chisum in forum Newbie
    Replies: 1
    Last Post: 2nd April 2009, 00:58
  3. Expanding widgets and windows automatically?
    By jakykong in forum Qt Tools
    Replies: 2
    Last Post: 25th September 2008, 01:17
  4. Layouts to ease resizing... [solved]
    By Dumbledore in forum Qt Tools
    Replies: 6
    Last Post: 16th February 2008, 16:06
  5. Performance in hiding/showing widgets
    By Paalrammer in forum Newbie
    Replies: 12
    Last Post: 14th February 2007, 18:57

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.