showing custom widget inside the mainWindow
-
Hi All,
I have a custom widget of QWidget type and I want to show it inside the main window. How can I do that?
When I run the code, I get two windows; a blank window MainWindow whereas the widget is shown in another window.
How can I get the contents of Widgets displayed on the blank MainWindow?Please suggest me some ways to achieve that.
-
@Swati777999 said in showing custom widget inside the mainWindow:
I have a custom widget of QWidget type and I want to show it inside the main window. How can I do that?
You already did that at https://forum.qt.io/topic/132782/syntax-for-vector-of-qpushbuttons-added-to-flowlayout with your
ScrollAreaApp
, right? -
@Swati777999 said in showing custom widget inside the mainWindow:
When I run the code, I get two windows; a blank window MainWindow whereas the widget is shown in another window.
Apart from what @JKSH says. If this is the case you are not setting the parent of your widget to the main window. You need that for it to be a child widget shown on the parent; a widget with no parent is shown as its own (top-level) window. The main window has no parent, that is why it is shown as its own window.
-
@JKSH said in showing custom widget inside the mainWindow:
@Swati777999 said in showing custom widget inside the mainWindow:
I have a custom widget of QWidget type and I want to show it inside the main window. How can I do that?
You already did that at https://forum.qt.io/topic/132782/syntax-for-vector-of-qpushbuttons-added-to-flowlayout with your
ScrollAreaApp
, right?Yes, I had done the first part of it but my whole design should be a
WIDGET
with-
a heading [
QLabel
] -
an array[
QVector
] of buttons [QPushButtons
] in a flow layout -
above to items arranged in a
QVBoxLayout
-
scrolling feature to the window.
I've achieved this design but I created it in a class which is a subclass of
QMainWindow
whereas I now realise I've to use this design as aQWidget
. -
-
@Swati777999 said in showing custom widget inside the mainWindow:
which is a subclass of QMainWindow whereas I now realise I've to use this design as a QWidget
Then subclass QWidget instead of QMainWindow...
-
@Swati777999 You need to create a layout to store your custom widget. The layout will belong to the main window, and the custom widget will belong to the layout.
In order to do this in C++, it would look like this:
in a method of your main window:
CustomWidget *widget = new CustomWidget(...);/ // i don't know your custom widget constructor QVBoxLayout *layout = new QVBoxLayout; //vbox is example; you can use hbox, form, grid, etc. . layout->addWidget(widget); this->setLayout(layout);
This will show your widget as a child of the main window.
From the designer, you have to drop a plain QWidget where you want your custom widget to reside in your main window UI file.
Then, you have to promote your custom widget via. right click, setting the header file & class name parameters.
Then you can use it as if it were any other widget in the designer, given it's properly promoted -
@jsulm said in showing custom widget inside the mainWindow:
@Swati777999 said in showing custom widget inside the mainWindow:
which is a subclass of QMainWindow whereas I now realise I've to use this design as a QWidget
Then subclass QWidget instead of QMainWindow...
Yes, that's where I am stuck . Refer to the last message of this post - Vector of QPushbuttons added to a flowlayout
In this I get 2 windows, one blank window (probably default window of the widget ) and other one is the Main Window.
#include "scrollareaapp.h" ScrollAreaApp::ScrollAreaApp(QWidget *parent) : QWidget(parent) { QMainWindow *main = new QMainWindow(); QWidget *flowWidget = new QWidget(main); 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(); 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); scroll->setWidget(WindowWidget); main->setLayout(WindowLayout); main->setCentralWidget(scroll); main->show(); main->setWindowTitle("Main Window"); }
Can someone tell me how to get a single window(hide widget's plain window, in this case)?
-
Already answered here: https://forum.qt.io/topic/132782/syntax-for-vector-of-qpushbuttons-added-to-flowlayout/47
-
@Swati777999 said in showing custom widget inside the mainWindow:
Yes, that's where I am stuck
In what way stuck?
I'm really wondering how often we have to repeat same stuff:- Do not create a new QMainWindow in your ScrollAreaApp
- Subclass QMainWindow instead of QWidget
I give up...
-
@Swati777999 said in showing custom widget inside the mainWindow:
In this I get 2 windows, one blank window (probably default window of the widget ) and other one is the Main Window.
You said you got your desired results at https://forum.qt.io/post/695547
Now, your new code doesn't have the desired results. So, tell us: What changes did you make?
-
@JKSH said in showing custom widget inside the mainWindow:
@Swati777999 said in showing custom widget inside the mainWindow:
In this I get 2 windows, one blank window (probably default window of the widget ) and other one is the Main Window.
You said you got your desired results at https://forum.qt.io/post/695547
Now, your new code doesn't have the desired results. So, tell us: What changes did you make?
Thanks for the patience @JKSH. I appreciate that.
Actually ,the entire widget [window with a heading and array of buttons in a vertical layout with scrolling feature]that you see, is a part of another application which is to be imported as a widget.
Now , I'm trying to do something like this :
scrollareaapp.h
class ScrollAreaApp : public QWidget { Q_OBJECT public: ScrollAreaApp(QWidget *parent = nullptr); ~ScrollAreaApp(); };
scrollareaapp.cpp
ScrollAreaApp::ScrollAreaApp(QWidget *parent) : QWidget(parent) { QMainWindow *main = new QMainWindow(); QWidget *flowWidget = new QWidget(main); 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(); 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); scroll->setWidget(WindowWidget); main->setLayout(WindowLayout); main->setCentralWidget(scroll); main->show(); main->setWindowTitle("Main Window"); }
So this entire widget is to be imported as a widget which fits into the centralWidget of another application.
I hope I made my point clearly. -
@Swati777999 said in showing custom widget inside the mainWindow:
So this entire widget is to be imported as a widget which fits into the centralWidget of another application
Why you still create a
QMainWindow
inside yourQWidget
? You always add one layer which is not necessary.
i believe the task as such is not complicated but the amount of cascading widgets and layouts makes it harder to read.If your widget needs to be part of another (already existing) widget or QMainWindow, then you dont need so many widgets and layouts in your widget.
Create a widget, assign a main layout, put other content and maybe other layouts with content in, done.
Not every app needs aQMainWindow
. It's recommended for stand-alone apps, becauseQMainWindow
provides some nice features like for example the integrated statusBar, but in the end,QMainWindow
is just a regularQWidget
.
So it is weird to create aQWidget
, then create a parentlessQMainWindow
inside and then show it.Edit:
Also, AFAICS yourmain
mainWindow "lives" in your widget's constructor only... It will get destroyed if you close your other widget, no matter if the mainWindow is still visible or not. -
@Pl45m4 said in showing custom widget inside the mainWindow:
@Swati777999 said in showing custom widget inside the mainWindow:
So this entire widget is to be imported as a widget which fits into the centralWidget of another application
Why you still create a
QMainWindow
inside yourQWidget
? You always add one layer which is not necessary.
i believe the task as such is not complicated but the amount of cascading widgets and layouts makes it harder to read.If you widgets needs to be part of another (already existing) widget or QMainWindow, then you dont need so many widgets and layouts in your widget.
Create a widget, assign a main layout, put other content and maybe other layouts with content in, done.
Not every app needs aQMainWindow
. It's recommended for stand-alone apps, becauseQMainWindow
provides some nice features like for example the integrated statusBar, but in the end,QMainWindow
is just a regularQWidget
.
So it is weird to create aQWidget
, then create a parentlessQMainWindow
inside and then show it.Edit:
Also, AFAICS yourmain
mainWindow "lives" in your widget's constructor only... It will get destroyed if you close your other widget, no matter if the mainWindow is still visible or not.My point is there's another application(let's call it MyApp) which has got a design.
My app has got some pushbuttons on the left side of the mainwindow. On clicking pushbutton A , I should be getting the result of the scrollareaApp in the central widget of MyApp.What you see below is MyApp and I've written the program above for scrollAreaapp
-
@Swati777999 said in showing custom widget inside the mainWindow:
My point is there's another application(let's call it MyApp) which has got a design.
I believe you have missed @Pl45m4's point. He is asking you to explain: What do you expect this line to do?:
QMainWindow *main = new QMainWindow();
So this entire widget is to be imported as a widget which fits into the centralWidget of another application.
Well, your
ScrollAreaApp
should not be the one that tries to import itself to the other application.- You should put code inside the
ScrollAreaApp
that determines how it looks, all by itself. - Your other application should contain the code that "fits [the ScrollAreaApp] to the other application".
- You should put code inside the
-
@JKSH said in showing custom widget inside the mainWindow:
I believe you have missed @Pl45m4's point. He is asking you to explain: What do you expect this line to do?:QMainWindow *main = new QMainWindow();
Actually, I was trying to write some codes after that [like setting a layout or so]so as to I can addWindowWidget
to it, something like that.Well, your
ScrollAreaApp
should not be the one that tries to import itself to the other application.- You should put code inside the
ScrollAreaApp
that determines how it looks, all by itself.
2. Your other application should contain the code that "fits [the ScrollAreaApp] to the other application".
Yes, my other application has code for it and I should be writing this code there while trying but I chose to create another project file for experimenting. - You should put code inside the
-
@Swati777999 said in showing custom widget inside the mainWindow:
@JKSH said in showing custom widget inside the mainWindow:
I believe you have missed @Pl45m4's point. He is asking you to explain: What do you expect this line to do?:QMainWindow *main = new QMainWindow();
Actually, I was trying to write some codes after that [like setting a layout or so]so as to I can addWindowWidget
to it, something like that.You should add your WindowWidget to your ScrollAreaApp (which is
this
in your ScrollAreaApp's constructor). You should not be adding it to some extra QMainWindow.Yes, my other application has code for it and I should be writing this code there while trying but I chose to create another project file for experimenting.
Unfortunately, the way that you're carrying out these experiments is making it harder for you to understand the important basics of widget parent-child relationships and layout management.
In summary: It is completely wrong to create a
new QMainWindow()
in your custom widget's constructor. Don't do it, ever... not even for experimenting. This is because the ScrollAreaApp's constructor should only contain things that are placed inside the ScrollAreaApp. (Does this make sense? If not, please ask) -
@JKSH said in showing custom widget inside the mainWindow:
@Swati777999 said in showing custom widget inside the mainWindow:
@JKSH said in showing custom widget inside the mainWindow:
I believe you have missed @Pl45m4's point. He is asking you to explain: What do you expect this line to do?:QMainWindow *main = new QMainWindow();
Actually, I was trying to write some codes after that [like setting a layout or so]so as to I can addWindowWidget
to it, something like that.You should add your WindowWidget to your ScrollAreaApp (which is
this
in your ScrollAreaApp's constructor). You should not be adding it to some extra QMainWindow.Yes, my other application has code for it and I should be writing this code there while trying but I chose to create another project file for experimenting.
Unfortunately, the way that you're carrying out these experiments is making it harder for you to understand the important basics of widget parent-child relationships and layout management.
In summary: It is completely wrong to create a
new QMainWindow()
in your custom widget's constructor. Don't do it, ever... not even for experimenting. This is because the ScrollAreaApp's constructor should only contain things that are placed inside the ScrollAreaApp. (Does this make sense? If not, please ask)I completely understand what you want to convey. So, basically it is preferred to experiment in the same location of the complete application rather than writing a part of code in different project file.