QStatusbar formatting and designing



  • I took a look at QStatusBar and i found the Things i need for what i want to do. But at the Moment i stuck with 1 Thing. I want Notifications in my Statusbar if something was successfull or went wrong. For that i want different Messages. At this Point its no Problem to do that, but i have a Problem with formatting and designing that Message. For the first Purpose i want just a Green Background for something that went good and red for others. After that the Background Color disappears.

    I searched the Web for Solutions and some Guys took QTimers, but this wont work in binding with my needs. Anyone has good Ideas for that?


  • Moderators

    Hi! You said timers won't work for you. Please elaborate on this so we can find a solution that suits your needs. The following uses a timer and looks good to me so far.

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QTimer>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_pushButton_clicked();
    
        void on_pushButton_2_clicked();
    
    private:
        Ui::MainWindow *ui;
        QTimer m_timer;
    
        void showStatusMessage(QString const &text, bool isOk);
        void onTimeout();
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QStatusBar>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        connect(&m_timer, &QTimer::timeout, this, &MainWindow::onTimeout);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::showStatusMessage(const QString &text, bool isOk)
    {
        ui->statusBar->showMessage(text);
        if (isOk)
           ui->statusBar->setStyleSheet("background-color: rgb(0, 255, 0); color: black");
        else
           ui->statusBar->setStyleSheet("background-color: rgb(255, 0, 0); color: black");
        m_timer.start(1000);
    }
    
    void MainWindow::onTimeout()
    {
        ui->statusBar->setStyleSheet("");
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        showStatusMessage("Good", true);
    }
    
    void MainWindow::on_pushButton_2_clicked()
    {
        showStatusMessage("Not good", false);
    }
    


  • oh thanks. looks like i used the timer wrong. this example helps me a lot.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.