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. Syntax for Vector of QPushbuttons added to FlowLayout
Forum Updated to NodeBB v4.3 + New Features

Syntax for Vector of QPushbuttons added to FlowLayout

Scheduled Pinned Locked Moved Solved General and Desktop
52 Posts 6 Posters 5.8k Views 2 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.
  • jsulmJ jsulm

    @Swati777999 said in Syntax for Vector of QPushbuttons added to FlowLayout:

    I need the object of QMainWindow

    Then your Window should subclass QMainWindow, not QWidget...

    Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by
    #43

    @jsulm said in Syntax for Vector of QPushbuttons added to FlowLayout:

    @Swati777999 said in Syntax for Vector of QPushbuttons added to FlowLayout:

    I need the object of QMainWindow

    Then your Window should subclass QMainWindow, not QWidget...

    Actually, I am making changes in the FlowLayout Example given in the Example section of QtCreator. That's why doing in this way.

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    jsulmJ 1 Reply Last reply
    0
    • Swati777999S Swati777999

      @jsulm said in Syntax for Vector of QPushbuttons added to FlowLayout:

      @Swati777999 said in Syntax for Vector of QPushbuttons added to FlowLayout:

      I need the object of QMainWindow

      Then your Window should subclass QMainWindow, not QWidget...

      Actually, I am making changes in the FlowLayout Example given in the Example section of QtCreator. That's why doing in this way.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #44

      @Swati777999 If you mean this example https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/layouts/flowlayout?h=5.15 then I can't see where a QMainWindow instance is created. Window is simply a QWidget, but if you need QMainWindow you can inherit from it instead of QWidget.
      Keep in mind that examples are exactly that: examples. You need to adopt them to your needs properly.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Swati777999S 1 Reply Last reply
      1
      • jsulmJ jsulm

        @Swati777999 If you mean this example https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/layouts/flowlayout?h=5.15 then I can't see where a QMainWindow instance is created. Window is simply a QWidget, but if you need QMainWindow you can inherit from it instead of QWidget.
        Keep in mind that examples are exactly that: examples. You need to adopt them to your needs properly.

        Swati777999S Offline
        Swati777999S Offline
        Swati777999
        wrote on last edited by
        #45

        @jsulm said in Syntax for Vector of QPushbuttons added to FlowLayout:
        @Swati777999 If you mean this example https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/layouts/flowlayout?h=5.15 then I can't see where a QMainWindow instance is created. Window is simply a QWidget, but if you need QMainWindow you can inherit from it instead of QWidget.

        Yes , right , this example I was referring to. Window is a subclass of QWidget here and I created QMainWindow to utilise scroll property.

        “ In order to be irreplaceable, one must always be different” – Coco Chanel

        jsulmJ 1 Reply Last reply
        0
        • Swati777999S Swati777999

          @jsulm said in Syntax for Vector of QPushbuttons added to FlowLayout:
          @Swati777999 If you mean this example https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/layouts/flowlayout?h=5.15 then I can't see where a QMainWindow instance is created. Window is simply a QWidget, but if you need QMainWindow you can inherit from it instead of QWidget.

          Yes , right , this example I was referring to. Window is a subclass of QWidget here and I created QMainWindow to utilise scroll property.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #46

          @Swati777999 said in Syntax for Vector of QPushbuttons added to FlowLayout:

          Window is a subclass of QWidget here and I created QMainWindow to utilise scroll property

          And this is the wrong approach.
          Subclass QMainWindow and do not create any additional QMainWindow instances.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          Swati777999S 3 Replies Last reply
          1
          • jsulmJ jsulm

            @Swati777999 said in Syntax for Vector of QPushbuttons added to FlowLayout:

            Window is a subclass of QWidget here and I created QMainWindow to utilise scroll property

            And this is the wrong approach.
            Subclass QMainWindow and do not create any additional QMainWindow instances.

            Swati777999S Offline
            Swati777999S Offline
            Swati777999
            wrote on last edited by
            #47

            @jsulm got it. Let me try.

            “ In order to be irreplaceable, one must always be different” – Coco Chanel

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @Swati777999 said in Syntax for Vector of QPushbuttons added to FlowLayout:

              Window is a subclass of QWidget here and I created QMainWindow to utilise scroll property

              And this is the wrong approach.
              Subclass QMainWindow and do not create any additional QMainWindow instances.

              Swati777999S Offline
              Swati777999S Offline
              Swati777999
              wrote on last edited by
              #48

              @jsulm said in Syntax for Vector of QPushbuttons added to FlowLayout:

              @Swati777999 said in Syntax for Vector of QPushbuttons added to FlowLayout:

              Window is a subclass of QWidget here and I created QMainWindow to utilise scroll property

              And this is the wrong approach.
              Subclass QMainWindow and do not create any additional QMainWindow instances.
              Done!

              Window::Window()

              {
              QScrollArea *scroll =new QScrollArea();
              QWidget *flowWidget = new QWidget();
              FlowLayout *flowLayout = new FlowLayout();
              
              int n=20;
              QVector <QPushButton *> buttons(n);
              
              for (int ii=0;ii<n;ii++)
              {
                  QPushButton * pb = new QPushButton(); // creating buttons
                     pb->setMinimumSize(200,200);
                     buttons.push_back(pb); // adding buttons to qvector
                     flowLayout->addWidget(pb);
              
              }
              
              flowWidget ->setMinimumSize(1200,1200);
              scroll->setWidget(flowWidget);
              flowWidget->setLayout(flowLayout);
              this->setCentralWidget(scroll);
              this->show();
              }
              

              It's working fine with Window as the subclass of QMainWindow.

              “ In order to be irreplaceable, one must always be different” – Coco Chanel

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @Swati777999 said in Syntax for Vector of QPushbuttons added to FlowLayout:

                Window is a subclass of QWidget here and I created QMainWindow to utilise scroll property

                And this is the wrong approach.
                Subclass QMainWindow and do not create any additional QMainWindow instances.

                Swati777999S Offline
                Swati777999S Offline
                Swati777999
                wrote on last edited by
                #49

                @jsulm

                I am doing part-2 for the following layout :
                QPushButtons+scroll.PNG

                blank display unfortunately!

                {
                QWidget *WindowWidget= new QWidget(this);
                WindowWidget->setMinimumSize(2000,2000);
                
                QVBoxLayout *WindowLayout = new QVBoxLayout(WindowWidget);
                
                
                QLabel *WindowHeading = new QLabel("Experimenting with Scrollbar in Flow Layout");
                WindowLayout->addWidget(WindowHeading);
                WindowLayout->setAlignment(WindowHeading,Qt::AlignCenter);
                QScrollArea *scroll =new QScrollArea();
                
                QWidget *flowWidget = new QWidget();
                flowWidget ->setMinimumSize(1200,1200);
                FlowLayout *flowLayout = new FlowLayout();
                
                
                int n=20;
                QVector <QPushButton *> buttons(n);
                
                for (int ii=0;ii<n;ii++)
                {
                    QPushButton * pb = new QPushButton(); // creating buttons
                
                       pb->setMinimumSize(200,200);
                       buttons.push_back(pb); // adding buttons to qvector
                
                       flowLayout->addWidget(pb);
                
                }
                flowWidget->setLayout(flowLayout);
                WindowLayout->addWidget(flowWidget);
                scroll->setWidget(flowWidget);
                this->setCentralWidget(WindowWidget);
                
                
                this->show();
                }
                

                “ In order to be irreplaceable, one must always be different” – Coco Chanel

                Pl45m4P 1 Reply Last reply
                0
                • Swati777999S Swati777999

                  @jsulm

                  I am doing part-2 for the following layout :
                  QPushButtons+scroll.PNG

                  blank display unfortunately!

                  {
                  QWidget *WindowWidget= new QWidget(this);
                  WindowWidget->setMinimumSize(2000,2000);
                  
                  QVBoxLayout *WindowLayout = new QVBoxLayout(WindowWidget);
                  
                  
                  QLabel *WindowHeading = new QLabel("Experimenting with Scrollbar in Flow Layout");
                  WindowLayout->addWidget(WindowHeading);
                  WindowLayout->setAlignment(WindowHeading,Qt::AlignCenter);
                  QScrollArea *scroll =new QScrollArea();
                  
                  QWidget *flowWidget = new QWidget();
                  flowWidget ->setMinimumSize(1200,1200);
                  FlowLayout *flowLayout = new FlowLayout();
                  
                  
                  int n=20;
                  QVector <QPushButton *> buttons(n);
                  
                  for (int ii=0;ii<n;ii++)
                  {
                      QPushButton * pb = new QPushButton(); // creating buttons
                  
                         pb->setMinimumSize(200,200);
                         buttons.push_back(pb); // adding buttons to qvector
                  
                         flowLayout->addWidget(pb);
                  
                  }
                  flowWidget->setLayout(flowLayout);
                  WindowLayout->addWidget(flowWidget);
                  scroll->setWidget(flowWidget);
                  this->setCentralWidget(WindowWidget);
                  
                  
                  this->show();
                  }
                  
                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #50

                  @Swati777999 said in Syntax for Vector of QPushbuttons added to FlowLayout:

                  blank display unfortunately!

                  Blank in which way?
                  How does your main.cpp look right now?


                  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
                  0
                  • Swati777999S Swati777999

                    @JKSH Now, I'm trying out the second part of this design

                    QPushButtons+scroll.PNG

                    You can see the bigger red-rectangle is what I was designing above. Now,I've to design the whole layout.Here's my code-

                    Window::Window()

                    {
                    QMainWindow *a= new QMainWindow(this);
                    
                    QWidget *WindowWidget= new QWidget();
                    WindowWidget->setMinimumSize(2000,2000);
                    
                    QVBoxLayout *WindowLayout = new QVBoxLayout(WindowWidget);
                    QScrollArea *scroll =new QScrollArea();
                    
                    QLabel *WindowHeading = new QLabel("Experimenting with Scrollbar in Flow Layout");
                    WindowLayout->addWidget(WindowHeading);
                    WindowLayout->setAlignment(WindowHeading,Qt::AlignCenter);
                    
                    
                    QWidget *flowWidget = new QWidget();
                    FlowLayout *flowLayout = new FlowLayout();
                    
                    int n=20;
                    QVector <QPushButton *> buttons(n);
                    
                    for (int ii=0;ii<n;ii++)
                    {
                        QPushButton * pb = new QPushButton(); // creating buttons
                    
                           pb->setMinimumSize(200,200);
                           buttons.push_back(pb); // adding buttons to qvector
                    
                           flowLayout->addWidget(pb);
                    
                    }
                    flowWidget->setLayout(flowLayout);
                    flowWidget ->setMinimumSize(1200,1200);
                    WindowLayout->addWidget(flowWidget);
                    
                    //scroll->setWidget(flowWidget);
                    
                    a->setCentralWidget(WindowWidget);
                    a->show();
                    }
                    

                    can't see it working! :'(

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by JKSH
                    #51

                    @Swati777999 said in Syntax for Vector of QPushbuttons added to FlowLayout:

                    In this case , the setminimumsize() for the widget is not used ,that's why the buttons take a vertical layout.

                    That's an incomplete answer.

                    1. You also get a "vertical layout" if you call flowWidget->setMinimumSize(100, 100). Why?
                    2. What is the minimum size that you need to get 2 buttons per row?

                    Now, I'm trying out the second part of this design

                    No, don't do this yet.

                    Before you proceed any further, simplify your design first!

                    With the objects of QMainWindow, QScrollArea, I am getting the desired result

                    You still have an extremely complicated way of getting the desired result. You need to know how to achieve your result after removing both Window and QMainWindow from your code.

                    Until you can achieve this, it is a bad idea to try out the second part of this design.

                    QPushButtons+scroll.PNG

                    Your diagram is wrong. The QScrollArea is not the orange scrollbars. The QScrollArea is the big red rectangle. Does this make sense?

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    Swati777999S 1 Reply Last reply
                    0
                    • JKSHJ JKSH

                      @Swati777999 said in Syntax for Vector of QPushbuttons added to FlowLayout:

                      In this case , the setminimumsize() for the widget is not used ,that's why the buttons take a vertical layout.

                      That's an incomplete answer.

                      1. You also get a "vertical layout" if you call flowWidget->setMinimumSize(100, 100). Why?
                      2. What is the minimum size that you need to get 2 buttons per row?

                      Now, I'm trying out the second part of this design

                      No, don't do this yet.

                      Before you proceed any further, simplify your design first!

                      With the objects of QMainWindow, QScrollArea, I am getting the desired result

                      You still have an extremely complicated way of getting the desired result. You need to know how to achieve your result after removing both Window and QMainWindow from your code.

                      Until you can achieve this, it is a bad idea to try out the second part of this design.

                      QPushButtons+scroll.PNG

                      Your diagram is wrong. The QScrollArea is not the orange scrollbars. The QScrollArea is the big red rectangle. Does this make sense?

                      Swati777999S Offline
                      Swati777999S Offline
                      Swati777999
                      wrote on last edited by
                      #52

                      @JKSH said in Syntax for Vector of QPushbuttons added to FlowLayout:

                      @Swati777999 said in Syntax for Vector of QPushbuttons added to FlowLayout:

                      In this case , the setminimumsize() for the widget is not used ,that's why the buttons take a vertical layout.

                      That's an incomplete answer.

                      1. You also get a "vertical layout" if you call flowWidget->setMinimumSize(100, 100). Why?
                      2. What is the minimum size that you need to get 2 buttons per row?

                      Now, I'm trying out the second part of this design

                      No, don't do this yet.

                      Here's the simplified code for the entire design . I created a new project scrollAreaApp with the base class as QMainWindow
                      scrollAreaApp.cpp

                      #include "scrollareaapp.h"
                      
                      ScrollAreaApp::ScrollAreaApp(QWidget *parent)
                          : QMainWindow(parent)
                      {
                          QWidget *flowWidget = new QWidget();
                          FlowLayout *flowLayout = new FlowLayout();
                          flowWidget->setLayout(flowLayout);
                          flowWidget ->setMinimumSize(1200,1200);
                          int n=20;
                          QVector <QPushButton *> buttons(n);
                      
                          for (int ii=0;ii<n;ii++)
                          {
                              QPushButton * pb = new QPushButton(); // creating buttons
                      
                                 pb->setMinimumSize(200,200);
                                 buttons.push_back(pb); // adding buttons to qvector
                      
                                 flowLayout->addWidget(pb);
                      
                          }
                      
                          QWidget *WindowWidget= new QWidget(this);
                          WindowWidget->setMinimumSize(1000,1000);
                      
                          QVBoxLayout *WindowLayout = new QVBoxLayout(WindowWidget);
                      
                          QLabel *WindowHeading = new QLabel("Experimenting with Scrollbar in Flow Layout");
                      
                          WindowLayout->addWidget(WindowHeading);
                          WindowLayout->setAlignment(WindowHeading,Qt::AlignLeft);
                          WindowLayout->addWidget(flowWidget);
                          
                          QScrollArea *scroll =new QScrollArea();
                          scroll->setWidget(WindowWidget);
                          this->setCentralWidget(scroll);
                          this->show();
                      } 
                      

                      scrollAreaApp-21.12-1.PNG

                      I am getting the desired result.

                      “ In order to be irreplaceable, one must always be different” – Coco Chanel

                      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