c++qticonsruntimeqtoolbar

On Qt, how to change the icon of an action in the toolbar at runtime?


In a program which calculates abritary precision numbers. I have an action on the taskbar.

QAction* button_stop_continue.

I've set the icon green icon in the beginning of the program, when calculations are being executed it should turn red and so on.

I've already tried something like this:

connect(this, SIGNAL(startedComputing()), this, SLOT(turnIconRed()));
connect(this, SIGNAL(finishedComputing()), this, SLOT(turnIconGreen()));

the function turnIconRed is looking similar to this:

void turnIconRed()
{
    button_stop_continue->setIcon(QIcon("images/red-light.png"));
}

I've come up with some incredibly-ugly algorithms :S. Isn't there a straight-forward way to deal with that on Qt? Any ideas?

Thanks.


Solution

  • I've found a solution using QToolButton here it is:

    Header : FenPrincipale.h

    #ifndef FENPRINCIPALE_H
    #define FENPRINCIPALE_H
    
    #include <QWidget>
    #include <QVBoxLayout>
    #include <QToolButton>
    #include <QToolBar>
    #include <QAction>
    #include <QTextEdit>
    
    class FenPrincipale : public QWidget {
        Q_OBJECT
    public:
        FenPrincipale();
    
    private slots:
        void goComputing();
        void stopComputing();
    
    private:
    
        QAction* actionDemarrer;    // Start computing
        QAction* actionArreter;     // Stop computing
        QToolBar* toolBarActions;
    
        QToolButton* boutonAction;  // this button holds the appropriate action
        QVBoxLayout* layoutPrincipale;
        QTextEdit* resultat;        // show result
    };
    

    ...

    Implementation : FenPrincipale.cpp

    #include "FenPrincipale.h"
    
    FenPrincipale::FenPrincipale() : QWidget()
    {
        this->setFixedSize(400, 200);
    
        // create actions
        actionDemarrer = new QAction(QIcon("bouton-vert.png"), "demarrer", this);
        actionArreter = new QAction(QIcon("bouton-rouge.png"), "arreter", this);
        boutonAction = new QToolButton;
        boutonAction->setDefaultAction(actionDemarrer);
    
        // create toolbar
        toolBarActions = new QToolBar(this);
        toolBarActions->addWidget(boutonAction);
    
        // create result widget
        resultat = new QTextEdit(this);
    
        // create layout
        layoutPrincipale = new QVBoxLayout(this);
        layoutPrincipale->addWidget(toolBarActions);
        layoutPrincipale->addWidget(resultat);
        this->setLayout(layoutPrincipale);
    
    
        // make connections
        QObject::connect(actionDemarrer, SIGNAL(triggered()), this, SLOT(goComputing()));
        QObject::connect(actionArreter, SIGNAL(triggered()), this, SLOT(stopComputing()));
    }
    
    void FenPrincipale::goComputing()
    {
        resultat->setText("Computing...");
        boutonAction->setDefaultAction(actionArreter);
    }
    
    void FenPrincipale::stopComputing()
    {
        resultat->setText("Partial result : {?}");
        boutonAction->setDefaultAction(actionDemarrer);
    }
    

    ...

    Main

    #include <QApplication>
    #include "FenPrincipale.h"
    
    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
        FenPrincipale fenetre;
    
        fenetre.show();
        return app.exec();
    }