我的第一个Qt小程序,帮忙看看。。。

软件和网站开发以及相关技术探讨
回复
头像
openware
帖子: 173
注册时间: 2009-02-22 14:15
联系:

我的第一个Qt小程序,帮忙看看。。。

#1

帖子 openware » 2009-09-18 16:45

代码: 全选

#include <iostream>
#include <QtCore>
using namespace std;


class Employee : public QObject
{
	Q_OBJECT
public:
	Employee()
	{
		mySalary = 0;
		mySalary2 = 0;
		connect(this,SIGNAL(salaryChanged(int)),this,SLOT(setSalary(int)));
	}
	int salary() const
	{
		return mySalary;
	}
public slots:
	void setSalary(int newSalary);

signals:
	void salaryChanged(int newSalary);

private:
	int mySalary;
	int mySalary2;

};

void Employee::salaryChanged(int newSalary)
{
	mySalary = newSalary;
}

void Employee::setSalary(int newSalary)
{
	mySalary2 = newSalary;
}

int main()
{	cout << "begin" << endl;
	Employee *em = new Employee();
        //ToDO
	cout << "begin" << endl;
	return 0;
}

加上那个Q_OBJECT宏后,编译通不过,错误信息为 undefined reference to `vtable for Employee'
去掉Q_OBJECT后能通过编译,但运行时出错,错误信息为 Object::connect: No such signal QObject::salaryChanged(int)

请问如何解决这个问题啊?
Desire Freedom, Love Beauty, Keep Hacking。
头像
wangdu2002
帖子: 13284
注册时间: 2008-12-13 19:39
来自: 物华天宝人杰地灵

Re: 我的第一个Qt小程序,帮忙看看。。。

#2

帖子 wangdu2002 » 2009-09-18 16:52

也许包含的头文件不全,也许类的定义和继承有问题,程序的结构也有问题。类的定义不要和主程序混淆。找几本Qt的教程好好钻研下,先按照教材的例程来搞。Qt编程到Qt中文论坛更专业些。
行到水穷处,坐看云起时。
海内生明月,天涯共此夕。
--------------------吾本独!
头像
ChloeRei
帖子: 1021
注册时间: 2007-11-23 9:49

Re: 我的第一个Qt小程序,帮忙看看。。。

#3

帖子 ChloeRei » 2009-09-19 7:41

编译用的指令是什么
头像
comfanter
帖子: 190
注册时间: 2009-05-02 14:51

Re: 我的第一个Qt小程序,帮忙看看。。。

#4

帖子 comfanter » 2009-09-19 8:11

这叫Qt程序吗? :em06
你先把最简单的hello world搞定吧! :em04
Qt自带的教程很丰富,很全面,建议多看看
#include<QtGui>

代码: 全选

int main(int argc,char *argv[])
{
    QApplication app(argc,argv);
    QLabel hello("Hello Qt!");
	hello.resize(400,200);	
    hello.show();
    return app.exec();
}
头像
openware
帖子: 173
注册时间: 2009-02-22 14:15
联系:

Re: 我的第一个Qt小程序,帮忙看看。。。

#5

帖子 openware » 2009-09-19 9:37

ChloeRei 写了:编译用的指令是什么
我用的是eclipse,编译Hello world能成,但这个就不行了。。。。
Desire Freedom, Love Beauty, Keep Hacking。
头像
openware
帖子: 173
注册时间: 2009-02-22 14:15
联系:

Re: 我的第一个Qt小程序,帮忙看看。。。

#6

帖子 openware » 2009-09-19 9:38

comfanter 写了:这叫Qt程序吗? :em06
你先把最简单的hello world搞定吧! :em04
Qt自带的教程很丰富,很全面,建议多看看
#include<QtGui>

代码: 全选

int main(int argc,char *argv[])
{
    QApplication app(argc,argv);
    QLabel hello("Hello Qt!");
	hello.resize(400,200);	
    hello.show();
    return app.exec();
}
我只是想深入理解一下,信号和槽的机制,在Qt里面如果连这个都搞不明白的话还是别再Qt里混了。。
Desire Freedom, Love Beauty, Keep Hacking。
头像
openware
帖子: 173
注册时间: 2009-02-22 14:15
联系:

Re: 我的第一个Qt小程序,帮忙看看。。。

#7

帖子 openware » 2009-09-19 15:17

折腾了一天终于搞定了!!!
终于对信号和槽有点了解了。
先贴一下代码吧:

代码: 全选

//employee.h
#include <QtCore>
#include <QApplication>
#include <iostream>
#include <QWidget>
using namespace std;

class Employee:public QObject
{
 Q_OBJECT
public:
    Employee();
    int getValue()
    {
        return value;
    }
    void peek();        //peek the private value
    void changeValue(int); //changeValue by manual and then ruslt in emiting  a signal
public slots:         //if valueChanged then setValue
    void setValue(int);
signals:
    void valueChanged(int);
private:
    int value;
};

代码: 全选

//employee.cpp
#include "employee.h"
Employee::Employee()
{
    value = 0;
    cout<<"the initial value is 0"<<endl;
    QObject::connect(this,SIGNAL(valueChanged(int)),this,SLOT(setValue(int)));
}

void Employee::changeValue(int num)
{
    if(value != num)
    {
        emit valueChanged(num);
    }
}

void Employee::setValue(int newValue)
{
    value = newValue;
}

void Employee::peek()
{
    cout<<"The value you have peeked is "<<value<<endl;
}

代码: 全选


//main.cpp
#include "employee.h"
#include <QApplication>
int main (int argc, char * argv[])
{
    QApplication app(argc, argv);
    cout<<"begin"<<endl;
    Employee em;
    cout<<em.getValue()<<endl;
    em.changeValue(6);
    cout<<em.getValue()<<endl;
    cout<<"end"<<endl;
    return  app.exec();
}
注意:
1.这三个文件不能合成一个文件,否则会出现一大堆“Undefine reference to XX ”之类的错误。我在这儿试过N次,也在这儿栽过N次 :em06 ,这儿给自己提个醒,也希望能引起大家的注意 :em02
至于原因我想因为编译时,要用moc工具对信号和槽的filename.h头文件中的宏进行处理,生成moc_filename.cpp文件,如果搞到一块的话moc就找不到filename.h文件了,就算找到了也会搞的一团糟。所以还是分开写比较好,既便于管理,又美观,还省去了不少烦人的错误。。。
2.如果要用Qt里的信号和槽机制的话,该类一定要继承QOject类,或是其子类,而且必须加上Q_OBJECT宏。原话是这么说的“All classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObject.”
3.在命令行下的话别忘了及时更新Makefile文件。否则会原地踏步,浪费时间和精力 :em02 。某些IDE可以自动更新,如果遇到错误又总是发现不了的话,可以到命令行下手动更新。。。
Desire Freedom, Love Beauty, Keep Hacking。
dbzhang800
帖子: 3182
注册时间: 2006-03-10 15:10
来自: xi'an China
联系:

Re: 我的第一个Qt小程序,帮忙看看。。。

#8

帖子 dbzhang800 » 2009-09-20 8:46

顺便提一下,这三个文件合到一块是没有问题的,只是需要稍微处理一下。

比如说你的这个文件 hello.cpp

那么你应该在文件的合适位置(比如说在main函数的前面)加入一行 #include "hello.moc"
(因为moc对hello.cpp中的宏进行处理后生成 helllo.moc ,而qmake不会自动将该文件包含进去)
头像
openware
帖子: 173
注册时间: 2009-02-22 14:15
联系:

Re: 我的第一个Qt小程序,帮忙看看。。。

#9

帖子 openware » 2009-09-21 22:25

dbzhang800 写了:顺便提一下,这三个文件合到一块是没有问题的,只是需要稍微处理一下。

比如说你的这个文件 hello.cpp

那么你应该在文件的合适位置(比如说在main函数的前面)加入一行 #include "hello.moc"
(因为moc对hello.cpp中的宏进行处理后生成 helllo.moc ,而qmake不会自动将该文件包含进去)
谢谢你的指教。不过我试了一下,还是没有通过,moc对处理之后应该生成moc_****.cpp文件吧,我在目录里发现
了这个。。。没有发现*.moc。
Desire Freedom, Love Beauty, Keep Hacking。
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

Re: 我的第一个Qt小程序,帮忙看看。。。

#10

帖子 BigSnake.NET » 2009-09-21 22:27

openware 写了:
ChloeRei 写了:编译用的指令是什么
我用的是eclipse,编译Hello world能成,但这个就不行了。。。。
用 qmake ..
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
回复