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.1k 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.
  • 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