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. CentralWidget hide & show issue
Forum Updated to NodeBB v4.3 + New Features

CentralWidget hide & show issue

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 899 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.
  • JonBJ JonB

    @Dev-G
    I think you do not understand something. If you put in

    Q_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 if show().

    D Offline
    D Offline
    Dev-G
    wrote on last edited by
    #5

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

    9dac99f9-4cad-4b21-bd41-1c41d2153373.jpg

    D 1 Reply Last reply
    0
    • D Dev-G

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

      9dac99f9-4cad-4b21-bd41-1c41d2153373.jpg

      D Offline
      D Offline
      Dev-G
      wrote on last edited by
      #6

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

      9dac99f9-4cad-4b21-bd41-1c41d2153373.jpg

      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.

      JonBJ 1 Reply Last reply
      0
      • D Dev-G

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

        9dac99f9-4cad-4b21-bd41-1c41d2153373.jpg

        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.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #7

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

        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #8

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply
          2
          • SGaistS SGaist

            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.

            D Offline
            D Offline
            Dev-G
            wrote on last edited by
            #9

            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.9dac99f9-4cad-4b21-bd41-1c41d2153373.jpg

            Hope this clarified my question a little bit!

            JonBJ SGaistS 2 Replies Last reply
            0
            • D Dev-G

              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.9dac99f9-4cad-4b21-bd41-1c41d2153373.jpg

              Hope this clarified my question a little bit!

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #10

              @Dev-G

              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), your Custome Widget IS your central widget. They are the same thing. Custome Widget == centralWidget(). There is no point calling centralWidget()->hide() if you follow it by Custome 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 is Custome 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.

              D 1 Reply Last reply
              2
              • JonBJ JonB

                @Dev-G

                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), your Custome Widget IS your central widget. They are the same thing. Custome Widget == centralWidget(). There is no point calling centralWidget()->hide() if you follow it by Custome 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 is Custome 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.

                D Offline
                D Offline
                Dev-G
                wrote on last edited by
                #11

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

                JonBJ 1 Reply Last reply
                0
                • D Dev-G

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

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #12

                  @Dev-G
                  No. It doesn't matter whether you use Designer/.ui file or not. I cannot remember whether Designer for a QMainWindow adds a blank QWidget 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 by qDebug() << mainWindow->centralWidget() in your code after setupUi().) If by chance it does not, you can always go mainWindow->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.

                  1 Reply Last reply
                  2
                  • D Dev-G

                    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.9dac99f9-4cad-4b21-bd41-1c41d2153373.jpg

                    Hope this clarified my question a little bit!

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    D 1 Reply Last reply
                    1
                    • SGaistS SGaist

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

                      D Offline
                      D Offline
                      Dev-G
                      wrote on last edited by
                      #14

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

                      SGaistS 1 Reply Last reply
                      0
                      • D Dev-G has marked this topic as solved on
                      • D Dev-G

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

                        SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #15

                        @Dev-G KISS is a good mantra to keep when developing :-)
                        Glad it's working now !

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        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