2012-12-04 27 views
0

我試圖使用Qt對一些基本的視覺應用學習C++。我想在開始時做一些簡單的事情,當我按下按鈕時,我想在某處顯示文本消息。這裏是我的代碼:不能使信號和槽工程的Qt

main.h

#ifndef MYAPP_H 
#define MYAPP_H 

#include <QWidget> 

class QLabel; 
class QString; 

class MyApp : public QWidget{ 
    public: 
     MyApp(QWidget *parent = 0); 
    protected slots: 
     void showIt(); 
    private: 
     QString *text_msg; 
     QLabel *message; 
}; 

#endif 

的main.cpp

#include <QApplication> 
#include <QFont> 
#include <QPushButton> 
#include <QWidget> 
#include <QLabel> 
#include <QVBoxLayout> 
#include <QFrame> 
#include <QString> 
#include <vector> 
#include <string> 
#include <map> 
#include "main.h" 

#include <fstream> 

using std::map; 
using std::vector; 
using std::string; 
using std::ofstream; 



/* implementation */ 
MyApp::MyApp(QWidget *parent):QWidget(parent){ 

    QString text_msg; 
    text_msg = "This is my first message written in C++! \n It was printed with Qt!"; 

    setFixedSize(400, 280); 

    QPushButton *quit = new QPushButton(tr("Quit"), this); 
    quit->setGeometry(62, 40, 75, 50); 
    quit->setFont(QFont("Times", 18, QFont::Bold)); 

    QPushButton *show_msg = new QPushButton(tr("Show!"), this); 
    show_msg->setGeometry(30,15,75,45); 
    show_msg->setFont(QFont("Times", 18, QFont::Bold)); 


    //message = new QLabel(); 
    QLabel *message = new QLabel(this); 
    //message->setFrameStyle(QFrame::Panel | QFrame::Sunken); 
    //message->setText(text_msg); 
    //message->setText("asdf"); 

    connect(quit, SIGNAL(clicked()), qApp, SLOT(quit())); 
    connect(show_msg, SIGNAL(clicked()), qApp, SLOT(showIt())); 

    QVBoxLayout *layout = new QVBoxLayout; 


    layout->addWidget(message); 
    layout->addWidget(show_msg); 
    layout->addWidget(quit); 


    setLayout(layout); 

     ofstream myfile; 
    myfile.open ("example"); 
    myfile << "Writing this to a file.\n"; 
    myfile.close(); 
} 

void MyApp::showIt(){ 
    //*text_msg = "xyz"; 
    ofstream myfile; 
    myfile.open ("example"); 
    myfile << "12121212121.\n"; 
    myfile.close(); 
    message->setText("1234"); 
} 



int main(int argc, char *argv[]) 
{ 
    /* assign messages for output 
    bool result; 
    string key = "first", message="this is a sample text"; 
    result = Messages::add_message(key, message); 
    */ 
    /* end */ 
    QApplication app(argc, argv); 
    MyApp my_simple_app; 
    my_simple_app.show(); 
    return app.exec(); 

} 

我不明白爲什麼程序不運行插槽成員函數。我在那裏放了一些應該在文件中打印一些文本的代碼,以瞭解該函數中的代碼是否將被執行,並且問題出現在QLabel消息中,但是成員函數不會被執行。

任何人都可以幫到我嗎?

謝謝。

回答

3

有3個東西,我需要改變你的代碼,使其工作:

首先,在main.h您需要使用Q_OBJECT宏:

class MyApp : public QWidget { 
    Q_OBJECT 

其次,在main.cxx,您需要更改爲正確的接收器連接調用(this而不是myApp):

connect(show_msg, SIGNAL(clicked()), this, SLOT(showIt())); 

第三,在main.cxx,您需要取消對創建message標籤作爲類成員的代碼:

message = new QLabel(this); 
+0

我相信connect可以和3個參數一起使用,使得'this'接收器是隱式的。 – Cubic

+0

@Cubic是的,這是真的,但我想使修復更加明確,這將有望更加明顯,有人試圖同時學習C++和Qt。 –

0

在此connect(show_msg, SIGNAL(clicked()), qApp, SLOT(showIt())); stainment改變qAppthis,然後再試一次

這意味着

connect(show_msg, SIGNAL(clicked()), this, SLOT(showIt())); 

我不是發售者是否有privat slots:protected slots:但usuely我用privat slots:也ü可以改變它,之間的不同試試:)

+0

它的工作,謝謝RA。 –

2

最重要的部分是信號連接到插槽這樣一個給定的對象:

connect(object_emitting, SIGNAL(clicked()),object_receiving, SLOT(showIt()));