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

Want to add scroll area only to Layout not entire window



  • I need something like the image attached below.
    0_1560940750456_Sample.jpg

    I am not using any UI, create only inheriting QMainWindow of Qt::FramelessWindowHint.

    I am confused to achieve this. I want to scroll the scrollbar on mouse move/ scroll/ drug up and down.

    1. Need to fixed the label on top as well as buttons on bottom during the scroll.
    2. Scroll bar should be out side of the QVBoxLayout.
    3. I have coded as:

    setGeometry(0, 0, 240, 320);
    QWidget *win = new QWidget(this);
    QVBoxLayout mainLayout=new QVBoxLayout(win);
    QLabel *titleBar=new QLabel("FORM TITLE");
    mainLayout->addWidget(titleBar);
    QVBoxLayout *midLayout=new QVBoxLayout();

    for(int i=0;i<10;i++{
    QCheckBox *chkItems=new QCheckBox("Item :"+str.setNum(i+1));
    midLayout->addWidget(chkItems);
    }
    mainLayout->addLayout(midLayout);

    QVBoxLayout *bottomLayout=new QVBoxLayout();

    QPushButton *btnSave= new QPushButton(tr("Save"));
    QPushButton *btnCacel= new QPushButton(tr("Cancel"));
    bottomLayout->addWidget(btnSave);
    bottomLayout->addWidget(btnCancel);
    mainLayout->addLayout(bottomLayout);

    QScrollArea *scrollArea=new QScrollArea(this);
    scrollArea->setWidget(win);
    scrollArea->horizontalScrollBar()->setVisible(false);
    setCentralWidget(scrollArea);

    1. I could't find any way to add the scroll bar to mainLayout except win (Widget);

    Need help.



  • @Rumzi said in Want to add scroll area only to Layout not entire window:

    I am not using any UI

    Why not?

    @Rumzi said in Want to add scroll area only to Layout not entire window:

    I could't find any way to add the scroll bar to mainLayout except win (Widget)

    Just adding a random scrollbar to your layout is not possible. You need something like a QScrollArea and put your widget with your layout inside that area.
    EDIT: Didnt realized, that you are already using a QScrollWidet :) But you still cant change the scroll + scrollbar behavior per default
    What do you mean by

    add the scroll bar to mainLayout except win (Widget)

    Just place your title label at the top and the buttonbox below outside of the QScrollArea

    @Rumzi said in Want to add scroll area only to Layout not entire window:

    I want to scroll the scrollbar on mouse move/ scroll/ drug up and down.

    To scroll by moving the mouse to the top / bottom of the widget, you have to implement it on your own by tracking your MouseMovement inside your widget.

    EDIT_2:

    @Rumzi said in Want to add scroll area only to Layout not entire window:

    inheriting QMainWindow of Qt::FramelessWindowHint

    Instead of setting the FramelessWindow-flag to your MainWindow, better create a frameless widget / dialog



  • @Pl45m4 I am working with a small device and main purpose to do so Frameless window.

    I can add scrollbar to the Frameless window but cannot add scrollbar only to the Layout. Somehow I am missing the good point.



  • @Rumzi

    Yes you are missing something. Your Widget (win) has no layout.
    You set "win" as widget to your ScrollArea, then you set ScrollArea as CentralWidget to your MainWindow.

    win->setLayout(mainLayout);
    

    Should fix it.



  • @Pl45m4 please find my codes:

    ///My globals.h file

    #ifndef GLOBALS_H
    #define GLOBALS_H

    #define GEOMETRY_X 0
    #define GEOMETRY_Y 0
    #define GEOMETRY_HEIGHT 320
    #define GEOMETRY_WIDTH 240

    /////My createnew.h file

    #ifndef CREATENEW_H
    #define CREATENEW_H

    #include <QtGui>
    #include <QWidget>
    #include <QMainWindow>
    class CreateNew : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit CreateNew(QWidget *parent = 0);

    private:
    QVBoxLayout *mainLayout;
    QVBoxLayout *midLayout;
    QHBoxLayout *bottomLayout;
    QLabel *title;

    QPushButton *btnSave;
    QPushButton *btnClose;
    

    signals:

    private slots:

    void on_btnSave_clicked();
    void on_btnClose_clicked();
    

    protected:
    void keyPressEvent(QKeyEvent *event);
    };

    #endif // CREATENEW_H

    /////My createnew.cpp file

    #include "createnew.h"
    #include "globals.h"

    CreateNew::CreateNew(QWidget *parent) :
    QMainWindow(parent,Qt::FramelessWindowHint)
    {
    setGeometry(GEOMETRY_X, GEOMETRY_Y+25, GEOMETRY_WIDTH, GEOMETRY_HEIGHT);
    QWidget *win = new QWidget(this);
    setCentralWidget(win);

    mainLayout=new QVBoxLayout(win);
    midLayout=new QVBoxLayout();
    
    
    midLayout->setGeometry(QRect(0,50,180,260));
    title=new QLabel("Test Form");
    midLayout->addWidget(title);
    
    QString str;
    for(int i=0; i<20;i++){
        QCheckBox *qcb=new QCheckBox("Item # :"+str.setNum(i+1));
        qcb->setStyleSheet("width:80px");
        midLayout->addWidget(qcb);
    }
    mainLayout->addLayout(midLayout);
    
    QScrollArea *scrollArea=new QScrollArea(this);
    scrollArea->setWidget(mainLayout->widget());
    scrollArea->setStyleSheet("QScrollBar:vertical { width: 5px; }");
    
    bottomLayout=new QHBoxLayout();
    btnSave=new QPushButton("Save");
    btnClose=new QPushButton("Close");
    bottomLayout->addWidget(btnSave);
    bottomLayout->addWidget(btnClose);
    mainLayout->addLayout(bottomLayout);
    
    connect(btnClose, SIGNAL(clicked()), this, SLOT(on_btnClose_clicked()));
    connect(btnSave, SIGNAL(clicked()), this, SLOT(on_btnSave_clicked()));
    

    }

    void CreateNew::on_btnSave_clicked()
    {
    this->close();
    }
    void CreateNew::on_btnClose_clicked()
    {

    }

    void CreateNew::keyPressEvent(QKeyEvent *event)
    {
    if (event->key() == Qt::Key_Escape ){
    this->close();
    }
    }


Log in to reply