Results 1 to 13 of 13

Thread: Widget layout problem

  1. #1
    Join Date
    Dec 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Widget layout problem

    Hi everyone!

    Currently I'm working on a new widget subclass which should implement a control
    like the navigation bars known from the MS Office applications. It should like the following screenshot:

    navigation.jpg

    My basic problem is to layout QPushButtons vertically, without free space between the
    QPushButtons. I thought: oh, that's easy. I'll take a QVBoxLayout, add the QPushButtons into that
    layout and a vertical spacer at last. I'll set the layout stretch to (0,0,0,...,1), the layout spacing to zero and it will work.
    I tried that concept in Designer, and ... it worked.

    But in my real implementation, I don't want add pure QPushButtons to the layout, but a special
    own widget. And here's the problem: If I subclass QWidget (for example MyButton : public QWidget) and
    add instances of that class to the layout,there is always some space between the widgets.
    I tried that with a primitive subclass of QWidget which only added a QPushButton. The ui looks now like the screenshot:

    header.jpg

    I don't understand: where does this space come from?

    I tried a lot of things like overriding the minimumSizeHint() function and so on. But nothing helped.
    Any ideas are very welcome!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Widget layout problem

    See QLayout::setContentMargins(). And have you seen QToolBox?

  3. #3
    Join Date
    Dec 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Widget layout problem

    Lykurg, thanks for your answer.

    Yes I already placed a call to setContentMargin(0, 0, 0, 0, 0) in my code. But that doesn't help.
    And yes I know the QToolBox class. I just want to do the navigation widget as an exercise!

  4. #4
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Widget layout problem

    hi i had the same problem...take a look at the StyleSheets they usually solve everything http://doc.qt.io/qt-4.8/http://doc.qt.nokia.com/4.7-snapshot/stylesheet-examples.html

    hope it helps i can give you the stylesheet code of my application if you want

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Widget layout problem

    No! Style sheets are deprecated. Don't use them.

    Then post a example on how you use the layout, because without any code we can only guess... (And add a spacer/stretch as a last item!)

  6. #6
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Widget layout problem

    Wow men didnt know they were being deprecated :S I use the following layout for the menu
    Qt Code:
    1. ui->menuBar->setStyleSheet ( "QMenuBar"
    2. "{"
    3. " background-color: qlineargradient( x1:0, y1:0, x2:0, y2:1, "
    4. " stop: 0 #b0c4de, "
    5. " stop: 0.8 #e6e6e8, "
    6. " stop: 1 #b0c4de ); "
    7. " margin: 0px; "
    8. " padding: 5px; "
    9. " spacing: 5px; "
    10. "}"
    11. "QMenuBar::item"
    12. "{"
    13. " background-color: qlineargradient( x1:0, y1:0, x2:0, y2:1, "
    14. " stop: 0 #b0c4de, "
    15. " stop: 0.8 #c8dcf6, "
    16. " stop: 1 #4682b4 ); "
    17. " border-width: 1px; "
    18. " border-color: darkkhaki; "
    19. " border-style: solid; "
    20. " border-radius: 5px; "
    21. " margin: 3px; "
    22. " padding: 4px; "
    23. " spacing: 20px; "
    24. "}"
    25. "QMenuBar::item:selected { /* when selected using mouse or keyboard */"
    26. "background: #a8a8a8;"
    27. "}"
    28. "QMenuBar::item:pressed {"
    29. "background: #888888;"
    30. "}"
    31. "QMenu {"
    32. "background-color: white;"
    33. "margin: 5px; /* some spacing around the menu */"
    34. "width: 180px;"
    35. "}"
    36. "QMenu::item {"
    37. "padding: 10px 28px 10px 23px;"
    38. "font: bold 12px;"
    39. "border: 1px solid transparent; /* reserve space for selection border */}"
    40. "QMenu::item:selected {"
    41. "border-color: darkblue;"
    42. "background: rgba(100, 100, 100, 150);"
    43. "}"
    44. "QMenu::icon:checked { /* appearance of a 'checked' icon */"
    45. "background: gray;"
    46. "border: 1px inset gray;"
    47. "position: absolute;"
    48. "top: 1px;"
    49. "right: 1px;"
    50. "bottom: 1px;"
    51. "left: 1px;}"
    52. "QMenu::separator {"
    53. "height: 2px;"
    54. "background: lightblue;"
    55. "margin-left: 10px;"
    56. "margin-right: 5px;}"
    57. "QMenu::indicator {"
    58. "width: 13px;"
    59. "height: 13px;}"
    60.  
    61. );
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Dec 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Widget layout problem

    Ok here comes the code. The following code is created by designer. I use two QPushButtons and a vertical spacer. They are put into a vertical layout. The vertical stretch factors are 0, 0, 1. The spacing is set to 11 so the buttons are aligned.

    void setupUi(QMainWindow *MainWindow)
    {
    if (MainWindow->objectName().isEmpty())
    MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    MainWindow->resize(400, 300);
    centralWidget = new QWidget(MainWindow);
    centralWidget->setObjectName(QString::fromUtf8("centralWidget")) ;
    widget = new QWidget(centralWidget);
    widget->setObjectName(QString::fromUtf8("widget"));
    widget->setGeometry(QRect(0, 0, 114, 113));
    verticalLayout = new QVBoxLayout(widget);
    verticalLayout->setSpacing(11);
    verticalLayout->setContentsMargins(11, 11, 11, 11);
    verticalLayout->setObjectName(QString::fromUtf8("verticalLayout") );
    verticalLayout->setContentsMargins(0, 0, 0, 0);
    pushButton = new QPushButton(widget);
    pushButton->setObjectName(QString::fromUtf8("pushButton"));
    pushButton->setStyleSheet(QString::fromUtf8("QPushButton { background: rgb(77, 77, 77); color: rgb(255, 255, 255)}"));

    verticalLayout->addWidget(pushButton);

    pushButton_2 = new QPushButton(widget);
    pushButton_2->setObjectName(QString::fromUtf8("pushButton_2") );
    pushButton_2->setStyleSheet(QString::fromUtf8("QPushButton { background: rgb(77, 77, 77); color: rgb(255, 255, 255)}"));

    verticalLayout->addWidget(pushButton_2);

    verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);

    verticalLayout->addItem(verticalSpacer);

    verticalLayout->setStretch(2, 1);
    MainWindow->setCentralWidget(centralWidget);
    menuBar = new QMenuBar(MainWindow);
    menuBar->setObjectName(QString::fromUtf8("menuBar"));
    menuBar->setGeometry(QRect(0, 0, 400, 22));
    MainWindow->setMenuBar(menuBar);
    mainToolBar = new QToolBar(MainWindow);
    mainToolBar->setObjectName(QString::fromUtf8("mainToolBar")) ;
    MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
    statusBar = new QStatusBar(MainWindow);
    statusBar->setObjectName(QString::fromUtf8("statusBar"));
    MainWindow->setStatusBar(statusBar);

    retranslateUi(MainWindow);

    QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi

    This result is:

    pushButtonLayout.jpg

    That's fine.

    Now I replace the QPushButtons in the code by a class of mine "MyButton". The MyButton class:


    class MyButton : public QWidget
    {
    Q_OBJECT
    public:
    explicit MyButton(QWidget *parent = 0);

    QSize minimumSizeHint() const;
    signals:

    public slots:

    private:
    QPushButton* m_button;
    };



    MyButton::MyButton(QWidget *parent) :
    QWidget(parent)
    {
    m_button = new QPushButton("PushButton", this);
    }


    QSize MyButton::minimumSizeHint() const {
    return m_button->minimumSizeHint();
    }


    The result is now this:

    mybutton.jpg

    I guess the problem is the MyButton class. Because when I change MyButton to derive from QPushButton the blank space is not there. So, where do I go wrong in the MyButton class? Which method do I have to override?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Widget layout problem

    Control the sizeHint and sizePolicy of your widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Widget layout problem

    Quote Originally Posted by QtMaci View Post
    MyButton::MyButton(QWidget *parent) :
    QWidget(parent)
    {
    m_button = new QPushButton("PushButton", this);
    }
    Shouldn't you also install a layout on the custom widget?

  10. #10
    Join Date
    Dec 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Widget layout problem

    Hello again!

    Now I tried several things:

    1. As Lykurg mentioned I added a layout in the MyButton class:


    MyButton::MyButton(QWidget *parent) :
    QWidget(parent)
    {
    QVBoxLayout* layout = new QVBoxLayout();
    m_button = new QPushButton("PushButton", this);
    layout->addWidget(m_button);
    layout->addStretch(1);
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);
    }

    But that doesn't change the result: the gap between two MyButton widgets is there :-(

    2. As wysota suggested I reimplemented the sizeHint() method as follows:

    QSize MyButton::sizeHint() const {
    return m_button->sizeHint();
    }

    and to consider the sizePolicy property I added the following line to the MyButton cstr. above:
    setSizePolicy(m_button->sizePolicy());

    The result is: the height of the buttons shrinked, the gap is there - see the following screenshot:

    MyButton.jpg

    Any more ideas?

  11. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Widget layout problem

    Don't add a stretch in MyButton!

  12. #12
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Widget layout problem

    I can't see a problem here, just get the MyButton above, stick it in the layout and it works. You just have to configure container layout correctly:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. QVBoxLayout* lay = new QVBoxLayout();
    5. lay->setSpacing( 0 );
    6. lay->setMargin( 0 );
    7. lay->addWidget( new MyButton() );
    8. lay->addWidget( new MyButton() );
    9. lay->addWidget( new MyButton() );
    10. lay->addStretch( 1 ); // to push the buttons close together
    11.  
    12. QWidget* w = new QWidget();
    13. w->setLayout( lay );
    14. this->setCentralWidget( w );
    15. }
    To copy to clipboard, switch view to plain text mode 
    button code as posted above:
    Qt Code:
    1. MyButton::MyButton(QWidget *parent) :
    2. QWidget(parent)
    3. {
    4. QVBoxLayout* layout = new QVBoxLayout();
    5. QPushButton* m_button = new QPushButton("PushButton", this);
    6. layout->addWidget(m_button);
    7. layout->addStretch(1); // not needed
    8. layout->setSpacing(0); // not needed if only single item is in the layout
    9. layout->setContentsMargins(0, 0, 0, 0);
    10. setLayout(layout);
    11. }
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Dec 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Widget layout problem

    I tried the solution of Spitfire and it works. That's exactly what I wanted to do. I compared it again to the layout code created by designer (see a mile above). I detected two problems: the verticalLayout is never set to a widget and the size needed is not 11 but 0. But: the sizeHint() method needs to be reimplemented in MyButton. That's the solution. All my faults...
    A big thank you and a round of drinks goes to everyone answered to this thread. Thanks a lot!!!

Similar Threads

  1. mainwindow layout vs widget layout
    By fatecasino in forum Newbie
    Replies: 2
    Last Post: 14th December 2010, 14:45
  2. Qt Designer Problem moving widget after breaking layout
    By nickolais in forum Qt Tools
    Replies: 0
    Last Post: 10th June 2010, 22:21
  3. How to change a widget to other widget in layout?
    By Kevin Hoang in forum Qt Programming
    Replies: 2
    Last Post: 20th March 2010, 10:55
  4. Problem setting central Widget layout
    By frenk_castle in forum Newbie
    Replies: 2
    Last Post: 26th September 2009, 16:43
  5. Layout (widget overlay)
    By whitefurrows in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2009, 23: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.