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

    @Pl45m4
    The content (= centralwidget) of your mainWindow should be your scrollArea then. And inside this scrollArea you place the widget with your buttons.
    Okay, Let me write the whole function according to your suggestion.

    Window::Window()
    {
    QMainWindow *a= new QMainWindow(this);
    
    QWidget *flowWidget = new QWidget();
    FlowLayout *flowLayout = new FlowLayout();
    flowWidget->setLayout(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);
    
    }
    
    QScrollArea *scroll =new QScrollArea();
    scroll->setWidget(flowWidget);
    
    a->setCentralWidget(scroll);
    
    
    a->show();
    
    setWindowTitle(tr("Flow Layout"));
    }
    

    I obviously,get 2 windows from the above code, Window1 corresponding to this by default window which is blank.
    Window2 is as shown below
    Program-2-17.12_window2.PNG

    scroll Area is applied to the window but the flowLayout seems not to be working and the widgets take vertical Layout. Can you help me to fix this?

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

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

    scroll Area is applied to the window but the flowLayout seems not to be working and the widgets take vertical Layout. Can you help me to fix this?

    Your flowWidget is too narrow -- that's why the flow layout can't put buttons side-by-side. Give it a larger minimum width.

    Remember: The width of the QScrollArea is not the same as the width of the widget it holds

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

    Swati777999S 1 Reply Last reply
    1
    • Pl45m4P Pl45m4

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

      Window::Window()
      {
      QMainWindow *a= new QMainWindow(this);

      QWidget *flowWidget = new QWidget();
      FlowLayout *flowLayout = new FlowLayout();
      flowWidget->setLayout(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);
      

      }

      QScrollArea *scroll =new QScrollArea();
      scroll->setWidget(flowWidget);

      a->setCentralWidget(scroll);

      a->show();

      That's not what I suggested....

      The window with the title flowLayout IS your MainWindow, right?! Why you created another one ?

      I obviously,get 2 windows from the above code, Window1 corresponding to this by default window which is blank.

      Yes, not very surprising from above code...

      Window::Window()

      What is this class? Your first mainWindow? Then you dont need to create another QMainWindow *a= new QMainWindow(this);
      inside this class. Remove it and call this->setCentralWidget(....) instead (assuming Window is a QMainWindow class).

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

      @Pl45m4 said in Syntax for Vector of QPushbuttons added to FlowLayout:
      Actually, I'm using the example file of flowLayout given in the section of Examples in Qt Creator where class Window used [subclass of QWidget].

      The window with the title flowLayout IS your MainWindow, right?! Why you created another one ?
      As you can see, there are two windows with names as flowLayout and FlowLayout . My mainwindow should be Flow Layout for which I wrote the following code:
      I needed to add scroll functionality to the following code.

          #include<QScrollArea>	
          Window::Window
      {
          FlowLayout *flowLayout = new FlowLayout(this);
          int n=20;
          QVector <QPushButton *> buttons(n);
      
          for (int ii=0;ii<n;ii++)
          {
              QPushButton * pb = new QPushButton(this); // creating buttons
              
                 pb->setMinimumSize(200,200);
                 buttons.push_back(pb); // adding buttons to qvector
      
                 flowLayout->addWidget(pb);
            }
      this ->show();
      }
      
      

      What is this class? Your first mainWindow? Then you dont need to create another QMainWindow *a= new QMainWindow(this);
      inside this class. Remove it and call this->setCentralWidget(....) instead (assuming Window is a QMainWindow class).

      I used QMainWindow *a= new QMainWindow(this); because it seemed to be working partially.

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

      1 Reply Last reply
      0
      • Pl45m4P Pl45m4

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

        Window::Window()
        {
        QMainWindow *a= new QMainWindow(this);

        QWidget *flowWidget = new QWidget();
        FlowLayout *flowLayout = new FlowLayout();
        flowWidget->setLayout(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);
        

        }

        QScrollArea *scroll =new QScrollArea();
        scroll->setWidget(flowWidget);

        a->setCentralWidget(scroll);

        a->show();

        That's not what I suggested....

        The window with the title flowLayout IS your MainWindow, right?! Why you created another one ?

        I obviously,get 2 windows from the above code, Window1 corresponding to this by default window which is blank.

        Yes, not very surprising from above code...

        Window::Window()

        What is this class? Your first mainWindow? Then you dont need to create another QMainWindow *a= new QMainWindow(this);
        inside this class. Remove it and call this->setCentralWidget(....) instead (assuming Window is a QMainWindow class).

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

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

        inside this class. Remove it and call this->setCentralWidget(....) instead (assuming Window is a QMainWindow class).

        this->setCentralWidget(....) this gives me error as this is an object of Window class which is a subclass of QWidget.
        setCentralWidget(.....) can be applied to the object of QMainWindow.

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

        1 Reply Last reply
        0
        • JKSHJ JKSH

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

          scroll Area is applied to the window but the flowLayout seems not to be working and the widgets take vertical Layout. Can you help me to fix this?

          Your flowWidget is too narrow -- that's why the flow layout can't put buttons side-by-side. Give it a larger minimum width.

          Remember: The width of the QScrollArea is not the same as the width of the widget it holds

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

          @JKSH said in Syntax for Vector of QPushbuttons added to FlowLayout:
          Your flowWidget is too narrow -- that's why the flow layout can't put buttons side-by-side. Give it a larger minimum width.
          Remember: The width of the QScrollArea is not the same as the width of the widget it holds

          This works like a charm! Thanks a ton! I was thinking it to be the fault of my design and made it too complicated.

          Just a doubt about the number of buttons appearing/row.

          Window::Window
          {
          QMainWindow *a= new QMainWindow(this);
          
          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);
          }
          
          QScrollArea *scroll =new QScrollArea();
          scroll->setWidget(flowWidget);
          
          a->setCentralWidget(scroll);
          a->show();
          }
          
          

          This is the result I get in flowLayout window.
          Program-1-20.12.PNG

          From the figure, I get 5 buttons per row but as per the following lines , shouldn't I be getting 6 buttons/row ( 1200/200=6)

          flowWidget ->setMinimumSize(1200,1200); // size of widget
          pb->setMinimumSize(200,200); //size of buttons
          
          

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

          Christian EhrlicherC 1 Reply Last reply
          0
          • Swati777999S Swati777999

            @JKSH said in Syntax for Vector of QPushbuttons added to FlowLayout:
            Your flowWidget is too narrow -- that's why the flow layout can't put buttons side-by-side. Give it a larger minimum width.
            Remember: The width of the QScrollArea is not the same as the width of the widget it holds

            This works like a charm! Thanks a ton! I was thinking it to be the fault of my design and made it too complicated.

            Just a doubt about the number of buttons appearing/row.

            Window::Window
            {
            QMainWindow *a= new QMainWindow(this);
            
            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);
            }
            
            QScrollArea *scroll =new QScrollArea();
            scroll->setWidget(flowWidget);
            
            a->setCentralWidget(scroll);
            a->show();
            }
            
            

            This is the result I get in flowLayout window.
            Program-1-20.12.PNG

            From the figure, I get 5 buttons per row but as per the following lines , shouldn't I be getting 6 buttons/row ( 1200/200=6)

            flowWidget ->setMinimumSize(1200,1200); // size of widget
            pb->setMinimumSize(200,200); //size of buttons
            
            
            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #35

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

            From the figure, I get 5 buttons per row but as per the following lines , shouldn't I be getting 6 buttons/row ( 1200/200=6)

            And you see that the widgets have some margins between them? So why should 6 widgets with 200x each + margins fit into 1200px??

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            Swati777999S 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

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

              From the figure, I get 5 buttons per row but as per the following lines , shouldn't I be getting 6 buttons/row ( 1200/200=6)

              And you see that the widgets have some margins between them? So why should 6 widgets with 200x each + margins fit into 1200px??

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

              @Christian-Ehrlicher That's right but by intuition/observation, I can say that, in the above figure, one more button can easily fit in. What do you think?

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

              JKSHJ 1 Reply Last reply
              0
              • Swati777999S Swati777999

                @Christian-Ehrlicher That's right but by intuition/observation, I can say that, in the above figure, one more button can easily fit in. What do you think?

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

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

                by intuition/observation, I can say that, in the above figure, one more button can easily fit in.

                Apply the same intuition to your previous case:

                Then, apply the same explanation that I gave you in my previous post.

                Then, explain to us what's happening.

                This works like a charm! Thanks a ton! I was thinking it to be the fault of my design and made it too complicated.

                Great!

                Complicated design does not cause code to stop working. However, it does make life very difficult for you.

                Window::Window
                {
                QMainWindow *a= new QMainWindow(this);
                

                You are making your design extremely complicated again... You don't need a Window plus a QMainWindow plus a QScrollArea.

                Before you proceed any further, simplify your design first!

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

                Swati777999S 2 Replies Last reply
                3
                • JKSHJ JKSH

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

                  by intuition/observation, I can say that, in the above figure, one more button can easily fit in.

                  Apply the same intuition to your previous case:

                  Then, apply the same explanation that I gave you in my previous post.

                  Then, explain to us what's happening.

                  This works like a charm! Thanks a ton! I was thinking it to be the fault of my design and made it too complicated.

                  Great!

                  Complicated design does not cause code to stop working. However, it does make life very difficult for you.

                  Window::Window
                  {
                  QMainWindow *a= new QMainWindow(this);
                  

                  You are making your design extremely complicated again... You don't need a Window plus a QMainWindow plus a QScrollArea.

                  Before you proceed any further, simplify your design first!

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

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

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

                  by intuition/observation, I can say that, in the above figure, one more button can easily fit in.

                  Apply the same intuition to your previous case:

                  Then, apply the same explanation that I gave you in my previous post.
                  Then, explain to us what's happening.

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

                  You are making your design extremely complicated again... You don't need a Window plus a QMainWindow plus a QScrollArea.Before you proceed any further, simplify your design first!

                  With the objects of QMainWindow,QScrollArea , I am getting the desired result as I have suppressed /commented window.show(); in main.cpp
                  Program-1-20.12.PNG

                  Follow main.cpp below:

                  #include <QApplication>
                  
                  #include "window.h"
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication app(argc, argv);
                      Window window;
                  #if defined(Q_OS_SYMBIAN)
                      window.showMaximized();
                  #else
                      //window.show();
                  #endif
                      return app.exec();
                  }
                  

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

                  jsulmJ 1 Reply Last reply
                  0
                  • Swati777999S Swati777999

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

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

                    by intuition/observation, I can say that, in the above figure, one more button can easily fit in.

                    Apply the same intuition to your previous case:

                    Then, apply the same explanation that I gave you in my previous post.
                    Then, explain to us what's happening.

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

                    You are making your design extremely complicated again... You don't need a Window plus a QMainWindow plus a QScrollArea.Before you proceed any further, simplify your design first!

                    With the objects of QMainWindow,QScrollArea , I am getting the desired result as I have suppressed /commented window.show(); in main.cpp
                    Program-1-20.12.PNG

                    Follow main.cpp below:

                    #include <QApplication>
                    
                    #include "window.h"
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication app(argc, argv);
                        Window window;
                    #if defined(Q_OS_SYMBIAN)
                        window.showMaximized();
                    #else
                        //window.show();
                    #endif
                        return app.exec();
                    }
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #39

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

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

                    Swati777999S 1 Reply Last reply
                    1
                    • JKSHJ JKSH

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

                      by intuition/observation, I can say that, in the above figure, one more button can easily fit in.

                      Apply the same intuition to your previous case:

                      Then, apply the same explanation that I gave you in my previous post.

                      Then, explain to us what's happening.

                      This works like a charm! Thanks a ton! I was thinking it to be the fault of my design and made it too complicated.

                      Great!

                      Complicated design does not cause code to stop working. However, it does make life very difficult for you.

                      Window::Window
                      {
                      QMainWindow *a= new QMainWindow(this);
                      

                      You are making your design extremely complicated again... You don't need a Window plus a QMainWindow plus a QScrollArea.

                      Before you proceed any further, simplify your design first!

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

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

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

                      JKSHJ 1 Reply Last reply
                      0
                      • jsulmJ jsulm

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

                        Swati777999S Offline
                        Swati777999S Offline
                        Swati777999
                        wrote on 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

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

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 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

                          Swati777999S 1 Reply Last reply
                          4
                          • 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

                                          • Login

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