Ufx
Ufx

Reputation: 2695

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.

Upvotes: 1

Views: 1542

Answers (2)

hyde
hyde

Reputation: 62898

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();
}

Upvotes: 3

Jablonski
Jablonski

Reputation: 18524

Create dialog with QVBoxLayout for example.

Place all widget which you need and add QStatusBar on bottom.

In main.cpp:

QDialog dlg;
QLayout *l = new QVBoxLayout(&dlg);
QLabel *lbl = new QLabel("text");
QPushButton *bb = new QPushButton("text");
l->addWidget(bb);
l->addWidget(lbl);
QStatusBar *b = new QStatusBar;
l->addWidget(b);
b->showMessage("text");
l->setMargin(0);
l->setSpacing(0);
dlg.show();

Update: for status tips you should create QDialog subclass:

dia.h:

#ifndef DIA_H
#define DIA_H

#include <QDialog>
 #include <QStatusBar>
 #include <QLayout>
 #include <QApplication>
 #include <QTextEdit>
 #include <QStatusTipEvent>
 #include <QPushButton>

 class Dia : public QDialog {
 public:
    Dia() : QDialog()
    {
        QVBoxLayout *l = new QVBoxLayout(this);
        QTextEdit *te = new QTextEdit;
        te->setStatusTip("this is TextEdit");
        l->addWidget(te);
        QPushButton *bb = new QPushButton("tezt");
        bb->setStatusTip("this is button");
        l->addWidget(bb);
        bar = new QStatusBar;
        l->addWidget(bar);
        l->setMargin(0);
        l->setSpacing(0);
    }
 private:
    QStatusBar *bar;
 protected:
    bool event(QEvent *e)
    {
        if(e->type()==QEvent::StatusTip)
        {
            QStatusTipEvent *ev = (QStatusTipEvent*)e;
            bar->showMessage(ev->tip());
            return true;
        }
        return QDialog::event(e);
    }
 };
#endif // DIA_H

Using:

#include "dia.h"
//...    
Dia dlg;
dlg.show();

Upvotes: 3

Related Questions