Results 1 to 14 of 14

Thread: only some of the widgets are resizing with parent

  1. #1
    Join Date
    Sep 2015
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default only some of the widgets are resizing with parent

    Hi all,
    I have created a custom widget, A, which I assign as the central widget.
    In its constructor I create and assign to it two other custom widgets - B and C.
    in A's constructor I put B and C inside a QVBoxLayout and add this layout to widget A.

    When I run the application and resize the window the B widget resizes properly but the C widget does not.
    I am attaching the redacted .h files and constructors of all these widgets.

    Any help will be appreciated.

    A.h:
    Qt Code:
    1. class A : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit A(QWidget *parent = 0);
    7. ~A();
    8. virtual int heightForWidth ( int w ) const { return w*535/1645;}
    9.  
    10. private:
    11. Ui::A *ui;
    12. B* header_widget;
    13. C* body_widget;
    14. QVBoxLayout* main_layout;
    15. void setMainLayout();
    16. };
    To copy to clipboard, switch view to plain text mode 

    A.cpp:
    Qt Code:
    1. A::A(QWidget *parent) : QWidget(parent), ui(new Ui::A)
    2. {
    3. ui->setupUi(this);
    4. setMainLayout();
    5. this->setLayout(main_layout);
    6. }
    7.  
    8. A::~A()
    9. {
    10. delete ui;
    11. }
    12.  
    13.  
    14. void A::setMainLayout(){
    15. main_layout = new QVBoxLayout();
    16.  
    17. header_widget = new B(this);
    18. body_widget = new C(this);
    19.  
    20. main_layout->addWidget(B);
    21. main_layout->addWidget(C);
    22. }
    To copy to clipboard, switch view to plain text mode 

    B.h:
    Qt Code:
    1. class B : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit B(QWidget *parent = 0);
    7. ~B();
    8. virtual int heightForWidth ( int w ) const { return w*535/1645;}
    9.  
    10.  
    11. private:
    12. Ui::B *ui;
    13. };
    To copy to clipboard, switch view to plain text mode 

    C.h:
    Qt Code:
    1. class C : public QStackedWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit C(QWidget *parent = 0);
    7. ~C();
    8. virtual int heightForWidth ( int w ) const { return w*535/1645;}
    9.  
    10. private:
    11. Ui::C *ui;
    12. A *widgetParent;
    13. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: only some of the widgets are resizing with parent

    Hmm.
    Since you create the content of A in code, why do you call ui->setUi(this)?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2015
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: only some of the widgets are resizing with parent

    Because it is auto-generated and I'd rather not mess with things I'm not sure about.
    Also when I remove it nothing changes.

  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: only some of the widgets are resizing with parent

    Quote Originally Posted by yi gi View Post
    Because it is auto-generated and I'd rather not mess with things I'm not sure about.
    Also when I remove it nothing changes.
    I would definitely remove anything that could be interfering and is not used at all.
    Leaving it in just makes it likely that you accidentally modify the designer file at some point and then wonder why your widget is messed up.

    So, what exactly happens right now?
    What does "not resize properly" mean exactly? what is C's resize behavior?

    Cheers,
    _

  5. #5
    Join Date
    Sep 2015
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: only some of the widgets are resizing with parent

    Hi anda_skoa,
    I'm sorry but apparently I was mistaken in the original post.
    As you can see widget C is a QStackedWidget with a QStackedLayout to which I add 4 different custom widgets, I.e. the hierarchy is:
    A->B,C
    C->D1,D2,D3,D4

    Now when I resize the window both B and C resize accordingly (i.e. expand/contract with A), but D1...D4 stay exactly the same.
    It might also be interesting to note that when I remove
    Qt Code:
    1. ui->setupUi(this);
    To copy to clipboard, switch view to plain text mode 
    from one of the stacked widgets (D1..D4) their content disappears.

  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: only some of the widgets are resizing with parent

    Ok, so B and C resize correcty, but the pages of C do not.

    Did you add the pages in designer (are they part of Ui::C) or did you add them manually using QStackedWidget::addWidget()?

    Cheers,
    _

  7. #7
    Join Date
    Sep 2015
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: only some of the widgets are resizing with parent

    Everything is done programmatically.
    In C I created a QStackedLayout, added each page using addWidget() and added the layout to C.

  8. #8
    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: only some of the widgets are resizing with parent

    Why did you create a stack layout?

    C is a subclass of QStackedWidget, there is no need for that.

    Also why do you have a designer form class if you are not using designer?

    Cheers,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    yi gi (16th October 2015)

  10. #9
    Join Date
    Jul 2015
    Posts
    26
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: only some of the widgets are resizing with parent

    be aware that all destructors have to be virtual

  11. #10
    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: only some of the widgets are resizing with parent

    Quote Originally Posted by newcfd View Post
    be aware that all destructors have to be virtual
    There is no new base class here, all classes are derived from QWidget and therefore QObject, which already has a virtual destructor.

    That comment does not contribute to this thread at all.

    Cheers,
    _

  12. #11
    Join Date
    Sep 2015
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: only some of the widgets are resizing with parent

    anda_skoa,

    First off - thank you very much. removing the stackedLayout and adding the widgets with addWidget really solved the problem. I also removed all references to the UI from the project.
    I now face a different resize problem - the widgets inside the QStackedWidget all have a similar structure:

    QWidget header, QWidget content, QWidget footer.
    all of whom are places inside a QGridLayout.

    The header widget has inner content and is set fine.
    The footer is currently empty (and will actually only be used in some of the pages)
    The content varies from page to page.

    For example in the first page content widget I have 3 QPushButtons set in this layout:
    Qt Code:
    1. home_layout = new QGridLayout();
    2. home_layout->addWidget(btn1,0,1,1,1);
    3. home_layout->addWidget(btn2,1,1,1,1);
    4. home_layout->addWidget(btn3,2,3,1,1);
    5.  
    6. home_layout->setColumnStretch(0,1);
    7. home_layout->setColumnStretch(1,2);
    8. home_layout->setColumnStretch(2,1);
    9.  
    10. home_layout->setRowStretch(0,1);
    11. home_layout->setRowStretch(1,1);
    12. home_layout->setRowStretch(2,1);
    To copy to clipboard, switch view to plain text mode 

    with the layout being added to the content afterwards.

    The problem is that while the header shows up and is resized as expected when resizing the parent - the content stays a small rectangle in the middle of the page.
    The goal is to have the page fill up the entire row it occupies. Any ideas what can I do about it?

  13. #12
    Join Date
    Sep 2015
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: only some of the widgets are resizing with parent

    To make things a bit easier to understand I made a PageWidget that sums it up.
    All pages inherit from this class and in all cases the content of the "content" and "footer" widgets is attached using "setLayout" in the subclass.


    PageWidget.h:

    Qt Code:
    1. class PageWidget : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit PageWidget(QWidget *parent = 0, QString& HeaderTitle = QString());
    7. ~PageWidget();
    8.  
    9. HeaderWidget* header;
    10. QWidget* content;
    11. QWidget* footer;
    12. QGridLayout* layout;
    13. };
    To copy to clipboard, switch view to plain text mode 

    PageWidget.cpp:
    Qt Code:
    1. #include "pagewidget.h"
    2.  
    3. PageWidget::PageWidget(QWidget *parent, QString &HeaderTitle) :
    4. QWidget(parent)
    5. {
    6. header = new HeaderWidget(this, HeaderTitle);
    7. content = new QWidget(this);
    8. footer = new QWidget(this);
    9.  
    10. layout = new QGridLayout(this);
    11. layout->addWidget(header,0,0,1,1,Qt::AlignCenter);
    12. layout->addWidget(content,1,0,5,1,Qt::AlignCenter);
    13. layout->addWidget(footer,2,0,1,1,Qt::AlignCenter);
    14.  
    15. setLayout(layout);
    16. }
    17.  
    18. PageWidget::~PageWidget(){}
    To copy to clipboard, switch view to plain text mode 

  14. #13
    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: only some of the widgets are resizing with parent

    For the PageWidget I would suggest just using a QVBoxLayout and using a non zero value as the second argument to addWidget for "content".

    Cheers,
    _

  15. The following user says thank you to anda_skoa for this useful post:

    yi gi (16th October 2015)

  16. #14
    Join Date
    Sep 2015
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: only some of the widgets are resizing with parent

    That did the trick. Thanks again!

Similar Threads

  1. Trouble with widgets resizing
    By papillon in forum Qt Programming
    Replies: 2
    Last Post: 9th August 2012, 11:47
  2. resizing widgets in scroller
    By mike b in forum Newbie
    Replies: 0
    Last Post: 19th May 2010, 02:35
  3. Automatic resizing of child widgets
    By JovianGhost in forum Newbie
    Replies: 3
    Last Post: 11th March 2010, 13:47
  4. Resizing child widget on changing size of parent?
    By anupamgee in forum Qt Programming
    Replies: 3
    Last Post: 13th November 2009, 09:39
  5. Prevent a QLabel resizing the parent QWidget
    By danc81 in forum Qt Programming
    Replies: 6
    Last Post: 10th November 2009, 21:54

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.