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

Dynamic QWidget from QtDesigner form



  • What I am doing wrong? I need dynamic QWidget with form created in QtDesigner. It is heavily decorated dialog therefore I cannot use QMessageBox. All code works but widget does not appear visible. It appears invisible and blocks underlaying widgets (form is Application Modal).

    void SomeClass::dialog(const QString& text)
    {
        if( Dlg == Q_NULLPTR )
        {
            Dlg = new QWidget((QWidget*)parent()); //parent() here is QMainWindow's centralWidget
            ui->setupUi( Dlg ); // ui is common QtDesigner form, it's Ok
            Dlg->raise();
            Dlg->update();
            connect( ui->okButton, SIGNAL(pressed()), this, SLOT(dialogYes()) );
            connect( ui->noButton, SIGNAL(pressed()), this, SLOT(dialogNo()) );
        }
        ui->messageText->setText( text );
        Dlg->show();
    }
    

    I remember that I did this years ago - and all worked fine. But may be something changed in latest Qt versions (or I forget do something).


  • Lifetime Qt Champion

    @Gourmet Why don't you simply instantiate your form directly instead of creating a QWidget instance?



  • @jsulm instantinate directly? This is direct "instantination" as it is described in docs. And form appears - but invisible without clean reason. Of course it has windowOpacity = 1.0 in properties.


  • Lifetime Qt Champion

    @Gourmet What I mean is:

    MyForm *myForm = new MyForm(parent());
    

    MyForm being your form created in designer.



  • @jsulm but in designer I have created just only UI XML file. And automatically generated ui_form.h file containing form init code. It requires QWidget to be shown.

    Can you tell why I do not see form with my code? If not - then show other solution completely, not only idea. I am straight on my code for while.


  • Lifetime Qt Champion

    @Gourmet I don't know why it is not visible. You could try the simple example from the doc with our ui to see whether it is visible:

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QWidget widget;
        Ui::YOUR_UI_HERE ui;
        ui.setupUi(&widget);
    
        widget.show();
        return app.exec();
    }
    

    Also you wrote: "It is heavily decorated dialog". What do you mean with "decorated"? Style-sheets?



  • @jsulm yes, style-sheets. The form is clearly visible in designer.

    The code you have shown is almost the same as mine. No significant difference.



  • @Gourmet

    What happens if you set your MainWindow as parent instead of the centralWidget? Or without any parent... Does it show up?



  • @Pl45m4 oops... parent() returns NULL. This is reason to investigate more...



  • The solution is:

    Dlg = new QWidget(QApplication::activeWindow());
    

Log in to reply