QScrollArea breaks layout



  • In the below code, for some reason putting the QScrollArea in the layout of the widget to the far left causes an unrelated widget to dissapear and two spaces to appear on the sides of the main layout(when you expand the window )

    @

    #include <QtGui>
    #include <QApplication>
    #include <QtCore>
    #include <QtWidgets>
    #include <stdint.h>
     
     
     
     
    int main(int argc, char *argv[])
        {
        QApplication app(argc, argv);
        QWidget *window = new QWidget;
            window->setWindowTitle("TOX");
        QHBoxLayout *layout = new QHBoxLayout;
            layout->setContentsMargins(0, 0, 0, 0);
            layout->setSpacing(0);
     
       QWidget *leftbar = new QWidget();
            leftbar->setStyleSheet("background-color:rgb(28, 28, 28)");
            leftbar->setFixedWidth(300);
            QVBoxLayout *leftbarlayout = new QVBoxLayout;
            leftbarlayout->setContentsMargins(0, 0, 0, 0);
            leftbarlayout->setSpacing(0);
            leftbarlayout->setAlignment(Qt::AlignTop);
                QWidget *statusbar = new QWidget;
                    statusbar->setStyleSheet("background-color:rgb(41, 41, 41)");
                    statusbar->setFixedHeight(100);
                    leftbarlayout->addWidget(statusbar, 0);
                QScrollArea *friendlistscroll = new QScrollArea;
                QWidget *friendlist = new QWidget;
                    friendlistscroll->setWidget(friendlist);
                    friendlist->setStyleSheet("background-color:rgb(28, 28, 28)");
                    leftbarlayout->addWidget(friendlistscroll, 1);
     
                QWidget *leftbuttons = new QWidget;
                    leftbuttons->setStyleSheet("background-color:rgb(28,89,88)");
                    leftbuttons->setFixedHeight(40);
                    leftbarlayout->addWidget(leftbuttons, 2);
                leftbar->setLayout(leftbarlayout);
        QWidget *chat = new QWidget;
            chat->setStyleSheet("background-color:rgb(255,255,255)");
     
        QWidget *rightbar = new QWidget();
            rightbar->setFixedWidth(170);
            rightbar->setMinimumHeight(50);
            QBoxLayout *rightbarlayout = new QVBoxLayout;
            rightbarlayout->setContentsMargins(0,0,0,0);
            rightbarlayout->setSpacing(0);
            rightbarlayout->setAlignment(Qt::AlignBottom);
                QWidget *rightbuttons = new QWidget;
                    rightbuttons->setStyleSheet("background-color:rgb(85, 27, 27)");
                    rightbuttons->setFixedHeight(80);
                    rightbarlayout->addWidget(rightbuttons);
     
                rightbar->setLayout(rightbarlayout);
     
        layout->addWidget(leftbar);
        layout->addWidget(chat);
     
        layout->addWidget(rightbar);
        window->setLayout(layout);
        window->show();
        return app.exec&#40;&#41;;
     
     
    }
    

    @



  • The chat widget is allocated no space if the window.width() is less than the sum of the fixed widths of the left and right bars. By default, all three widgets are allocated equal horizontal space in "layout" if the window is wide enough. The two fixed width bars are shown in the middle of their horizontal space allocation, leaving space to the left and right. The chat widget has a zero-width size hint and "disappears".

    You probably want to set the stretch factors to make all the new space go to the chat widget (the only non-fixed width one):
    @
    layout->setStretchFactor(leftbar, 0);
    layout->setStretchFactor(chat, 1);
    layout->setStretchFactor(rightbar, 0);
    @
    You'll find the chat widget gets a size then also.



  • I did what you said, I think, I set a minimum width to chat and applied that stretch factor, this causes the UI to look correct when it's opened.
    However, when I resize the window, the chat area refuses to stretch, and causes those spaces to appear again



  • Insert the three lines above immediately before line 64 of your example and do nothing else.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.