Results 1 to 8 of 8

Thread: dynamically building qdialog at runtime

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2012
    Posts
    57
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default dynamically building qdialog at runtime

    Hello,
    I want my QDialog class to react to the resizeevent (portrait/landscape for andriod).
    But QWidget::setLayout for the dialog only works correctly when called from the constructor it seems.
    So how is this done?

    This does not work correctly, it shows widget2 on top of widget1:
    Qt Code:
    1. Dialog::Dialog()
    2. {
    3. createstuff();
    4. }
    5.  
    6. Dialog::createstuff()
    7. {
    8. QVBoxLayout *layout = new QVBoxLayout;
    9. layout->addWidget(widget1);
    10. layout->addWidget(widget2);
    11. ..etc
    12. setLayout(layout);
    13. }
    To copy to clipboard, switch view to plain text mode 

    createstuff is going to have a parameter 'bool landscape' and will be called from resizeEvent() and I hope I will be able to use either a QVBoxLayout or a QHBoxLayout according to the event.
    This is what I came up with, so if there are better ways please let me know..

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: dynamically building qdialog at runtime

    Quote Originally Posted by Cremers View Post
    This does not work correctly, it shows widget2 on top of widget1:
    What did you expect it to show?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2012
    Posts
    57
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: dynamically building qdialog at runtime

    When I call setLayout() from the constructor those 2 widgets (which are in itself QFormLayout's with some labels etc) are stacked vertically.
    So that's what I was expecting, but it seems it only works when setLayout() is called from constructor.
    This defeats the purpose of dynamically changing the layout at runtime.
    Off course I may have the wrong approach totally.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: dynamically building qdialog at runtime

    You mean that one is literally on the other (meaning parts of them occupy the same space)? Can you provide a code snippet that demonstrates the faulty behaviour? Also do you get any warnings in the console regarding the layout?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2012
    Posts
    57
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: dynamically building qdialog at runtime

    Screenshot http://jcremers.com/screenshot.png
    You can see the 'layout 1' text behind the 'layout 2' text.
    No warnings in console.
    This is based on an example at http://qt-project.org/doc/qt-4.8/lay...iclayouts.html

    Qt Code:
    1. Edit::Edit()
    2. {
    3. box1 = new QGroupBox(tr("layout1"));
    4. box2 = new QGroupBox(tr("layout2"));
    5. buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    6. connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
    7. connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
    8. }
    9. //----------------------------------
    10. void Edit::makeform(bool landscape)
    11. {
    12. QFormLayout *layout1 = new QFormLayout;
    13. layout1->addRow(new QLabel(tr("Line 1:")), new QLineEdit);
    14. layout1->addRow(new QLabel(tr("Line 2, long text:")), new QComboBox);
    15. layout1->addRow(new QLabel(tr("Line 3:")), new QSpinBox);
    16. box1->setLayout(layout1);
    17. QFormLayout *layout2 = new QFormLayout;
    18. layout2->addRow(new QLabel(tr("Line 1:")), new QLineEdit);
    19. layout2->addRow(new QLabel(tr("Line 2, long text:")), new QComboBox);
    20. layout2->addRow(new QLabel(tr("Line 3:")), new QSpinBox);
    21. box2->setLayout(layout2);
    22. QVBoxLayout *layout = new QVBoxLayout;
    23. layout->addWidget(box1);
    24. layout->addWidget(box2);
    25. layout->addWidget(buttons);
    26. setLayout(layout);
    27. setWindowTitle(landscape ? "landscape" : "portrait");
    28. }
    29. //----------------------------------------------------------
    30. void Edit::resizeEvent(QResizeEvent *event)
    31. {
    32. int w = event->size().width(), h = event->size().height();
    33. QWidget::resizeEvent(event);
    34. makeform(w > h);
    35. }
    To copy to clipboard, switch view to plain text mode 

    I'll probably have done something stupid, yes?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: dynamically building qdialog at runtime

    Yes, you created a new set of widgets instead of reusing those you already had. Since you removed the original layout but didn't remove the widgets in it, the widgets stayed where they were and new ones were put in the newly created layout.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    Cremers (23rd October 2012)

  8. #7
    Join Date
    May 2012
    Posts
    57
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: dynamically building qdialog at runtime

    LOL

    I already was wondering about those 'new QLabel' etc.

    Thanks.


    Added after 34 minutes:


    I still haven't got it right. I have created the widgets in the constructor in stead of on the stack.
    When I set the layout of box2 to a simple widget, it works, but if I set it to layout2, the program crashes badly.

    Qt Code:
    1. //----------------------------------
    2. void Edit::makeform(bool landscape)
    3. {
    4. layout1->addRow(labelname, editname);
    5. layout1->addRow(labeldate, editdate);
    6. layout1->addRow(labeltime, edittime);
    7. box1->setLayout(layout1);
    8. layout2->addRow(labeldst, editdst);
    9. box2->setLayout(layout2);
    10. vlayout->addWidget(box1);
    11. vlayout->addWidget(box2);
    12. vlayout->addWidget(buttons);
    13. setLayout(vlayout);
    14. setWindowTitle(landscape ? "landscape" : "portrait");
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Cremers; 23rd October 2012 at 20:41.

  9. #8
    Join Date
    May 2012
    Posts
    57
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: dynamically building qdialog at runtime

    For what you are trying to do you should look at http://doc.qt.digia.com/latest/layou...iclayouts.html
    Thanks.

Similar Threads

  1. Replies: 9
    Last Post: 25th March 2011, 21:22
  2. QDialog.exec() exiting without calling QDialog::accept()
    By doggrant in forum Qt Programming
    Replies: 3
    Last Post: 2nd February 2011, 11:35
  3. Replies: 1
    Last Post: 25th September 2010, 08:20
  4. Replies: 8
    Last Post: 21st March 2010, 11:31
  5. closing a Qdialog called from a Qdialog
    By OverTheOCean in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2009, 08:02

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
  •  
Qt is a trademark of The Qt Company.