• 6324阅读
  • 7回复

新手求助,大家来帮我看看错在哪里了 [复制链接]

上一主题 下一主题
离线larryzhao
 

只看楼主 倒序阅读 楼主  发表于: 2006-05-19
刚刚接触KDE/Qt,正在慢慢学习,以下是我写的一个类:

#include <qapplication.h>
#include <qlabel.h>
#include <qstring.h>
#include <qwidget.h>
#include <qpainter.h>
#include <qpushbutton.h>
#include <kapp.h>
#include <stdio.h>

class threeWidget : public QWidget{
  Q_OBJECT

  public :
    threeWidget(QWidget *parent = 0, const char *name = 0){
        QWidget(parent, name);
       
        aButton = new QPushButton("topbutton", this);
        aLabel = new QLabel("thelabel", this);
        //this.addWidget(aButton);
        aButton->setGeometry(15, 15, 90, 20);
        aLabel->setGeometry(15, 40, 90, 40);
        QObject::connect(aButton, SIGNAL(clicked()), this, SLOT(buttonHandler()));
    }
 
  public slots:
    void buttonHandler(){

        aButton->setText("aaaaa");
    }

  private:
    QPushButton *aButton;
    QLabel     *aLabel;
};

问题在于,只要加上"Q_OBJECT",编译就会报错:
************************************
main.o: In function `threeWidget':/home/larry_zb/helloworld/src/main.cpp:52: undefined reference to `vtable for threeWidget'
:/home/larry_zb/helloworld/src/main.cpp:52: undefined reference to `vtable for threeWidget'
main.o: In function `~threeWidget':/home/larry_zb/helloworld/src/main.cpp:48: undefined reference to `vtable for threeWidget'
:/home/larry_zb/helloworld/src/main.cpp:48: undefined reference to `vtable for threeWidget'
************************************

可是书上说要设置slot必须要声明"Q_OBJECT",大家帮我看看是那里错了,谢谢大家


PS: 有没有专门讲Qt Designer的电子书呀,能给小弟几本么?谢谢大家了
PS2: KDE/QT编程有没有象msdn和javadoc这样比较方便的api库的帮助亚?KDevelop连函数提示都没有,照书抄的迷迷糊糊的...
离线XChinux

只看该作者 1楼 发表于: 2006-05-19
Qt Assistant,KAssistant
这就是帮助。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线bruce1007
只看该作者 2楼 发表于: 2006-05-19
你的是这样:
public :
  threeWidget(QWidget *parent = 0, const char *name = 0){
    QWidget(parent, name);

  。。。

试试这样:
public :
  threeWidget(QWidget *parent = 0, const char *name = 0) : QWidget(parent, name)
{
...
}
离线zn2006
只看该作者 3楼 发表于: 2006-05-19
把你的代码分开在main.cpp .h .cpp 里面写试试
离线larryzhao

只看该作者 4楼 发表于: 2006-05-22
自己顶,高手们来帮帮我呀...谢谢了,我实在不知道错在哪里了...
离线shiroki

只看该作者 5楼 发表于: 2006-05-22
你在加了Q_OBJECT之后要重新生成makefile,还要make clean;make才行
--
shiro is White
ki is tree
http://www.cuteqt.com
论坛 http://www.cuteqt.com/bbs
博客 http://www.cuteqt.com/blog
博客镜像: http://sites.cuteqt.com/cuteqt
Linux/Qt/嵌入式讨论群 http://qun.qq.com/air/5699823
离线larryzhao

只看该作者 6楼 发表于: 2006-05-23
楼上的斑斑,我没有懂你的意思,我没有自己写过makefile所以听不懂...我是在用KDevelop KDE/C++在学习,是automake的,我看了你的帖子以后就再执行了一次automake然后再编译还是会出现我主贴里的错误,这到底是为什么呢?

我如果注释掉Q_OBJECT,程序就能过能运行,不过slot是没有用了...

斑竹救救我噢~~
离线larryzhao

只看该作者 7楼 发表于: 2006-05-23
实际测试后,发现zn2006朋友说的对的,我把threeWidget类按照规范写在.h, .cpp里面以后就正确了....

感谢zn2006和以上各位关心我问题的朋友, 谢谢了
快速回复
限100 字节
 
上一个 下一个