【问题标题】:On Qt, how to change the icon of an action in the toolbar at runtime?在 Qt 上,如何在运行时更改工具栏中的动作图标?
【发布时间】:2012-09-23 23:08:46
【问题描述】:

在计算 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 上没有直接的方法来处理这个问题吗?有什么想法吗?

谢谢。

【问题讨论】:

  • thisthis 之间的连接无效。相反,您可以在需要的地方调用任何方法。
  • 这只是为了演示。代码实际上是完全不同的。
  • 我不明白问题出在哪里:你发出一个信号,它就改变了——它有多丑?

标签: c++ qt icons runtime qtoolbar


【解决方案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"));
}

现在,在您的 MainWindow 中,您只需将其插槽连接到正确的信号即可使用该自定义 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()));
}

【讨论】:

  • 这是一个很好的解决方案,但我自己找到了一个,因为我不指望做任何其他遗产。谢谢:D
【解决方案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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 2011-12-23
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多