Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to put QscrollArea into gridlayout?



  • Hi everyone
    I wrote a simple program where the dialog window opens in maximized mode and there are some labels and buttons set to this window. I set a gridlayout to my window and added the labels and buttons to the layout , and my code worked fine. When I try to use scrollarea instead of the pink label(which is in row=1 and column=1), I don't get the same result that I got from putting the pink label in (row=1 and column=1).
    I know that when labels or buttons are supposed to be in a scrollarea, I must declare a widget and setwidget to the scrollarea, then declare a layout and setlayout to the widget. But for this case(where the scrollarea itself must be in a layout) I don't know what to do.
    I would appreciate if someone could help me
    Thanks

    Here is the code:

        this->setWindowState(this->windowState() ^ Qt::WindowMaximized);
        this->setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | 
        Qt::WindowMaximizeButtonHint);
    
        int widthW = this->frameGeometry().width();
        int heightW = this->frameGeometry().height();
    
    
        red_label = new QLabel(this);
        red_label->showFullScreen();
        red_label->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
        red_label->setLayoutDirection(Qt::RightToLeft);
        red_label->setObjectName("forlumha_Name");
        red_label->setStyleSheet(
        "QLabel#forlumha_Name {"
            "background-color:red;"
                    "}"
                    );
    
        pink_label = new QLabel(this);
        pink_label ->showFullScreen();
        pink_label ->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
        pink_label ->setLayoutDirection(Qt::RightToLeft);
        pink_label ->setObjectName("forlumha_Name");
        pink_label ->setStyleSheet(
        "QLabel#forlumha_Name {"
            "background-color:pink;"
                    "}"
                    );
    
    
    
        yellow_label = new QLabel(this);
        yellow_label->showFullScreen();
        yellow_label->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
        yellow_label->setLayoutDirection(Qt::RightToLeft);
        yellow_label->setObjectName("forlumha_Name");
        yellow_label->setStyleSheet(
        "QLabel#forlumha_Name {"
            "background-color:yellow;"
                    "}"
                    );
    
    
    
        button_one = new QPushButton(this);
        button_one->setFixedSize(50,50);
        button_two = new QPushButton(this);
        button_two->setFixedSize(50,50);
        button_three = new QPushButton(this);
        button_three->setFixedSize(50,50);
    
    
    
        //..........set layouts......//
    
        QGridLayout *layout = new QGridLayout();
        this->setLayout( layout );
        layout->addWidget(red_label,0,0,15,1);
        layout->addWidget(yellow_label,0,1,1,1);
        layout->addWidget(pink_label,1,1,14,1);
    
        layout->addWidget(button_one,6,2,1,1);
        layout->addWidget(button_two,7,2,1,1);
        layout->addWidget(button_three,8,2,1,1);
    
    

    The output :
    out1.png

    When I comment the pink_label parts and declare a scrollArea and put it in row=1 and column=1,the output is :
    output2.png


  • Lifetime Qt Champion

    Hi,

    Which version of Qt are you using ?
    On which OS ?

    Can you provide a full minimal buildable example to ensure we can test in the same conditions as you ?

    By the way, you did not show how you setup the version with the QScrollArea.



  • I would let Designer do much of the work. From Qt Creator, create a dialog project with a Form class so all the glue is already built in, setting up the UI class, the Dialog widget and all. Open designer.ui in Creator's built-in designer, drag/drop a ScrollArea onto your Dialog widget, add a GridLayout, place your buttons and labels, change the label palettes to your colors and all. You wouldn't need to write a line of code. Can't show you an example designer class, since, like SGaist said, you left out much of the details.



  • @SGaist Hi. I am using Qt creator 4.3.1. , Based on Qt 5.9.0(MSC 2015,32bit). OS: windows 10, 64bit.
    the complete code :
    dialog.h:

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    #include <QLabel>
    #include <QPushButton>
    #include <QScrollArea>
    
    
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = 0);
        ~Dialog();
        QLabel *red_label;
        QLabel *pink_label;
        QLabel *yellow_label;
        QScrollArea *scrollArea;
        QPushButton *button_one;
        QPushButton * button_two;
        QPushButton * button_three;
    
    
    private:
        Ui::Dialog *ui;
    };
        #endif // DIALOG_H
    
    
    
    
    

    dialog.cpp:

    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QLabel>
    #include <QGridLayout>
    #include <QWidget>
    
    Dialog::Dialog(QWidget *parent) :
       QDialog(parent),
       ui(new Ui::Dialog)
    {
       ui->setupUi(this);
    
       this->setWindowState(this->windowState() ^ Qt::WindowMaximized);
       this->setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
    
    
    
       red_label = new QLabel(this);
       red_label->showFullScreen();
       red_label->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
       red_label->setLayoutDirection(Qt::RightToLeft);
       red_label->setObjectName("forlumha_Name");
       red_label->setStyleSheet(
       "QLabel#forlumha_Name {"
           "background-color:red;"
                   "}"
                   );
    
      /* pink_label = new QLabel(this);
       pink_label ->showFullScreen();
       pink_label ->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
       pink_label ->setLayoutDirection(Qt::RightToLeft);
       pink_label ->setObjectName("forlumha_Name");
       pink_label ->setStyleSheet(
       "QLabel#forlumha_Name {"
           "background-color:pink;"
                   "}"
                   );*/
    
    
    
       yellow_label = new QLabel(this);
       yellow_label->showFullScreen();
       yellow_label->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
       yellow_label->setLayoutDirection(Qt::RightToLeft);
       yellow_label->setObjectName("forlumha_Name");
       yellow_label->setStyleSheet(
       "QLabel#forlumha_Name {"
           "background-color:yellow;"
                   "}"
                   );
    
    
    
       button_one = new QPushButton(this);
       button_one->setFixedSize(50,50);
       button_two = new QPushButton(this);
       button_two->setFixedSize(50,50);
       button_three = new QPushButton(this);
       button_three->setFixedSize(50,50);
    
       //.......adding scrollarea........//
       scrollArea = new QScrollArea(this);
    
    
       //..........set layouts......//
    
       QGridLayout *layout = new QGridLayout();
       this->setLayout( layout );
       layout->addWidget(red_label,0,0,15,1);
       layout->addWidget(yellow_label,0,1,1,1);
       //layout->addWidget(pink_label,1,1,14,1);
       layout->addWidget(scrollArea,1,1,14,1);
    
    
       layout->addWidget(button_one,6,2,1,1);
       layout->addWidget(button_two,7,2,1,1);
       layout->addWidget(button_three,8,2,1,1);
    
    
    
    
    }
    
    Dialog::~Dialog()
    {
       delete ui;
    }
    

    pro file:

    #-------------------------------------------------
    #
    # Project created by QtCreator 2020-11-25T19:05:12
    #
    #-------------------------------------------------
    
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = maximized2
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which as been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    
    SOURCES += \
            main.cpp \
            dialog.cpp
    
    HEADERS += \
            dialog.h
    
    FORMS += \
            dialog.ui
    
    CONFIG += mobility
    MOBILITY = 
    
    
    

    main.cpp:

    #include "dialog.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
       QApplication a(argc, argv);
       Dialog w;
       w.show();
    
       return a.exec();
    }
    
    


  • @Psnarf Hi. I posted the complete code. Actually I am supposed to use this code in another project, where I have to write it without using designer.



  • First, you need to upgrade Qt and Creator, if you can. Using zero instead of nullptr may be considered bad form. I know Creator v4.13.3 no longer uses an integer for a pointer.

    Dialog(QWidget *parent = nullptr);
    

    "the scrollarea itself must be in a layout"
    Did you read https://doc.qt.io/qt-5/layout.html?



  • @Psnarf Yes I read the documentation. I set width to the red label:

    red_label->setFixedWidth(500);
    

    and the code worked fine.
    I don't know why in the case of using labels(red and pink labels in the code) I don't need set width to the red label, but when I replace scrollarea with the pink label, the red label collapses, so I have to set width to the red label.


Log in to reply