首页| 论坛| 消息

标题:关于信号和槽.....一个初学者的问题
作者:tresss
日期:2006-12-07 12:48
内容:

刚开始学QT....看了一份教程,可能比较老...很多地方与现在的QT版本对不上.

现在看到信号和槽的链接上.代码如下:
#include
#include
#include
//#include

class MWindow:public QWidget
{
Q_OBJECT
public:
MWindow();
public slots:
void MwExit();
private:
QPushButton *BtnExit;
};
MWindow::MWindow()
{
setGeometry(200,200,100,300); //the point of Mainform When start
BtnExit=new QPushButton("Select All",this);
BtnExit->setGeometry(10,10,80,30);
BtnExit->setFont(QFont("Times",12,QFont::Bold,TRUE));
connect(BtnExit,SIGNAL(clicked()),this,SLOT(MwExit()));
}
void MWindow::MwExit()
{
BtnExit->setGeometry(30,30,60,30);
exit(0);
}
void main(int argc,char **argv)
{
QApplication a(argc,argv);
MWindow w;
w.show();
a.exec();
}

这里编译为什么通不过...提示很多"undefined reference to `vtable for MWindow'"
如果将上边的Q_OBJECT注释掉之后,编译是成功的.但是这时点击按钮没有任何反应.........
错误在哪里呢?!


#1 [cavendish 12-07 16:26]
没有moc
注意你的编译过程
建议使用tmake/qmake辅助生成makefile
#2 [漏斗の水风 12-08 10:11]
或者你将写成.h,.cpp和main.cpp这种形式
我记得我最开始也遇到这种情况,改写成这样就行了,也不知道是为什么。你试试看
#3 [soyeon 12-11 09:01]
我刚开始的时候也遇到这样的问题,当时是把2个文件合成了一个文件才可以通过编译的,所以说这个问题的原因不出在这里,请问哪个高手知道原因的,请告诉我们一下,谢谢了。。。
#4 [soyeon 12-11 09:28]
好像 不用那个 Q-OBJECT就 OK了,
#5 [浪漫天使 12-11 15:15]
把下面的部分 放到一个 头文件里面,就叫main.h
#include
#include
#include
//#include

class MWindow:public QWidget
{
Q_OBJECT
public:
MWindow();
public slots:
void MwExit();
private:
QPushButton *BtnExit;
};
把下面放到main.cpp里面
#include "main.h"
MWindow::MWindow()
{
setGeometry(200,200,100,300); //the point of Mainform When start
BtnExit=new QPushButton("Select All",this);
BtnExit->setGeometry(10,10,80,30);
BtnExit->setFont(QFont("Times",12,QFont::Bold,TRUE));
connect(BtnExit,SIGNAL(clicked()),this,SLOT(MwExit()));
}
void MWindow::MwExit()
{
BtnExit->setGeometry(30,30,60,30);
exit(0);
}
void main(int argc,char **argv)
{
QApplication a(argc,argv);
MWindow w;
w.show();
a.exec();
}
OK

<< 1 2 >> (1/2)

回复 发表
主题 版块