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...
@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 ofQtCreator
. That's why doing in this way. -
@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 ofQtCreator
. That's why doing in this way.@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. -
@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.@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.
-
@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.
@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. -
@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.@jsulm got it. Let me try.
-
@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.@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.
-
@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.I am doing part-2 for the following layout :
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(); }
-
I am doing part-2 for the following layout :
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(); }
@Swati777999 said in Syntax for Vector of QPushbuttons added to FlowLayout:
blank display unfortunately!
Blank in which way?
How does yourmain.cpp
look right now? -
@JKSH Now, I'm trying out the second part of this design
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! :'(
@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.
- You also get a "vertical layout" if you call
flowWidget->setMinimumSize(100, 100)
. Why? - 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 resultYou still have an extremely complicated way of getting the desired result. You need to know how to achieve your result after removing both
Window
andQMainWindow
from your code.Until you can achieve this, it is a bad idea to try out the second part of this design.
Your diagram is wrong. The QScrollArea is not the orange scrollbars. The QScrollArea is the big red rectangle. Does this make sense?
- You also get a "vertical layout" if you call
-
@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.
- You also get a "vertical layout" if you call
flowWidget->setMinimumSize(100, 100)
. Why? - 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 resultYou still have an extremely complicated way of getting the desired result. You need to know how to achieve your result after removing both
Window
andQMainWindow
from your code.Until you can achieve this, it is a bad idea to try out the second part of this design.
Your diagram is wrong. The QScrollArea is not the orange scrollbars. The QScrollArea is the big red rectangle. Does this make sense?
@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.
- You also get a "vertical layout" if you call
flowWidget->setMinimumSize(100, 100)
. Why? - 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 asQMainWindow
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(); }
I am getting the desired result.
- You also get a "vertical layout" if you call