2012-12-04 26 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()));