Results 1 to 5 of 5

Thread: Minimum size hint ignored when adding widgets dynamically

  1. #1
    Join Date
    Mar 2016
    Posts
    16
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Minimum size hint ignored when adding widgets dynamically

    I've noticed that the minimum size hint of widgets in a layout are ignored if you add the widgets later on (i.e. not in the constructor).

    Here is an example that reproduces the problem I'm having.

    layoutproblem.h
    Qt Code:
    1. #ifndef LAYOUTPROBLEM_H
    2. #define LAYOUTPROBLEM_H
    3.  
    4. #include <QWidget>
    5.  
    6. class QMdiArea;
    7.  
    8. class LayoutProblem : public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit LayoutProblem(QWidget *parent = 0);
    14. virtual ~LayoutProblem();
    15.  
    16. private slots:
    17. void createUI();
    18.  
    19. private:
    20. QMdiArea* mdiArea_;
    21. };
    22.  
    23. #endif // LAYOUTPROBLEM_H
    To copy to clipboard, switch view to plain text mode 

    layoutproblem.cpp
    Qt Code:
    1. #include "layoutproblem.h"
    2.  
    3. #include <QVBoxLayout>
    4. #include <QGridLayout>
    5. #include <QMdiArea>
    6. #include <QTimer>
    7. #include <QTabWidget>
    8. #include <QApplication>
    9. #include <QPushButton>
    10.  
    11.  
    12. LayoutProblem::LayoutProblem(QWidget *parent) :
    13. QWidget(parent),
    14. mdiArea_(new QMdiArea)
    15. {
    16. setLayout(new QGridLayout);
    17. layout()->addWidget(mdiArea_);
    18. mdiArea_->setViewMode(QMdiArea::TabbedView);
    19.  
    20. resize(200, 200);
    21.  
    22. QTimer::singleShot(500, this, SLOT(createUI()));
    23. // createUI();
    24. }
    25.  
    26. LayoutProblem::~LayoutProblem()
    27. {
    28. }
    29.  
    30. void LayoutProblem::createUI()
    31. {
    32. for(unsigned i = 0; i != 2; ++i)
    33. {
    34. QWidget* window = new QWidget;
    35. window->setLayout(new QVBoxLayout);
    36. QPushButton* w = new QPushButton("test");
    37. w->setMinimumSize(200, 200);
    38. w->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    39. window->layout()->addWidget(w);
    40.  
    41. w = new QPushButton("test");
    42. w->setMinimumSize(200, 200);
    43. w->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    44. window->layout()->addWidget(w);
    45.  
    46. QTabWidget* tab = new QTabWidget;
    47. tab->addTab(window, "tab " + QString::number(i));
    48.  
    49. mdiArea_->addSubWindow(tab);
    50. tab->setWindowTitle("window " + QString::number(i));
    51. tab->showMaximized();
    52. }
    53. }
    54.  
    55. int main(int argc, char *argv[])
    56. {
    57. QApplication a(argc, argv);
    58. LayoutProblem w;
    59. w.show();
    60.  
    61. return a.exec();
    62. }
    To copy to clipboard, switch view to plain text mode 

    Run this code and you will notice that you can scale the window much smaller than the minimum size hint of the QPushButton (which is set to 200x200). Next, try commenting the QTimer line and call createUI() directly:

    Qt Code:
    1. // QTimer::singleShot(500, this, SLOT(createUI()));
    2. createUI();
    To copy to clipboard, switch view to plain text mode 

    Re-run the code and now everything works as expected.

    How can I add widgets to my layouts dynamically without Qt ignoring the minimum size hints?
    "People who get offended should be offended" -- Linus Torvalds

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Minimum size hint ignored when adding widgets dynamically

    I don't have any suggestions to resolve the issue unfortunately, but I can confirm that I experienced the exact same behavior you described on my Mac using Qt 5.7.0. Seems like it should work. Interestingly enough, setMaximumSize seems to be honored, so my best guess is it's a bug. I'd suggest that you report it as such.

    Good luck.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Mar 2016
    Posts
    16
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Minimum size hint ignored when adding widgets dynamically

    "People who get offended should be offended" -- Linus Torvalds

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Minimum size hint ignored when adding widgets dynamically

    I am not sure I understand: you are saying that you can manually resize the window to a smaller size than what the minimum sizes would allow?

    Cheers,
    _

  5. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Minimum size hint ignored when adding widgets dynamically

    @anda_skoa, yes, I used his example code and confirmed that the minimum size is ignored if set via the singleshot timer and honored when set in the constructor. I tried several things to make it work, but could not. I was able to confirm that the maximum size seems to work regardless, so it would seem to be a bug to me.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Replies: 5
    Last Post: 21st March 2016, 13:29
  2. Replies: 1
    Last Post: 7th April 2015, 08:05
  3. Adding widgets to layout dynamically
    By anbu01 in forum Newbie
    Replies: 2
    Last Post: 15th May 2014, 13:41
  4. Adding widgets dynamically
    By miw in forum Qt Programming
    Replies: 1
    Last Post: 23rd July 2009, 11:31
  5. Adding/removing widgets dynamically...
    By ReilenBlaeyze in forum Newbie
    Replies: 2
    Last Post: 16th February 2006, 11:55

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.