Qt的,如何在运行时更改的工具栏在动作的图标?(On Qt, how to change the i

2019-08-01 03:25发布

在该计算abritary精度数的程序。 我有任务栏上的动作。

QAction* button_stop_continue.

我给自己定的图标绿色图标在节目的开始,正在执行计算时,应当开启红色等。

我已经尝试过这样的事情:

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

功能turnIconRed正在与此类似:

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

我想出了一些令人难以置信的丑陋的算法:S。 是不是有直接的方式来处理与Qt的? 有任何想法吗?

谢谢。

Answer 1:

我将继承的QAction并添加一些逻辑在其中也可以是状态。 这是从来没有硬编码的东西颜色为方法的名称是个好主意。 通过继承的QAction的外观和感觉被封装。

这可能是这样的:

头文件:

class StateAction : public QAction
{
    Q_OBJECT
public:
    explicit StateAction(QObject *parent = 0);

public slots:
    void start();
    void stop();
    void pause();
};

实现文件:

StateAction::StateAction(QObject *parent) :
    QAction(parent)
{
    this->stop();
}

void StateAction::start()
{
    this->setIcon(QIcon(":/states/start.png"));
}

void StateAction::stop()
{
    this->setIcon(QIcon(":/states/stop.png"));
}

void StateAction::pause()
{
    this->setIcon(QIcon(":/states/pause.png"));
}

现在,在你的主窗口,你可以使用自定义的QAction简单地通过它的插槽连接到正确的信号:

头文件:

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

signals:
    void startedComputing();
    void finishedComputing();
    void pausedComputing();

private:
    void createActions();
    void createToolbars();
    void createConnections();
    StateAction *m_stateAction;
};

实现文件:

...

void MainWindow::createConnections()
{
    connect(this, SIGNAL(startedComputing()), m_stateAction, SLOT(start()));
    connect(this, SIGNAL(finishedComputing()), m_stateAction, SLOT(stop()));
    connect(this, SIGNAL(pausedComputing()), m_stateAction, SLOT(pause()));
}


Answer 2:

我发现这里使用QToolButton它是一个解决方案:

标题: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
};

...

实施: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);
}

...

主要

#include <QApplication>
#include "FenPrincipale.h"

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    FenPrincipale fenetre;

    fenetre.show();
    return app.exec();
}


文章来源: On Qt, how to change the icon of an action in the toolbar at runtime?