1

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でそれを処理する簡単な方法はありませんか?何か案は?

ありがとう。

4

2 に答える 2

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()));
}
于 2012-10-03T06:38:33.870 に答える
1

ここで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();
}
于 2012-10-04T05:21:38.247 に答える