Qt : how to implement QDialog StatusBar

13,826

Solution 1

I presume when you say it doesn't work, that you are not seeing the status bar when you run.

I don't see any way to do this wholly in the designer. The designer certainly resists the idea of promoting something to a QStatusBar. I suppose you could fool the designer by subclassing QStatusBar, and then promoting a QWidget to your subclass.

But I don't think we need to go that route just yet. I think with a few tweaks to the code you have above should help.

In the designer, add a layout, it doesn't matter what kind, at the bottom of your dialog. I called mine 'StatusBarLayout'. You can see the layout (the red box that's squished at the bottom). I removed the bottom margin in the dialog so that the status bar is flush at the bottom.

enter image description here

Now remove everything in the above code about layout l, and just do this:

bar = new QStatusBar(this);
pUI->StatusBarLayout->addWidget(bar);
pUI->textEdit->setStatusTip("XXX");

The textEdit was something added in the designer. Now when you run it you should see this:

enter image description here

I hope that helps

Edit:

You can also set the Status Tips for various widgets in the designer too, so there is no need to do that in the code unless you want to.

Solution 2

Try adding a QStatusBar like this:

QDialog dialog;
QLayout* layoutWidget = new QVBoxLayout(&dialog);
layoutWidget ->addWidget(new QTextEdit);
QStatusBar* statusBar = new QStatusBar;
layoutWidget ->addWidget(statusBar );
Share:
13,826
user63898
Author by

user63898

developer

Updated on June 26, 2022

Comments

  • user63898
    user63898 almost 2 years

    I have QDialog that is heavily designed with QDesigner , I saw on the web that I could add QStatusBar with code like this :

    #include <QDialog>
    #include <QStatusBar>
    #include <QLayout>
    #include <QApplication>
    #include <QTextEdit>
    #include <QStatusTipEvent>
    
    class Dialog : public QDialog {
    public:
    Dialog() : QDialog(){
    QLayout *l = new QVBoxLayout(this);
    QTextEdit *te = new QTextEdit;
    te->setStatusTip("XXX");
    l->addWidget(te);
    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);
    }
    };
    
    int main(int argc, char **argv){
    QApplication app(argc, argv);
    Dialog dlg;
    return dlg.exec();
    }
    

    Its not even working in my case .. maybe the QDialog is already have few layets that holds widget.

    My question is can I some how use palceholder in the QDesigner or somehow promote widget that place hold the QStatusbar class? I don’t know …

    What can I do in such case? can I implement new QStatusbar?

    Thanks

  • user63898
    user63898 almost 13 years
    i have problem , all my gui is allready done in the QtDesigner , how can i place the StatusBar with the QDesigner ? can i place Widget placeholder and then create my StatusBar and promote it with the designer?
  • user63898
    user63898 almost 13 years
    what i did i just placed widget place holder in the designer , and in code i just remove it and replace with the status bar