Results 1 to 8 of 8

Thread: Layout

  1. #1
    Join Date
    Jan 2020
    Posts
    18
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Layout

    I am trying to create a layout on top of a widget derived from QWidget:

    myWidget::myWidget(QWidget *parent, const char *name )
    {
    setFixedSize(300, 150); // Just for now

    m_plotpanel = new PlotPanel(this); // PlotPanel is scubclassed from QWidget, not MainWindow
    m_controllpanel = new PlotPanel(this); // for now
    m_plotpanel->setStyleSheet("background-color: red"); // to see that
    m_controllpanel->setStyleSheet("background-color: black"); // they are both there

    horLayout = new QHBoxLayout;
    horLayout->addWidget(m_plotpanel);
    horLayout->addWidget(m_controllpanel);
    m_plotpanel->show();
    m_controllpanel->show();

    widgets.png

    Both widgets are shown, with the right colours, but they are detached from myWidget. How can I attach them to myWidget? Do I have to use QDockWidget?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,468
    Thanked 273 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Layout

    Add this line (QWidget::setLayout):
    Qt Code:
    1. setLayout(horLayout);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2020
    Posts
    18
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Layout

    Quote Originally Posted by Lesiok View Post
    Add this line (QWidget::setLayout):
    Qt Code:
    1. setLayout(horLayout);
    To copy to clipboard, switch view to plain text mode 
    I have tried that, but them I the widgets do not show at all.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,310
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Layout

    Remove the two show() calls for the child widgets. They will be "shown" when your myWidget instance is shown. And add the "setLayout()" call instead.

    If myWidget is derived from QWidget and you intend for it to be the central widget for a QMainWindow, then you need to call setCentralWidget() with the pointer in your main window constructor. If it is meant to be a standalone, top-level widget (i.e. floating), then at some point in your code you also need to call show() on the myWidget instance.
    <=== 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.

  5. #5
    Join Date
    Jan 2020
    Posts
    18
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Layout

    Quote Originally Posted by d_stranz View Post
    Remove the two show() calls for the child widgets. They will be "shown" when your myWidget instance is shown. And add the "setLayout()" call instead.

    If myWidget is derived from QWidget and you intend for it to be the central widget for a QMainWindow, then you need to call setCentralWidget() with the pointer in your main window constructor. If it is meant to be a standalone, top-level widget (i.e. floating), then at some point in your code you also need to call show() on the myWidget instance.
    I have got a QStackedWidget in my MainWindow and I call setCentralWidget there:
    Qt Code:
    1. setCentralWidget(m_StackedWidget);
    To copy to clipboard, switch view to plain text mode 
    and that works perfectly, in the sense that I can change between the two stacked widgets (using slots). myWidget is supposed to "sit on top of" both of these stacked widgets, so maybe there is some interaction here that I have not understood? The widgests in the layout still do not show; without
    Qt Code:
    1. setLayout(horLayout)
    To copy to clipboard, switch view to plain text mode 
    the two widgets in my layout are visible, but free-floating. With
    Qt Code:
    1. setLayout(horLayout)
    To copy to clipboard, switch view to plain text mode 
    they remain hidden, and I see the top stacked widget (they have all got different colours).

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,310
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Layout

    So, how are you creating your instance of myWidget? As a child of your QMainWindow or of the QStackedWidget? In that case, it is probably being shown but is hidden behind the main window or stacked widget.

    If you want your myWindow instance to be free-floating, then you need to create it as a top-level widget (i.e. with a NULL parent) and call its show() method to make it visible. It will be free-floating and completely independent of your main window, so it is possible that when it is shown, it will actually be behind the main window and you will have to move the main window to be able to see it. That could be what is happening now.

    The reason your two widgets are being shown independently with your current code is that even though you are adding them to a layout, you aren't putting that layout into any widget. So when you call show() on those sub-widgets, they essentially get re-parented (with NULL parents) as top-level widgets, ignoring the layout.

    If myWidget is free-floating, it can be anywhere on the screen and won't be confined to the area of the main window.
    <=== 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.

  7. #7
    Join Date
    Jan 2020
    Posts
    18
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Layout

    Quote Originally Posted by d_stranz View Post
    So, how are you creating your instance of myWidget? As a child of your QMainWindow or of the QStackedWidget? In that case, it is probably being shown but is hidden behind the main window or stacked widget.

    If you want your myWindow instance to be free-floating, then you need to create it as a top-level widget (i.e. with a NULL parent) and call its show() method to make it visible. It will be free-floating and completely independent of your main window, so it is possible that when it is shown, it will actually be behind the main window and you will have to move the main window to be able to see it. That could be what is happening now.

    The reason your two widgets are being shown independently with your current code is that even though you are adding them to a layout, you aren't putting that layout into any widget. So when you call show() on those sub-widgets, they essentially get re-parented (with NULL parents) as top-level widgets, ignoring the layout.

    If myWidget is free-floating, it can be anywhere on the screen and won't be confined to the area of the main window.
    I finally got it working. The problem turned out to be that I had no widgets on m_plotpanel, so I think that the layout was OK, but its dimensions were probably zero (0x0). Declaring a QLabel in m_plotpanel, thus
    Qt Code:
    1. QLabel *label = new QLabel(this);
    To copy to clipboard, switch view to plain text mode 
    made the red and black widgets show up as two small squares. Inserting some text increased the size accordingly. Adding layout to m_plotpanel also works as I wold expect from the documentation, so my problem is solved.

    Although it doesn't really matter, I am a bit curious why empty labels have finite dimensions. Do they have some minimum size? Anyway, thanks for the help.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,310
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Layout

    Although it doesn't really matter, I am a bit curious why empty labels have finite dimensions. Do they have some minimum size? Anyway, thanks for the help.
    Glad you got it working. I didn't realize your two child widgets were empty. When I am mocking up a UI and want some placeholders that I will replace later with real widgets, I usually choose a QTreeView or QTableView since these have a finite size and will expand to fill the space they are placed into.

    QLabel inherits from QFrame, which contains a frameWidth property. It defaults to 1, so there is a 1-pixel border all around it. Layouts also contain a margin, which separates the layout from its container as well as establishes padding between items in the layout. So there is almost always some empty space allocated to yield a finite size.
    <=== 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.

Similar Threads

  1. Replies: 1
    Last Post: 18th March 2016, 16:08
  2. Replies: 6
    Last Post: 14th February 2011, 00:06
  3. Replies: 0
    Last Post: 12th December 2010, 06:09
  4. Replies: 0
    Last Post: 25th May 2009, 11:00
  5. Replies: 7
    Last Post: 15th June 2007, 17:13

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.