Results 1 to 8 of 8

Thread: QWidget resize troubles

  1. #1
    Join Date
    Mar 2015
    Posts
    20
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default QWidget resize troubles

    Hallo, I have to manage 3 different layout for each tab of a QTabWidget (but, in general, for its currentWidget).
    MyClass::MyFunction is called every time a layout change is necessary.
    In fact, my code is not able to let m_pWidget1 and m_pWidget2 be resized (both have resize() function overwritten, but it's not called as I expect).
    Why?
    And: How can I force m_pWidget1 and m_pWidget2 to be resized?

    I post the code (initializations omitted, but it's not there the problem):

    Qt Code:
    1. // .h
    2.  
    3. class MyClass
    4. {
    5. QTabWidget * m_pTabWidget;
    6.  
    7. QHBoxLayout * m_pHLayout;
    8.  
    9. QWidget * m_pWidget1;
    10. QWidget * m_pWidget2;
    11. };
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. // .cpp
    2.  
    3. void MyClass::MyFunction(void)
    4. {
    5. m_pHLayout = new QHBoxLayout();
    6.  
    7. m_pHLayout->setMargin(0);
    8. m_pHLayout->setSpacing(0);
    9.  
    10. switch(m_eLayout)
    11. {
    12. case e_Layout_1:
    13. {
    14. m_pHLayout->addWidget(m_pWidget1);
    15. m_pHLayout->addWidget(m_pWidget2);
    16. }
    17. break;
    18. case e_Layout_2:
    19. {
    20. m_pHLayout->addWidget(m_pWidget1, 1);
    21. m_pHLayout->addWidget(m_pWidget2, 2);
    22. }
    23. break;
    24. case e_Layout_3:
    25. {
    26. m_pHLayout->addWidget(m_pWidget1, 2);
    27. m_pHLayout->addWidget(m_pWidget2, 1);
    28. }
    29. break;
    30. }
    31.  
    32. m_pTabWidget->currentWidget()->setLayout(m_pHLayout); //HERE - I expect that the resize event for both m_pWidget1 and pWidget2 is called.....
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QWidget resize troubles

    From the documentation of QWidget::setLayout():
    If there already is a layout manager installed on this widget, QWidget won't let you install another. You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout.
    No part of the code you posted deletes the existing layout, if any.

  3. #3
    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: QWidget resize troubles

    Since the only thing you seem to change is the stretch factor associated with the two widgets, why don't you keep the layout and just call setStretchFactor()?

    Cheers,
    _

  4. #4
    Join Date
    Mar 2015
    Posts
    20
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Re: QWidget resize troubles

    From the code I posted you are right, yeye_olive; in fact, all the object I reported as pointer (*) are in my original code SmartPointer, a class which incapsulate the pointer lifecycle avoiding memory leaks.
    I worried it could be misleading to post the whole code, but I omitted an essential particular: the old layout is in fact destroyed, as the real code is:

    Qt Code:
    1. SmartPointer<QHBoxLayout> m_pHLayout;
    2.  
    3. m_pHLayout = SmartPointer<QHBoxLayout>(new QHBoxLayout()); // operator = of class template <class T> class SmartPointer deletes the incapsulated object if it's assigned to a different one.
    To copy to clipboard, switch view to plain text mode 

    So, thank you for the hints, but the problem is not the old layout deletion (unless I have to call something like "detachLayout" before it goes deleted).
    I'm suspecting that's not enough to simply assign a new layout to a certain widget (after having destroyed the previous one!) to grant the resizing of the layouts children.
    I'm looking for the action I have to take to grant such resizing.

  5. #5
    Join Date
    Mar 2015
    Posts
    20
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Re: QWidget resize troubles

    Hallo, i found a solution: no idea if it's stylish to call hide() and show() for currentWidget!!

    Qt Code:
    1. void MyClass::MyFunction(void)
    2. {
    3. m_pTabWidget->currentWidget()->hide();
    4.  
    5. m_pHLayout = SmartPointer<QHBoxLayout>(new QHBoxLayout());
    6.  
    7. // ................. the same code as before .............................
    8.  
    9. m_pTabWidget->currentWidget()->setLayout(m_pHLayout); //HERE - I expect that the resize event for both m_pWidget1 and pWidget2 is called.....
    10.  
    11. m_pTabWidget->currentWidget()->show(); // ...in fact it's called HERE !!!!!!!!
    12. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: QWidget resize troubles

    So, why do you need a new layout if you are not changing the type of layout or ordering of widgets?

    Cheers,
    _

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QWidget resize troubles

    What you observe is strange. There are several ways to force a widget to update its layout like QLayout::activate() or QWidget::updateGeometry(), but they should not be needed.

    Be careful with those smart pointers: I suppose that the widget deletes its layout in its destructor, which means that the last layout is deleted twice.

    In any case, you should follow anda_skoa's suggestion and reuse the existing layout if you can.

  8. #8
    Join Date
    Mar 2015
    Posts
    20
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Re: QWidget resize troubles

    To reuse the same layout is also feasible for me, i'll try to follow anda_skoa's suggestion..
    Thank you!

Similar Threads

  1. how to resize Qmainwindow with Qwidget ?
    By zeynepb.bil in forum Qt Programming
    Replies: 10
    Last Post: 28th September 2017, 23:48
  2. How to disable resize in a QWidget
    By aguleo in forum Newbie
    Replies: 2
    Last Post: 17th February 2013, 20:58
  3. Replies: 15
    Last Post: 11th September 2012, 07:26
  4. Qwidget resize delay problem
    By zl2k in forum Qt Programming
    Replies: 2
    Last Post: 10th November 2008, 14:02
  5. Resize QWidget in QMainWindow
    By aamer4yu in forum Qt Programming
    Replies: 1
    Last Post: 8th March 2007, 12:16

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.