QT新手,简单的connect问题求助

wryse 2010-08-17 11:43:40
刚上手qt,很简单的代码,纯粹想试一下,结果就撞墙了 = =|||
class TestButton : public QPushButton
{
public:
TestButton(QWidget *parent = 0) : QPushButton(parent) {}
void click()
{
QMessageBox testBox(QMessageBox::NoIcon, "cc", "dd");
testBox.exec();
};
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QMainWindow testWindow;
testWindow.resize(640, 480);

TestButton *testButton = new TestButton(&testWindow);
testButton->resize(100, 30);
testButton->move(200, 200);
if (QObject::connect(testButton, SIGNAL(clicked()), testButton, SLOT(click())) == false)
{
QMessageBox testBox(QMessageBox::NoIcon, "aa", "bb");
testBox.exec();
}

testWindow.show();
app.exec();
delete testButton;
return 0;
}

现象是connect不会出错,但点上按钮就直接出segment fault,也就是空指针错误。我是linux系统。
个人估计还是button用法不对,但没查到该怎么写。还请高手指点。
...全文
236 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
巴依老爷 2010-08-17
  • 打赏
  • 举报
回复
还得加上Q_OBJECT

class TestButton : public QPushButton
{
Q_OBJECT
...
lefttime 2010-08-17
  • 打赏
  • 举报
回复
testButton中没有定义click()这个槽~`` 要符合代码中调用的话, TestButton中应该将void click()定义成public slots~``
巴依老爷 2010-08-17
  • 打赏
  • 举报
回复
对c++标准来说当然没问题,问题是qt自己需要一些额外的东西(moc),这就有限制了。
其实是可以只有一个main.cpp的,不过那样就更难懂了,需要在int main()主函数之前#include "main.moc"
所以我觉得还是放在.h里更自然些
wryse 2010-08-17
  • 打赏
  • 举报
回复
呃,终于搞定了,多谢指点~看来果然不能偷懒……不过比较奇怪为什么不能放在一个cpp里写,那样不违反什么规则吧,虽然的确不规范……
巴依老爷 2010-08-17
  • 打赏
  • 举报
回复
只有一个cpp那不行,得把TestButton的定义单独放到TestButton.h里
wryse 2010-08-17
  • 打赏
  • 举报
回复
我的编译步骤是,只留一个cpp文件,内容就是6楼的代码加上include。然后依次执行qmake -project(生成pro文件),qmake(生成Makefile,没有Makefile.Debug或者Makefile.Release),make(9楼的错误信息)
巴依老爷 2010-08-17
  • 打赏
  • 举报
回复
运行qmake,确认你的Makefile,Makefile.Debug,Makefile.Release都是这次运行生成的
还是不行的话。。。新建一个工程
wryse 2010-08-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 darkdong 的回复:]

删除所有makefile,重新生成一次
[/Quote]
试过,只留cpp其他都删的,一样的结果
巴依老爷 2010-08-17
  • 打赏
  • 举报
回复
删除所有makefile,重新生成一次
wryse 2010-08-17
  • 打赏
  • 举报
回复
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -o test.o test.cpp
g++ -Wl,--no-undefined -o TestQt test.o -L/usr/lib -lQtGui -lQtCore -lpthread
test.o: In function `main':
test.cpp:(.text+0x92): undefined reference to `vtable for TestButton'
test.cpp:(.text+0x99): undefined reference to `vtable for TestButton'
collect2: ld 返回 1
make: *** [TestQt] 错误 1


以上是错误信息,代码完全用的6楼的代码,就多了几个include,编译过程是qmake-project,qmake,make
lefttime 2010-08-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wryse 的回复:]

引用 6 楼 lefttime 的回复:

C/C++ code
class TestButton : public QPushButton
{
Q_OBJECT
public:
TestButton(QWidget *parent = 0) : QPushButton(parent) {}

public slots:
void click()
{
QMessageBo……
[/Quote]
最好把错误信息完整贴出来看看 :)
wryse 2010-08-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lefttime 的回复:]

C/C++ code
class TestButton : public QPushButton
{
Q_OBJECT
public:
TestButton(QWidget *parent = 0) : QPushButton(parent) {}

public slots:
void click()
{
QMessageBox testBox……
[/Quote]

我现在完全照抄,编译时也报undefined reference to `vtable for TestButton'。是在链接步骤报的错。我用的qmake生成的makefile不应该少东西吧,是不是代码里还少了什么?
多谢楼上各位回复,还请再指点一下!
lefttime 2010-08-17
  • 打赏
  • 举报
回复
class TestButton : public QPushButton
{
Q_OBJECT
public:
TestButton(QWidget *parent = 0) : QPushButton(parent) {}

public slots:
void click()
{
QMessageBox testBox(QMessageBox::NoIcon, "cc", "dd");
testBox.exec();
};
};
......
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QMainWindow testWindow;
testWindow.resize(640, 480);

TestButton *testButton = new TestButton(&testWindow);
testButton->resize(100, 30);
testButton->move(200, 200);
if (QObject::connect(testButton, SIGNAL(clicked()), testButton, SLOT(click())) == false)
{
QMessageBox testBox(QMessageBox::NoIcon, "aa", "bb");
testBox.exec();
}

testWindow.show();
app.exec();
delete testButton;
return 0;
}
skyjsq 2010-08-17
  • 打赏
  • 举报
回复
click()的定义有问题要加上这句:
public slots:
void click()
{
QMessageBox testBox(QMessageBox::NoIcon, "cc", "dd");
testBox.exec();
};
wryse 2010-08-17
  • 打赏
  • 举报
回复
在TestButton类的开头加上Q_OBJECT以后编译时报告undefined reference to `vtable for TestButton'。是还少什么东西没加还是别的原因?
wryse 2010-08-17
  • 打赏
  • 举报
回复
QPushButton中有click这个槽的吧,不能通过继承来取得么?能麻烦楼上二位给出完整代码么?

16,225

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧