c++qtqmainwindowqdialog

Dialog with statusbar


I need to use a dialog with a statusbar. I don't know how can I use it in QDialog so I use QMainWindow. But QMainWindow doesn't have exec() function and show() function works asynchronously.

I use QtDesigner and I did not understand how can I add statusbar using it. I also want to see status tips of widnow's widgets in this statusbar.


Solution

  • You can make your own dialog window modal with QWidget::setWindowModality, probably ApplicationModal or WindowModal, for example in its constructor. Additionally, you probably want to set window flags for dialog, so you can give you dialog a parent. So, add these to your dialog mainwindow constructor:

    setWindowModality(Qt::ApplicationModal);
    setWindowFlags(Qt::Dialog);
    

    That way it will open as independent window even with parent, block out rest of your GUI until closed, prevent that from getting user input events. This should behave the same as if you used QDialog::open.

    To catch the user closing the dialog, you should probably add the same signals as used by QDialog to it, and emit them as appropriate. That way you can your custom dialog and QDialog interchangeably, and also your code is easier to understand (this is called static polymorphism, giving semantically unrelated but functionally equivalent things same names).


    Here's some example code, first constructor of DialogWindow custom class:

    DialogWindow ::DialogWindow (QWidget *parent) : QMainWindow(parent)
    {
        setWindowFlags(Qt::Dialog);
        setWindowModality(Qt::ApplicationModal);
        setCentralWidget(new QLabel("Dialog")); // show some content
    }
    

    And then main function to use it:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QPushButton w("Open the Dialog"); // the "main window" of the whole application
        DialogWindow *dialog = new DialogWindow (&w);
        QObject::connect(&w, SIGNAL(clicked()), dialog, SLOT(show()));
        w.show();
    
        return a.exec();
    }