2012-10-03 24 views
1

在計算abritary精確數字的程序中。 我在任務欄上有一個操作。在Qt上,如何在運行時更改工具欄中某個動作的圖標?

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中處理這個問題不是直接的嗎?有任何想法嗎?

謝謝。

+0

連接是無用的。相反,您可以根據需要調用任何方法。 – fasked

+0

這只是爲了演示。代碼實際上完全不同。 –

+1

我不明白問題是什麼:你發出一個信號,它發生了變化 - 它有多醜? – cmannett85

回答

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

...`this`和`this`之間

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

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

    fenetre.show(); 
    return app.exec(); 
} 
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())); 
} 
+0

這是一個很好的解決方案,但我自己找到了一個,因爲我沒有指望做任何其他遺產。感謝:D –

相關問題