Results 1 to 10 of 10

Thread: why QT layout doesn't work on the second widget?

  1. #1
    Join Date
    Mar 2010
    Posts
    68
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default why QT layout doesn't work on the second widget?

    I have a GUI Application, the main form is widget and display as full screen. The main widget contains some sub-widgets and controls, they was placed in layouts. To this point, my application is perfect fluid and work well.

    Now, I add a second widget with a class, create few layouts, and add buttons and sub-widgets into layouts as same way I do with main widget. In the a button on main widget, I call to this second widget, the second widget show, but I don't know why the layouts doesn't works.

    Somebody help me why the layout doesn't work on the second widget? Thanks!

  2. #2
    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: why QT layout doesn't work on the second widget?

    Have you set a layout on the second widget as a whole?

    Can you post a picture of the second widget when "the layouts doesn't work" and explain what it is the isn't working? Can you post the ui file?

  3. #3
    Join Date
    Mar 2010
    Posts
    68
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: why QT layout doesn't work on the second widget?

    Quote Originally Posted by ChrisW67 View Post
    Have you set a layout on the second widget as a whole?

    Can you post a picture of the second widget when "the layouts doesn't work" and explain what it is the isn't working? Can you post the ui file?
    Yes, I have. I don't know why it doesn't works right now.

    I have attach my example project. help me!
    Attached Files Attached Files

  4. #4
    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: why QT layout doesn't work on the second widget?

    Neither of the two form UI files has a layout set on the top-level. In Designer select the Form object in the Object browser. Notice the symbol with a crossed red circle? That means no layout is applied. Right click the Form object entry, select "Lay out", then select the layout of your choice. If the widgets on the form need nested layout then you add them in a similar way.

    If you remedy this then the code you have placed in show() of both form classes is not needed (and probably should have been in the constructor anyway). You can call QWidget::showFullScreen() instead of show() from main() too.

  5. #5
    Join Date
    Mar 2010
    Posts
    68
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: why QT layout doesn't work on the second widget?

    Thank Chris!

    I follow your instruction. I created layouts by Qt Designer, but nothing change, I still having the same problem.

    I don't want to setup layouts on Qt Designer, because it makes me very hard to order or add the controls.

  6. #6
    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: why QT layout doesn't work on the second widget?

    I don't understand your aversion to using Designer to set a layout manager. You can easily remove widgets from a layout and re-add them elsewhere, even when Designer is generating the initial code for you. But hey, different strokes for different folks.

    Anyway, in the Form class move all the code in the show() method into the constructor after setupUi(). Do the same for the Widget class but delete the line calling showFullScreen(). Delete the show() method from both classes. In main.cpp call w.showFullScreen().

    How is that?

  7. #7
    Join Date
    Mar 2010
    Posts
    68
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: why QT layout doesn't work on the second widget?

    Thank Chris very much!

    I don't understand events on QT, why it doesn't works when I place it in show event.
    I move that code to constructor, if I add in to that code a message box, the layouts doesn’t works. Is that mean the layouts have to setup first?

  8. #8
    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: why QT layout doesn't work on the second widget?

    The show "event" (and its sibling hide()) is a method to make a constructed widget object visible (not visible). Imagine you have a dialog on which your compound widget is only visible when another setting is true, do you really want to create a new set of layout objects every time the widget is shown? I suspect not. The construction (the bulk of it) need only happen once and the logical place is the constructor, that's why Qt Creator's templates put the Designer Ui setup there.

    When you say "I don't want to setup layouts on Qt Designer, because it makes me very hard to order or add the controls." Can you explain what you want to do that you feel Designer is stopping you doing?

  9. #9
    Join Date
    Mar 2010
    Posts
    68
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: why QT layout doesn't work on the second widget?

    I know the construction happen once, but we can't call it again, if I want to change the layout when users resize the form to make form always look beautiful, the problem starting here. Such as, in case, I have to build a layout has spaces, and changes the order display of controls. How to update the layout in this case?

    Maybe, I don’t have experiences to use QT Designer. I drag a layout to form, really hard to add a control to this layout as directly. It made some different between design and result. I still having change on code to make it look same design.

    Thank you for your discussion.

  10. #10
    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: why QT layout doesn't work on the second widget?

    You could see how close you can get using a QGridLayout, which allows widgets to span grid cells,control sizing with stretch factors, and dynamically add/remove and rearrange them. If your idea of "beautiful" can be implemented by this layout then you could change it as needed in the QWidget::resizeEvent ( QResizeEvent * event ) (leave the layout manager in place, just remove the widgets and reinsert them in the right places). You might chose between one of two widget arrangements based on the ratio of width to height (portrait vs. landscape) and let the default layout handle it.

    For something truly fluid, with nothing in lines etc. you could implement your own layout manager by deriving from QLayout, apply that to your widget, and let it do its thing.

    You don't say what the application is. I haven't done anything for a mobile phone. There may well be better solutions for space management of a small display.

Similar Threads

  1. Widget layout in GridLayout
    By Dato0011 in forum Qt Programming
    Replies: 8
    Last Post: 7th December 2009, 09:26
  2. changing layout of a widget
    By mikro in forum Qt Programming
    Replies: 10
    Last Post: 4th August 2009, 20:21
  3. Layout (widget overlay)
    By whitefurrows in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2009, 23:54
  4. Stacked Layout. & Widget.
    By csvivek in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2008, 08:56
  5. Qt4 widget and nested layout issue.
    By bunjee in forum Qt Programming
    Replies: 12
    Last Post: 18th January 2007, 20:29

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.