CentralWidget hide & show issue
-
Hi all, during my development I find out an interesting scenario. Please see below code snippet for details. The custom widget contains docks on Top and Bottom.
1.
parent->setCentralWidget(Custome Widget);
Custome Widget->show();
parent->centralWidget()->hide();
2.
parent->setCentralWidget(Custome Widget);
parent->centralWidget()->hide();
Custome Widget->show();The first one gives me the result I want(top and bottom dock will fill the entire screen), but the second one won't hide centralWidget area( I will see the centralwidget as blank space between top and bottom docks)
Thanks~
-
Hi all, during my development I find out an interesting scenario. Please see below code snippet for details. The custom widget contains docks on Top and Bottom.
1.
parent->setCentralWidget(Custome Widget);
Custome Widget->show();
parent->centralWidget()->hide();
2.
parent->setCentralWidget(Custome Widget);
parent->centralWidget()->hide();
Custome Widget->show();The first one gives me the result I want(top and bottom dock will fill the entire screen), but the second one won't hide centralWidget area( I will see the centralwidget as blank space between top and bottom docks)
Thanks~
-
@Dev-G
The first one ends by hiding the custom/central widget while the second one ends up showing it. Why do you think it should hide the widget? So what is unexpected?@JonB Hi, my doubt is why the order matters? From my perspective, show is showing widget(docks for my case) and hide is about the centralwidget and centralwidget only which shouldn't impact my widget? Why the show for my widget will impact centralwidget, I expect it remain hidden for my second case.
-
@JonB Hi, my doubt is why the order matters? From my perspective, show is showing widget(docks for my case) and hide is about the centralwidget and centralwidget only which shouldn't impact my widget? Why the show for my widget will impact centralwidget, I expect it remain hidden for my second case.
@Dev-G
I think you do not understand something. If you put inQ_ASSERT(parent->centralWidget() == CustomeWidget);
you will find they are the same thing --- the widget you set via
parent->setCentralWidget(CustomeWidget);
. You seem to think they are different objects, but they are not.It does not matter which one you address in code, they are the same widget. So if the last thing is
hide()
, on either one, it ends up being hidden. And reverse ifshow()
. -
@Dev-G
I think you do not understand something. If you put inQ_ASSERT(parent->centralWidget() == CustomeWidget);
you will find they are the same thing --- the widget you set via
parent->setCentralWidget(CustomeWidget);
. You seem to think they are different objects, but they are not.It does not matter which one you address in code, they are the same widget. So if the last thing is
hide()
, on either one, it ends up being hidden. And reverse ifshow()
.@JonB You explanation make sense, but I don't think it resolved my doubt, maybe below graph will help. The top chart corresponding to first piece of code, the bottom chart corresponding to second piece of code. Why second part of code won't hide the "central widget" part?
-
@JonB You explanation make sense, but I don't think it resolved my doubt, maybe below graph will help. The top chart corresponding to first piece of code, the bottom chart corresponding to second piece of code. Why second part of code won't hide the "central widget" part?
@Dev-G said in CentralWidget hide & show issue:
@JonB You explanation make sense, but I don't think it resolved my doubt, maybe below graph will help. The top chart corresponding to first piece of code, the bottom chart corresponding to second piece of code. Why second part of code won't hide the "central widget" part?
The central widget in second chart I guess refereeing to the central widget in .ui file.
I have a .ui file which already created a central widget for me. Based on my understanding, the first piece of code will hide central widget in the .ui file, but the second piece of code won't hide that part.
-
@Dev-G said in CentralWidget hide & show issue:
@JonB You explanation make sense, but I don't think it resolved my doubt, maybe below graph will help. The top chart corresponding to first piece of code, the bottom chart corresponding to second piece of code. Why second part of code won't hide the "central widget" part?
The central widget in second chart I guess refereeing to the central widget in .ui file.
I have a .ui file which already created a central widget for me. Based on my understanding, the first piece of code will hide central widget in the .ui file, but the second piece of code won't hide that part.
@Dev-G said in CentralWidget hide & show issue:
I have a .ui file which already created a central widget for me.
But you replace that (your design-time one is thrown away) in both cases when you go
parent->setCentralWidget(Custome Widget);
....To visualize the layout of a
QMainWindow
see the diagram in https://doc.qt.io/qt-6/qmainwindow.html#details. -
Hi,
As @JonB rightly explained, the order matters. You are accessing the same widget in both cases.
One thing that looks unclean though is your use of
parent
, it seems you are writing code somewhere you shouldn't.CustomWidget::CustomWidget(Ui::MainWindow *uiMain, QMainWindow *parent)
: mUIMain(uiMain)
{
QDockWidget dock1 = new QDockWidget(parent);
dock1->setAllowedAreas(Qt::AllDockWidgetAreas);
parent->addDockWidget(Qt::TopDockWidgetArea, dock1);QDockWidget dock2 = new QDockWidget(parent); dock2->setAllowedAreas(Qt::AllDockWidgetAreas); parent->addDockWidget(Qt::TopDockWidgetArea, dock2); QDockWidget dock3 = new QDockWidget(parent); dock3->setAllowedAreas(Qt::AllDockWidgetAreas); parent->addDockWidget(Qt::BottomDockWidgetArea, dock3); QDockWidget dock4 = new QDockWidget(parent); dock4->setAllowedAreas(Qt::AllDockWidgetAreas); parent->addDockWidget(Qt::BottomDockWidgetArea, dock4);
}
@SGaist @JonB Hi all, my CustomWidget looks like above. Parent is a parameter which I passed from mainwindow.cpp . I think I did understand the layout of QMainWindow, correct me if I was wrong. When I create my CustomWidget above 4 docks were hidden.
parent->setCentralWidget(Custome Widget); //tells my custom widget where to put itself otherwise a new window will pop up
parent->centralWidget()->hide(); // I want to hide central widget which mentioned https://doc.qt.io/qt-6/qmainwindow.html#details.
Custome Widget->show(); // I want to show my CustomWidget which central widget is hidden.I am expecting above code piece to achieve top part in chart from below, instead bottom part of chart was showing.
Hope this clarified my question a little bit!
-
CustomWidget::CustomWidget(Ui::MainWindow *uiMain, QMainWindow *parent)
: mUIMain(uiMain)
{
QDockWidget dock1 = new QDockWidget(parent);
dock1->setAllowedAreas(Qt::AllDockWidgetAreas);
parent->addDockWidget(Qt::TopDockWidgetArea, dock1);QDockWidget dock2 = new QDockWidget(parent); dock2->setAllowedAreas(Qt::AllDockWidgetAreas); parent->addDockWidget(Qt::TopDockWidgetArea, dock2); QDockWidget dock3 = new QDockWidget(parent); dock3->setAllowedAreas(Qt::AllDockWidgetAreas); parent->addDockWidget(Qt::BottomDockWidgetArea, dock3); QDockWidget dock4 = new QDockWidget(parent); dock4->setAllowedAreas(Qt::AllDockWidgetAreas); parent->addDockWidget(Qt::BottomDockWidgetArea, dock4);
}
@SGaist @JonB Hi all, my CustomWidget looks like above. Parent is a parameter which I passed from mainwindow.cpp . I think I did understand the layout of QMainWindow, correct me if I was wrong. When I create my CustomWidget above 4 docks were hidden.
parent->setCentralWidget(Custome Widget); //tells my custom widget where to put itself otherwise a new window will pop up
parent->centralWidget()->hide(); // I want to hide central widget which mentioned https://doc.qt.io/qt-6/qmainwindow.html#details.
Custome Widget->show(); // I want to show my CustomWidget which central widget is hidden.I am expecting above code piece to achieve top part in chart from below, instead bottom part of chart was showing.
Hope this clarified my question a little bit!
parent->setCentralWidget(Custome Widget); //tells my custom widget where to put itself otherwise a new window will pop up parent->centralWidget()->hide(); // I want to hide central widget which mentioned https://doc.qt.io/qt-6/qmainwindow.html#details. Custome Widget->show(); // I want to show my CustomWidget which central widget is hidden.
I have explained more than once that, after
setCentralWidget(Custome Widget)
, yourCustome Widget
IS your central widget. They are the same thing.Custome Widget == centralWidget()
. There is no point callingcentralWidget()->hide()
if you follow it byCustome Widget->show()
, that hides and then shows the same widget. In your case there is no separate central widget which is a different object from your custom widget, you have made your custom widget be the central widget. Until you understand this your question/code does not make sense.In your first code/screenshot the last statement is
parent->centralWidget()->hide();
, so the central widget/your custom widget is not shown. In your second code/screenshot the last statement isCustome Widget->show();
so the central widget/your custom widget is shown.I don't know what you want to achieve. Maybe you want to put your custom widget on the central widget? If so set a layout on your central widget and add your custom widget to that.
centralWidget()->setLayout(new QVBoxLayout); centralWidget()->layout()->addWidget(Custome Widget); Custome Widget->hide() /* or */ Custome Widget->show();
In this situation the custom widget would not replace the central widget. Now you can show and hide your custom widget while leaving the central widget shown.
-
parent->setCentralWidget(Custome Widget); //tells my custom widget where to put itself otherwise a new window will pop up parent->centralWidget()->hide(); // I want to hide central widget which mentioned https://doc.qt.io/qt-6/qmainwindow.html#details. Custome Widget->show(); // I want to show my CustomWidget which central widget is hidden.
I have explained more than once that, after
setCentralWidget(Custome Widget)
, yourCustome Widget
IS your central widget. They are the same thing.Custome Widget == centralWidget()
. There is no point callingcentralWidget()->hide()
if you follow it byCustome Widget->show()
, that hides and then shows the same widget. In your case there is no separate central widget which is a different object from your custom widget, you have made your custom widget be the central widget. Until you understand this your question/code does not make sense.In your first code/screenshot the last statement is
parent->centralWidget()->hide();
, so the central widget/your custom widget is not shown. In your second code/screenshot the last statement isCustome Widget->show();
so the central widget/your custom widget is shown.I don't know what you want to achieve. Maybe you want to put your custom widget on the central widget? If so set a layout on your central widget and add your custom widget to that.
centralWidget()->setLayout(new QVBoxLayout); centralWidget()->layout()->addWidget(Custome Widget); Custome Widget->hide() /* or */ Custome Widget->show();
In this situation the custom widget would not replace the central widget. Now you can show and hide your custom widget while leaving the central widget shown.
@JonB I have a question, will the default .ui file's central widget make this problem different? If don't have pre-defined central widget in .ui file I think your statement makes perfect sense.
With the pre-defined .ui file, my custom widget ie. docks will stay at top and bottom, leave some space in the middle ie. the pre-defined central widget.
Does this make more sense now?
-
@JonB I have a question, will the default .ui file's central widget make this problem different? If don't have pre-defined central widget in .ui file I think your statement makes perfect sense.
With the pre-defined .ui file, my custom widget ie. docks will stay at top and bottom, leave some space in the middle ie. the pre-defined central widget.
Does this make more sense now?
@Dev-G
No. It doesn't matter whether you use Designer/.ui
file or not. I cannot remember whether Designer for aQMainWindow
adds a blankQWidget
for its central widget (I have a feeling it does) or leaves it with no central widget. (You can discover by looking at the.ui
file or the generated.h
file, or byqDebug() << mainWindow->centralWidget()
in your code aftersetupUi()
.) If by chance it does not, you can always gomainWindow->setCentralWidget(new QWidget)
. Either way it works as I described and as documented.I don't know what else to say. Can't keep repeating the same stuff. Already suggested that, at a guess, you want to add your custom widget onto the central widget, not replace it.
-
CustomWidget::CustomWidget(Ui::MainWindow *uiMain, QMainWindow *parent)
: mUIMain(uiMain)
{
QDockWidget dock1 = new QDockWidget(parent);
dock1->setAllowedAreas(Qt::AllDockWidgetAreas);
parent->addDockWidget(Qt::TopDockWidgetArea, dock1);QDockWidget dock2 = new QDockWidget(parent); dock2->setAllowedAreas(Qt::AllDockWidgetAreas); parent->addDockWidget(Qt::TopDockWidgetArea, dock2); QDockWidget dock3 = new QDockWidget(parent); dock3->setAllowedAreas(Qt::AllDockWidgetAreas); parent->addDockWidget(Qt::BottomDockWidgetArea, dock3); QDockWidget dock4 = new QDockWidget(parent); dock4->setAllowedAreas(Qt::AllDockWidgetAreas); parent->addDockWidget(Qt::BottomDockWidgetArea, dock4);
}
@SGaist @JonB Hi all, my CustomWidget looks like above. Parent is a parameter which I passed from mainwindow.cpp . I think I did understand the layout of QMainWindow, correct me if I was wrong. When I create my CustomWidget above 4 docks were hidden.
parent->setCentralWidget(Custome Widget); //tells my custom widget where to put itself otherwise a new window will pop up
parent->centralWidget()->hide(); // I want to hide central widget which mentioned https://doc.qt.io/qt-6/qmainwindow.html#details.
Custome Widget->show(); // I want to show my CustomWidget which central widget is hidden.I am expecting above code piece to achieve top part in chart from below, instead bottom part of chart was showing.
Hope this clarified my question a little bit!
@Dev-G I confirm your CustomWidget class looks strange. When using designer, Qt Creator already provides you with a custom class using the output generate by uic.
Based on the name of your variables, you should have a class named MainWindow that is already using
Ui::MainWindow
hence CustomWidget is just a layer of indirection that complicates your code for no benefits. All the things you are currently doing in CustomWidget's constructor should be done in MainWindow's constructor. -
@Dev-G I confirm your CustomWidget class looks strange. When using designer, Qt Creator already provides you with a custom class using the output generate by uic.
Based on the name of your variables, you should have a class named MainWindow that is already using
Ui::MainWindow
hence CustomWidget is just a layer of indirection that complicates your code for no benefits. All the things you are currently doing in CustomWidget's constructor should be done in MainWindow's constructor. -
-
@SGaist Thank you for your help! Indeed it is something wrong with my CustomWIdget design. I was inherit QWidget before for some reason, when I switch back to QMainWindow and cleaned up a bit my code. Everything works just fine now!