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. Scrollbar main window
Forum Updated to NodeBB v4.3 + New Features

Scrollbar main window

Scheduled Pinned Locked Moved General and Desktop
16 Posts 5 Posters 19.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.
  • D Offline
    D Offline
    dzakens
    wrote on last edited by Chris Kawa
    #1

    i create some gui by qt designer and i want make my window scrollable when it needs to scroll because of limited size of window

    my code :

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
    
        
    
        QScrollArea *scrollarea = new QScrollArea(this);
    
        scrollarea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
        scrollarea->setWidgetResizable(false);
    
        scrollarea->setGeometry(0,0,700,600);
        this->setCentralWidget(scrollarea);
    
         ui->setupUi(this);
    
        //setWindowFlags( (windowFlags() | Qt::CustomizeWindowHint) & ~Qt::WindowMaximizeButtonHint);
        setFixedWidth(700);
        setFixedHeight(600);
    }
    

    where is the problem?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You haven't said what the problem is, but from the first look you should move the entire scrollarea code below the ui->setupUi(this); line, because that's where the actual place for the central widget is created.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dzakens
        wrote on last edited by
        #3

        i tried like this but this kind of action make application break down

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by Chris Kawa
          #4

          I see two options:

          • You create the scroll view inside your .ui file directly, and only do the ui->setupUi(this).
          • You make sure your ui has a widget as the base class, and then do the setupUi call on the contents widget of the scrollView instead of on this.

          At the moment, your scrollView and your other widgets from your .ui file kind of clash. Both are on the main window, but they know nothing of each other and have no relationship. You will need to make sure the widgets in your .ui file become a children of the contents widget of the scroll view.

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You need to be more specific - what is the problem and what does break down mean? crash? shut down? not starting? not showing window? showing something different than you intended (if so, what is it etc.). The more details you give the faster you get the answer.

            setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded) - this is the default so you don't need it
            setWidgetResizable(false) - this is the default so you don't need it
            setGeometry(0,0,700,600) - this has no effect since you're placing scrollarea as a central widget so it will resize to fill that area

            To recap - setupUi needs to be on top of any ui mangling. If your app is failing it's because of some other problem.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dzakens
              wrote on last edited by Chris Kawa
              #6
              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
              
                  ui->setupUi(this);
              
                  QScrollArea *scrollarea = new QScrollArea(this);
                  this->setCentralWidget(scrollarea);
              
                  scrollarea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
                  scrollarea->setWidgetResizable(false);
              
                  //scrollarea->setGeometry(0,0,700,600);
              
                  QObjectList list = this->children();
                  for(int i=0;i<list.size();++i)
                      list[i]->setParent(scrollarea);
              
              
                  //setWindowFlags( (windowFlags() | Qt::CustomizeWindowHint) & ~Qt::WindowMaximizeButtonHint);
                  //setFixedWidth(700);
                  //setFixedHeight(600);
              
              
              }
              

              i tried like this - doesn't turn on , occurs windows error

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #7
                QObjectList list = this->children();
                    for(int i=0;i<list.size();++i)
                        list[i]->setParent(scrollarea);
                

                This makes no sense. You are reparenting everything inside the main window, including the scrollarea to scrollarea?

                Maybe just throw out all of that code(except setupUi) and stick to designer. You can add the scrollarea and anything inside it from there.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Indeed, it doesn't make sense. In general, if you say that you get errors, you should post those errors. Those errors contain information on what wend wrong.

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dzakens
                    wrote on last edited by Chris Kawa
                    #9

                    there is written that app don't response a i have to close it by task manager

                    @Chris-Kawa said:

                    QObjectList list = this->children();
                       for(int i=0;i<list.size();++i)
                           list[i]->setParent(scrollarea);
                    

                    This makes no sense. You are reparenting everything inside the main window, including the scrollarea to scrollarea?

                    Maybe just throw out all of that code(except setupUi) and stick to designer. You can add the scrollarea and anything inside it from there.

                    i assume that jak should ui->setup(this) first , next set central widget as scroll area but how i can set all object from window to join scroll view and furthermore how i can join other object creat dynamically

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dzakens
                      wrote on last edited by Chris Kawa
                      #10
                          ui->setupUi(this);
                      
                      
                      
                          QScrollArea *scrollarea = new QScrollArea(this);
                          this->setCentralWidget(scrollarea);
                      
                          scrollarea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
                          scrollarea->setWidgetResizable(false);
                      

                      scrollarea overwrite gui created by qt designer

                      1 Reply Last reply
                      0
                      • Chris KawaC Offline
                        Chris KawaC Offline
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on last edited by Chris Kawa
                        #11

                        Take a look at this example. Maybe it will guide you a little more:

                        MainWindow::MainWindow(QWidget *parent) :
                            QMainWindow(parent),
                            ui(new Ui::MainWindow)
                        {
                            ui->setupUi(this);
                        
                            QLineEdit*    someControl1 = new QLineEdit("example");
                            QPushButton*  someControl2 = new QPushButton("example");
                            QTableWidget* someControl3 = new QTableWidget(3,3);
                        
                            QVBoxLayout* layout = new QVBoxLayout();
                            layout->addWidget(someControl1);
                            layout->addWidget(someControl2);
                            layout->addWidget(someControl3);
                        
                            QWidget* contentWidget = new QWidget();
                            contentWidget->setLayout(layout);
                        
                            QScrollArea* scrollArea = new QScrollArea();
                            scrollArea->setWidget(contentWidget);
                        
                            setCentralWidget(scrollArea);
                        }
                        

                        scroll area example!

                        M 1 Reply Last reply
                        2
                        • D Offline
                          D Offline
                          dzakens
                          wrote on last edited by
                          #12

                          thanks for your example :) now i've got na question about layouts - is there any kind of layout that i can manage manually not in vertical or horizontal way ? is there possibility to inherit from qlayout and then do it manually?

                          1 Reply Last reply
                          0
                          • Chris KawaC Offline
                            Chris KawaC Offline
                            Chris Kawa
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Windows and controls are usually rectangular so the layouts are too - apart from horizontal and vertical there are also QGridLayout, QStackedLayout and QFormLayout.

                            You might of course try to inherit from QLayout and do some crazy stuff. Something like CircleLayout would be fun to implement for sure :) But in most cases the 5 basic Qt layouts are more than enough if properly configured so check if any of them meets your requirements before trying to do it yourself.

                            1 Reply Last reply
                            0
                            • Chris KawaC Chris Kawa

                              Take a look at this example. Maybe it will guide you a little more:

                              MainWindow::MainWindow(QWidget *parent) :
                                  QMainWindow(parent),
                                  ui(new Ui::MainWindow)
                              {
                                  ui->setupUi(this);
                              
                                  QLineEdit*    someControl1 = new QLineEdit("example");
                                  QPushButton*  someControl2 = new QPushButton("example");
                                  QTableWidget* someControl3 = new QTableWidget(3,3);
                              
                                  QVBoxLayout* layout = new QVBoxLayout();
                                  layout->addWidget(someControl1);
                                  layout->addWidget(someControl2);
                                  layout->addWidget(someControl3);
                              
                                  QWidget* contentWidget = new QWidget();
                                  contentWidget->setLayout(layout);
                              
                                  QScrollArea* scrollArea = new QScrollArea();
                                  scrollArea->setWidget(contentWidget);
                              
                                  setCentralWidget(scrollArea);
                              }
                              

                              scroll area example!

                              M Offline
                              M Offline
                              MH24
                              wrote on last edited by
                              #14

                              @Chris-Kawa said in Scrollbar main window:

                              Take a look at this example. Maybe it will guide you a little more:

                              MainWindow::MainWindow(QWidget *parent) :
                                  QMainWindow(parent),
                                  ui(new Ui::MainWindow)
                              {
                                  ui->setupUi(this);
                              
                                  QLineEdit*    someControl1 = new QLineEdit("example");
                                  QPushButton*  someControl2 = new QPushButton("example");
                                  QTableWidget* someControl3 = new QTableWidget(3,3);
                              
                                  QVBoxLayout* layout = new QVBoxLayout();
                                  layout->addWidget(someControl1);
                                  layout->addWidget(someControl2);
                                  layout->addWidget(someControl3);
                              
                                  QWidget* contentWidget = new QWidget();
                                  contentWidget->setLayout(layout);
                              
                                  QScrollArea* scrollArea = new QScrollArea();
                                  scrollArea->setWidget(contentWidget);
                              
                                  setCentralWidget(scrollArea);
                              }
                              

                              scroll area example!

                              How will we go about if the scrollArea is set up in Designer, and the pushbutton and lineedit placed within it there instead of in code?

                              I modified your above code like this:

                              QWidget *contentWidget = new QWidget();
                              ui->pushButton->setParent(ui->scrollArea);
                              ui->lineEdit->setParent(ui->scrollArea);
                               //(^if I dont set their parent here they dont appear, even though in Designer I have placed them inside scrollArea)
                              
                              ui->scrollArea->setWidget(contentWidget);
                              setCentralWidget(ui->scrollArea);
                              

                              but I dont see any scrollbar.

                              JonBJ 1 Reply Last reply
                              0
                              • M MH24

                                @Chris-Kawa said in Scrollbar main window:

                                Take a look at this example. Maybe it will guide you a little more:

                                MainWindow::MainWindow(QWidget *parent) :
                                    QMainWindow(parent),
                                    ui(new Ui::MainWindow)
                                {
                                    ui->setupUi(this);
                                
                                    QLineEdit*    someControl1 = new QLineEdit("example");
                                    QPushButton*  someControl2 = new QPushButton("example");
                                    QTableWidget* someControl3 = new QTableWidget(3,3);
                                
                                    QVBoxLayout* layout = new QVBoxLayout();
                                    layout->addWidget(someControl1);
                                    layout->addWidget(someControl2);
                                    layout->addWidget(someControl3);
                                
                                    QWidget* contentWidget = new QWidget();
                                    contentWidget->setLayout(layout);
                                
                                    QScrollArea* scrollArea = new QScrollArea();
                                    scrollArea->setWidget(contentWidget);
                                
                                    setCentralWidget(scrollArea);
                                }
                                

                                scroll area example!

                                How will we go about if the scrollArea is set up in Designer, and the pushbutton and lineedit placed within it there instead of in code?

                                I modified your above code like this:

                                QWidget *contentWidget = new QWidget();
                                ui->pushButton->setParent(ui->scrollArea);
                                ui->lineEdit->setParent(ui->scrollArea);
                                 //(^if I dont set their parent here they dont appear, even though in Designer I have placed them inside scrollArea)
                                
                                ui->scrollArea->setWidget(contentWidget);
                                setCentralWidget(ui->scrollArea);
                                

                                but I dont see any scrollbar.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #15

                                @MH24
                                Not sure what you are trying to do here.

                                You use QWidget::setParent(), I would never do that, always add to a widget, or more to the point add to its layout.

                                Your scroll area has a single widget, contentWidget, but then you seem to be trying to put the button and line edit directly onto the scroll area too.

                                I would expect hierarchy to look like this:

                                Main Window
                                    central widget
                                        scroll area
                                            content widget
                                                layout for content widget in scroll area (horizontal or vertical, vertical here)
                                                    line edit
                                                    button
                                                    table widget
                                

                                I would also think you can do all this in Designer not just from code, but not certain.

                                M 1 Reply Last reply
                                3
                                • JonBJ JonB

                                  @MH24
                                  Not sure what you are trying to do here.

                                  You use QWidget::setParent(), I would never do that, always add to a widget, or more to the point add to its layout.

                                  Your scroll area has a single widget, contentWidget, but then you seem to be trying to put the button and line edit directly onto the scroll area too.

                                  I would expect hierarchy to look like this:

                                  Main Window
                                      central widget
                                          scroll area
                                              content widget
                                                  layout for content widget in scroll area (horizontal or vertical, vertical here)
                                                      line edit
                                                      button
                                                      table widget
                                  

                                  I would also think you can do all this in Designer not just from code, but not certain.

                                  M Offline
                                  M Offline
                                  MH24
                                  wrote on last edited by MH24
                                  #16

                                  @JonB I couldnt go to Designer in my actual app because my content was generated dynamically. What I wrote here was for testing. And yes I did indeed end up using layouts, giving them to contentWidget and made that a widget of scrollArea. Then set scrollArea as central widget. But I had a button whose geometry I couldnt quite set within layouts, so I separately added it to scrollArea through setParent and set its geometry from there.
                                  Thank you

                                  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