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

Syntax for Vector of QPushbuttons added to FlowLayout

Scheduled Pinned Locked Moved Solved General and Desktop
52 Posts 6 Posters 4.6k Views
  • 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.
  • J jsulm
    20 Dec 2021, 08:28

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

    I am getting the desired result as I have suppressed /commented window.show(); in main.cpp

    Because your Window creates and shows a new QMainWindow! Why?!
    I think you was already told several times that there is no need to create two windows.
    Why don't you remove

    QMainWindow *a= new QMainWindow(this);
    

    and show Window?

    S Offline
    S Offline
    Swati777999
    wrote on 20 Dec 2021, 08:34 last edited by
    #41

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

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

    I am getting the desired result as I have suppressed /commented window.show(); in main.cpp

    Because your Window creates and shows a new QMainWindow! Why?!
    I think you was already told several times that there is no need to create two windows.

    Why don't you remove
    QMainWindow *a= new QMainWindow(this); ?
    As already mentioned before, Window class is a sub-class of QWidget ,for setting scrolling option , I need the object of QMainWindow , that's the reason why I created the object a of QMainWindow.

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

    J 1 Reply Last reply 20 Dec 2021, 08:35
    0
    • S Swati777999
      20 Dec 2021, 08:34

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

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

      I am getting the desired result as I have suppressed /commented window.show(); in main.cpp

      Because your Window creates and shows a new QMainWindow! Why?!
      I think you was already told several times that there is no need to create two windows.

      Why don't you remove
      QMainWindow *a= new QMainWindow(this); ?
      As already mentioned before, Window class is a sub-class of QWidget ,for setting scrolling option , I need the object of QMainWindow , that's the reason why I created the object a of QMainWindow.

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 20 Dec 2021, 08:35 last edited by
      #42

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

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

      S 1 Reply Last reply 20 Dec 2021, 09:14
      4
      • J jsulm
        20 Dec 2021, 08:35

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

        S Offline
        S Offline
        Swati777999
        wrote on 20 Dec 2021, 09:14 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

        J 1 Reply Last reply 20 Dec 2021, 09:18
        0
        • S Swati777999
          20 Dec 2021, 09:14

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

          J Online
          J Online
          jsulm
          Lifetime Qt Champion
          wrote on 20 Dec 2021, 09:18 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

          S 1 Reply Last reply 20 Dec 2021, 09:25
          1
          • J jsulm
            20 Dec 2021, 09:18

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

            S Offline
            S Offline
            Swati777999
            wrote on 20 Dec 2021, 09:25 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

            J 1 Reply Last reply 20 Dec 2021, 09:26
            0
            • S Swati777999
              20 Dec 2021, 09:25

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

              J Online
              J Online
              jsulm
              Lifetime Qt Champion
              wrote on 20 Dec 2021, 09:26 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

              S 3 Replies Last reply 20 Dec 2021, 09:29
              1
              • J jsulm
                20 Dec 2021, 09:26

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

                S Offline
                S Offline
                Swati777999
                wrote on 20 Dec 2021, 09:29 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
                • J jsulm
                  20 Dec 2021, 09:26

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

                  S Offline
                  S Offline
                  Swati777999
                  wrote on 20 Dec 2021, 09:44 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
                  • J jsulm
                    20 Dec 2021, 09:26

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

                    S Offline
                    S Offline
                    Swati777999
                    wrote on 20 Dec 2021, 10:01 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

                    P 1 Reply Last reply 20 Dec 2021, 10:37
                    0
                    • S Swati777999
                      20 Dec 2021, 10:01

                      @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();
                      }
                      
                      P Online
                      P Online
                      Pl45m4
                      wrote on 20 Dec 2021, 10:37 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
                      • S Swati777999
                        20 Dec 2021, 08:30

                        @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! :'(

                        J Online
                        J Online
                        JKSH
                        Moderators
                        wrote on 20 Dec 2021, 12:20 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

                        S 1 Reply Last reply 21 Dec 2021, 02:18
                        0
                        • J JKSH
                          20 Dec 2021, 12:20

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

                          S Offline
                          S Offline
                          Swati777999
                          wrote on 21 Dec 2021, 02:18 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

                          50/52

                          20 Dec 2021, 10:37

                          • Login

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