Syntax for Vector of QPushbuttons added to FlowLayout
-
@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 buttonspb->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 yourMainWindow
, 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 anotherQMainWindow *a= new QMainWindow(this);
inside this class. Remove it and callthis->setCentralWidget(....)
instead (assumingWindow
is aQMainWindow
class).@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 classWindow
used [subclass of QWidget].The window with the title
flowLayout
IS yourMainWindow
, right?! Why you created another one ?
As you can see, there are two windows with names asflowLayout
andFlowLayout
. My mainwindow should beFlow 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 anotherQMainWindow *a= new QMainWindow(this);
inside this class. Remove it and callthis->setCentralWidget(....)
instead (assumingWindow
is aQMainWindow
class).
I usedQMainWindow *a= new QMainWindow(this);
because it seemed to be working partially. -
@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 buttonspb->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 yourMainWindow
, 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 anotherQMainWindow *a= new QMainWindow(this);
inside this class. Remove it and callthis->setCentralWidget(....)
instead (assumingWindow
is aQMainWindow
class).@Pl45m4 said in Syntax for Vector of QPushbuttons added to FlowLayout:
inside this class. Remove it and call
this->setCentralWidget(....)
instead (assumingWindow
is aQMainWindow
class).this->setCentralWidget(....)
this gives me error asthis
is an object ofWindow
class which is a subclass ofQWidget
.
setCentralWidget(.....)
can be applied to the object ofQMainWindow
. -
@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
@JKSH said in Syntax for Vector of QPushbuttons added to FlowLayout:
YourflowWidget
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 holdsThis 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.
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
-
@JKSH said in Syntax for Vector of QPushbuttons added to FlowLayout:
YourflowWidget
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 holdsThis 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.
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
@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??
-
@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??
@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?
-
@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?
@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 aQMainWindow
plus aQScrollArea
.Before you proceed any further, simplify your design first!
-
@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 aQMainWindow
plus aQScrollArea
.Before you proceed any further, simplify your design first!
@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 aQMainWindow
plus aQScrollArea
.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 /commentedwindow.show();
inmain.cpp
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(); }
-
@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 aQMainWindow
plus aQScrollArea
.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 /commentedwindow.show();
inmain.cpp
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(); }
@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 removeQMainWindow *a= new QMainWindow(this);
and show Window?
-
@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 aQMainWindow
plus aQScrollArea
.Before you proceed any further, simplify your design first!
@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:
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 removeQMainWindow *a= new QMainWindow(this);
and show Window?
@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 ofQWidget
,for setting scrolling option , I need the object ofQMainWindow
, that's the reason why I created the objecta
ofQMainWindow
. -
@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 ofQWidget
,for setting scrolling option , I need the object ofQMainWindow
, that's the reason why I created the objecta
ofQMainWindow
.@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...
-
@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