在Qt上,如何在运行时更改工具栏中的动作图标? [英] On Qt, how to change the icon of an action in the toolbar at runtime?

查看:545
本文介绍了在Qt上,如何在运行时更改工具栏中的动作图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在计算磨损精度数字的程序中. 我在任务栏上有一个动作.

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()));

turnIconRed函数看起来与此类似:

the function turnIconRed is looking similar to this:

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

我提出了一些令人难以置信的丑陋算法:S.在Qt上没有简单的方法可以解决这个问题吗?有什么想法吗?

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?

谢谢.

推荐答案

我已经找到了使用 QToolButton 的解决方案,它是:

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

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

这篇关于在Qt上,如何在运行时更改工具栏中的动作图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆