Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Want to add scroll area only to Layout not entire window
Qt 6.11 is out! See what's new in the release blog

Want to add scroll area only to Layout not entire window

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rumzi
    wrote on last edited by
    #1

    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.

    Pl45m4P 1 Reply Last reply
    0
    • R Rumzi

      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.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @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


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      1
      • R Offline
        R Offline
        Rumzi
        wrote on last edited by
        #3

        @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.

        Pl45m4P 1 Reply Last reply
        0
        • R Rumzi

          @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.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @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.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          4
          • R Offline
            R Offline
            Rumzi
            wrote on last edited by
            #5

            @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();
            }
            }

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved