2013-05-25 87 views
1

我對QT很新,在閱讀完文檔之後,我仍然無法從同一個類獲得按鈕調用方法。任何人都可以幫忙或指出我錯在哪裏?QT如何添加動作到按鈕

#include "GUI.h" 
    GUI::GUI() { 
    window = new QWidget(); 
    QGridLayout * layout = new QGridLayout; 

    //Sukuriami procesų label'iai 

    QLabel * startStopLabel = new QLabel("Start_Stop"); 
    QLabel * readUILabel = new QLabel("ReadUI"); 
    QLabel * jclLabel = new QLabel("JCL"); 
    QLabel * dataToOutputLabel = new QLabel("DataToOutput"); 
    QLabel * inputToRamLabel = new QLabel("InputToRam"); 
     QLabel * mainProcLabel = new QLabel("MainProc"); 
     QLabel * jobGovernorLabel = new QLabel("JobGovernor"); 
     QLabel * loaderLabel = new QLabel("Loader"); 
    QLabel * virtualMachineLabel = new QLabel("VirtualMachine"); 
     QLabel * interruptLabel = new QLabel("Interrupt"); 
     QLabel * printErrorLabel = new QLabel("PrintError"); 

     //Sukuriami procesų laukai duomenų išvedimui 

     startStop = new QTextBrowser(); 
     readUI = new QTextBrowser(); 
     jcl = new QTextBrowser(); 
     dataToOutput = new QTextBrowser(); 
     inputToRam = new QTextBrowser(); 
     mainProc = new QTextBrowser(); 
     jobGovernor = new QTextBrowser(); 
     loader = new QTextBrowser(); 
     virtualMachine = new QTextBrowser(); 
     interrupt = new QTextBrowser(); 
     printError = new QTextBrowser(); 


     forward = new QPushButton(); 
     addAction(); 
     connect(forward, SIGNAL(clicked()), this , SLOT(addText())); 
    // QPushButton * newJob = new QPushButton(); 
    // QPushButton * cancel = new QPushButton(); 

    // QAction * action; 
      QMainWindow * window2 = new QMainWindow(); 




     //layout tvarkymas 

     layout->addWidget(startStopLabel, 0, 0, Qt::AlignHCenter); 
     layout->addWidget(readUILabel, 0, 1, Qt::AlignHCenter); 
     layout->addWidget(jclLabel, 0, 2, Qt::AlignHCenter); 
     layout->addWidget(dataToOutputLabel, 0, 3, Qt::AlignHCenter); 
     layout->addWidget(startStop, 1, 0); 
     layout->addWidget(readUI, 1, 1); 
     layout->addWidget(jcl, 1, 2); 
     layout->addWidget(dataToOutput, 1, 3); 
     layout->addWidget(inputToRamLabel, 2, 0, Qt::AlignHCenter); 
    layout->addWidget(mainProcLabel, 2, 1, Qt::AlignHCenter); 
     layout->addWidget(jobGovernorLabel, 2, 2, Qt::AlignHCenter); 
     layout->addWidget(loaderLabel, 2, 3, Qt::AlignHCenter); 
     layout->addWidget(inputToRam, 3, 0); 
     layout->addWidget(mainProc, 3, 1); 
     layout->addWidget(jobGovernor, 3, 2); 
     layout->addWidget(loader, 3, 3); 
     layout->addWidget(virtualMachineLabel, 4, 0, Qt::AlignHCenter); 
     layout->addWidget(interruptLabel, 4, 1, Qt::AlignHCenter); 
     layout->addWidget(printErrorLabel, 4, 2, Qt::AlignHCenter); 
     layout->addWidget(virtualMachine, 5, 0); 
     layout->addWidget(interrupt, 5, 1); 
     layout->addWidget(printError, 5, 2); 
     layout->addWidget(forward, 5, 3); 


     window->setLayout(layout); 
     window->setWindowState(Qt::WindowMaximized); 
     window->setWindowTitle("AutoMagic"); 
     window->setWindowIcon(QIcon("kiriya.jpg")); 

    window->show(); 
    } 

    GUI::~GUI() { 
    } 

    void GUI::addText(){ 

     startStop->append("works"); 
     window->repaint(); 
    } 

和gui.h

class GUI: public QMainWindow { 

     Q_OBJECT 

     QWidget * window; 
     QTextBrowser * startStop; 
     QTextBrowser * readUI; 
     QTextBrowser * jcl; 
     QTextBrowser * dataToOutput; 
     QTextBrowser * inputToRam; 
     QTextBrowser * mainProc; 
     QTextBrowser * jobGovernor; 
     QTextBrowser * loader; 
     QTextBrowser * virtualMachine; 
    QTextBrowser * interrupt; 
    QTextBrowser * printError; 
    QPushButton * forward; 

    public: 
     GUI(); 
     virtual ~GUI(); 
     void addText(); 
     void addAction(); 
    private: 

    }; 

我將不勝感激,如果有人可以幫助我理解這部分內容。

+1

您還沒有將'addText()'定義爲'slot'。 – cmannett85

回答

1

您需要了解Qr的信號和插槽機制。 最簡單的地方就是Qt附帶的示例程序之一。

像任何事件驅動的GUI程序看起來複雜在第一,但它可能是最簡單的機制之一,實際上

2

工作,你必須充分了解信號和槽(它們用於對象之間的通信) 。這個機制是Qt的一個核心特徵,可能是與其他框架提供的功能大不相同的部分。

閱讀這篇文章,解釋他們的基礎知識:

http://qt-project.org/doc/qt-4.8/signalsandslots.html

1
connect (ui->pushButton, SIGNAL(clicked()), this, SLOT(pushButtonClicked())); 

pushButtonClicked()是你定義的函數。