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. QStackedWidget does not show in QDialog and how to discard pending changes
Qt 6.11 is out! See what's new in the release blog

QStackedWidget does not show in QDialog and how to discard pending changes

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 1.5k Views 1 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.
  • T Offline
    T Offline
    Tiago M.Pinto
    wrote on last edited by
    #1

    Hi!

    I am trying to implement a QStackedWidget in a custom class that inherits from QDialog but the QStackedWidget does not become visible. I tried in another custom class that inherits from QWidget and everything worked fine. Using setCurrentIndex didn't solve the problem either.
    What may be the problem?

    In addition, is there any mechanism in Qt to discard any pending changes in a QDialog when QDialogButtonBox::reject is triggered? Or is it necessary to keep track of all the values at the time the dialog is shown?

    Regards!

    Pl45m4P 1 Reply Last reply
    0
    • T Tiago M.Pinto

      Hi!

      I am trying to implement a QStackedWidget in a custom class that inherits from QDialog but the QStackedWidget does not become visible. I tried in another custom class that inherits from QWidget and everything worked fine. Using setCurrentIndex didn't solve the problem either.
      What may be the problem?

      In addition, is there any mechanism in Qt to discard any pending changes in a QDialog when QDialogButtonBox::reject is triggered? Or is it necessary to keep track of all the values at the time the dialog is shown?

      Regards!

      Pl45m4P Online
      Pl45m4P Online
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @Tiago-M-Pinto

      Hi.
      Does your QStackedWidget have your dialog class as parent? Was it added to your dialog's layout?
      Maybe you can show what you did?!

      No, AFAIK there is no "restore default" for QDialog, but it shouldn't be too hard to implement.
      (Returning default values / discard changes, when dialog fires rejected signal)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      T 1 Reply Last reply
      1
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Use a QWidget and not a QDialog - it's not meant to be included into a layout.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        Pl45m4P 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          Use a QWidget and not a QDialog - it's not meant to be included into a layout.

          Pl45m4P Online
          Pl45m4P Online
          Pl45m4
          wrote on last edited by
          #4

          @Christian-Ehrlicher

          Why?
          I've used a QStackedWidget with a QDialog before and it worked?!
          But it was added in QtDesigner.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          Christian EhrlicherC 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @Tiago-M-Pinto

            Hi.
            Does your QStackedWidget have your dialog class as parent? Was it added to your dialog's layout?
            Maybe you can show what you did?!

            No, AFAIK there is no "restore default" for QDialog, but it shouldn't be too hard to implement.
            (Returning default values / discard changes, when dialog fires rejected signal)

            T Offline
            T Offline
            Tiago M.Pinto
            wrote on last edited by
            #5

            @Pl45m4 Yes, my QStackedWidget has my dialog class as parent. I am trying to add a QGroupBox. This is my constructor:

            MyDialog::MyDialog(QWidget *parent)
                : QDialog(parent,Qt::WindowTitleHint)
            {    
                QVBoxLayout *layout = new QVBoxLayout(this);
                layout->setSizeConstraint(QLayout::SetFixedSize);
            
                QStackedWidget *stack = new QStackedWidget(this);
                stack->addWidget(new QGroupBox("My GroupBox"));
            
                QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help);
            
                layout->addWidget(stack);
                layout->addWidget(buttonBox);
            }
            

            The parent of this dialog is a QTreeWidget, maybe that's the problem? I will try with a widget instead of a dialog as @Christian-Ehrlicher suggested.

            JonBJ 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @Christian-Ehrlicher

              Why?
              I've used a QStackedWidget with a QDialog before and it worked?!
              But it was added in QtDesigner.

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Pl45m4 said in QStackedWidget does not show in QDialog and how to discard pending changes:

              I've used a QStackedWidget with a QDialog before and it worked?!

              Just because it worked doesn't mean it makes any sense. A QDialog is for a standalone dialog, nothing which should go into a layout. QDialog does not provide anything over a plain QWidget which helps you when you add it to a layout except that it sets some window flags internally which contradicts your efforts putting it in a layout.

              See https://doc.qt.io/qt-5/qdialog.html#details : 'A dialog window is a top-level window'

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • T Tiago M.Pinto

                @Pl45m4 Yes, my QStackedWidget has my dialog class as parent. I am trying to add a QGroupBox. This is my constructor:

                MyDialog::MyDialog(QWidget *parent)
                    : QDialog(parent,Qt::WindowTitleHint)
                {    
                    QVBoxLayout *layout = new QVBoxLayout(this);
                    layout->setSizeConstraint(QLayout::SetFixedSize);
                
                    QStackedWidget *stack = new QStackedWidget(this);
                    stack->addWidget(new QGroupBox("My GroupBox"));
                
                    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help);
                
                    layout->addWidget(stack);
                    layout->addWidget(buttonBox);
                }
                

                The parent of this dialog is a QTreeWidget, maybe that's the problem? I will try with a widget instead of a dialog as @Christian-Ehrlicher suggested.

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

                @Christian-Ehrlicher
                I think things are getting confused here. The code only shows a QStackedWidget being added onto a (layout on a) QDialog. That should be OK.

                @Tiago-M-Pinto
                If you have added a QDialog onto a layout that would not be, but I don't think you're saying that is the situation?

                T 1 Reply Last reply
                0
                • JonBJ JonB

                  @Christian-Ehrlicher
                  I think things are getting confused here. The code only shows a QStackedWidget being added onto a (layout on a) QDialog. That should be OK.

                  @Tiago-M-Pinto
                  If you have added a QDialog onto a layout that would not be, but I don't think you're saying that is the situation?

                  T Offline
                  T Offline
                  Tiago M.Pinto
                  wrote on last edited by Tiago M.Pinto
                  #8

                  @JonB
                  I am not adding the dialog onto a layout. I only dynamically allocate several new QDialog objects in a QTreeWidget object that becomes the parent of those dialogs.

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Tiago M.Pinto
                    wrote on last edited by
                    #9

                    Just solved this issue. I had some connections that I forgot to change when I implemented the stackedWidget and they were triggering the setVisible signal of the groupBox. I changed those connections in order to use stackedWidget::setCurrentIndex() and all worked fine.

                    JonBJ 1 Reply Last reply
                    1
                    • T Tiago M.Pinto

                      Just solved this issue. I had some connections that I forgot to change when I implemented the stackedWidget and they were triggering the setVisible signal of the groupBox. I changed those connections in order to use stackedWidget::setCurrentIndex() and all worked fine.

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

                      @Tiago-M-Pinto
                      :( Which is why coders should always test on a minimal, standalone application when they get unexpected behaviour in their large, existing application....

                      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