Nominate our 2022 Qt Champions!

[SOLVED]Show and use a custom dialog from within a widget or mainwindow

  • Hi all,
    I have a 'stupid' question about using a costum dialog from within a widget/mainwindow.
    I have a widget with a pushbutton, when i push the button i want to show a custom dialog and use its input to work with inside the first widget.
    When i try this from within 'main.cpp' it works without a problem, but when i use the same from within the widget nothing seems to happen.
    Does anybody know what im doing wrong or better, where i have to look for it?
    This seems to not work:
    //filename is widget.cpp
    #include "dialog.h"
    void Widget::on_pushButton_clicked()
    this.hide(); //works
    Dialog d; // doesnt work?; // doesnt work
    This works:
    //filename is main.cpp
    #include "dialog.h"
    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    Dialog d; //works; //works
    return a.exec();

    I know that i can make a function within the main.cpp to get it work, but i want to use it from within the widget.cpp and not use the main.cpp for functions ed. Is this possible or is it a really stupid question?

    Thanks in advance,

  • You should not hide your main widget.

    You should call d.exec() on the dialog. This opens the new window as modal dialog, i.e. it blocks input to all other windows while it is open. exec() returns when the dialog is closed (either by accept() or by reject(). See the docs of "QDialog": for some more information.

  • This looks a lot like another, "very recent discussion": here. You did "search before asking":, didn't you?

  • Thanks both!
    And yes i've searched but with the wrong keywords i quess. :)
    Sorry, for wasting your time.

Log in to reply