Results 1 to 3 of 3

Thread: QStackedWidget on QHBoxLayout

  1. #1
    Join Date
    Jan 2020
    Qt products

    Default QStackedWidget on QHBoxLayout

    I am trying to use QHBoxLayout for the following layout:

    | m_widgetS | m_stackedWidget |

    I want m_widgetS to be placed on the left hand side of a QStackedWidget populated with m_widgetA and later m_widgetB, m_widgetC, etc.
    I am trying to create something similar to the KDE config dialogue, where I can switch widgets by selecting from a list on m_widgetS.
    I am still using Qt 5 and my code looks like this:

    m_widgetS = new WidgetS(this); // subclassed QWidget
    m_widgetS->setStyleSheet("background-color: red"); // Make widgets visible

    m_widgetA = new WidgetA(this); // subclassed QWidget
    m_widgetA->setStyleSheet("background-color: blue");

    m_stackedWidget = new QStackedWidget(this);
    m_stackedWidget->addWidget(m_widgetA); // just one for now

    m_hBox = new QHBoxLayout(this);
    m_hBox->addWidget(m_widgetS); // The static widget
    m_hBox->addWidget(m_stackedWidget); // <-------


    The strange thing is that the stacked widget covers the whole 600x600 area. If I comment out the line marked "// <-------" I can see the blue m_widgetA, now sized 400x600 and the red m_widgetS; that latter of size 600x600 and placed in front of the blue m_widgetA. The blue one is slightly higher than the red one (also strange), so I can see both. I have red the docs, but something must be wrong. Any ideas?

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 858 Times in 845 Posts
    Qt products

    Default Re: QStackedWidget on QHBoxLayout

    If I comment out the line marked "// <-------"
    This is because you are creating all of your widgets as children of "this" (whatever it is), so they are all siblings. Once you start adding them to the HBox or stacked widget, you change the parent-child relationships so they are not siblings any more. The stacked widget and widget_s are still siblings, but now widget_a is a child of the stacked widget.

    There could be several problems:

    1 - Your widget_s does not have a minimum size, so the HBox thinks it can squeeze it to zero width.

    2 - The stacked widget has a size policy that says it can expand to take up as much width as possible

    3 - Your widget_a does not have a maximum size, so it can grow to match the stacked widget's width

    4 - You have not set any "stretch" properties on the boxes within your HBox, the the HBox will size the widgets according to their preferences.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2020
    Qt products

    Default Re: QStackedWidget on QHBoxLayout

    I managed to fix it by setMinimumSize(). Thanks for the help.

Similar Threads

  1. QHBoxLayout
    By VitaliBR in forum Qt Programming
    Replies: 7
    Last Post: 6th December 2010, 22:03
  2. QHBoxLayout & QVBoxLayout
    By damodharan in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2010, 15:13
  3. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 08:57
  4. QHBoxLayout Problem
    By Seema Rao in forum Qt Programming
    Replies: 5
    Last Post: 29th April 2006, 16:51
  5. Replies: 3
    Last Post: 19th April 2006, 21:53


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.