Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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



  • @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)


  • Lifetime Qt Champion

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



  • @Christian-Ehrlicher

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



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


  • Lifetime Qt Champion

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



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



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



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



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


Log in to reply