标题:【提问】为什么无法执行connect ?????
作者:xiaoyuz
日期:2006-01-20 21:28
内容:
我先从QWidget派生了my2base,然后再从my2base派生my2
在my2中建立了一个PushButton到copy()的连接,但在编译时出错:
g++ -c -pipe -DQWS -fno-exceptions -fno-rtti -Wall -W -O2 -DNO_DEBUG -I/friendly-arm/x86-qtopia/qt/include -I/friendly-arm/x86-qtopia/qtopia/include -o my2.o my2.cpp
my2.cpp: In constructor `my2::my2(QWidget*, const char*, unsigned int)':
my2.cpp:8: no matching function for call to `my2::connect(QPushButton*&, const
char[11], QObject*, const char[8])'
/friendly-arm/x86-qtopia/qt/include/qobject.h:110: candidates are: static bool
QObject::connect(const QObject*, const char*, const QObject*, const char*)
/friendly-arm/x86-qtopia/qt/include/qobject.h:210: bool
QObject::connect(const QObject*, const char*, const char*) const
make: *** Error 1
源代码如下,my2base.h 和my2base.cpp都是designer自动生成的
-------------------------------------------------------------------------------------------------------------
my2base.h
/****************************************************************************
** Form interface generated from reading ui file 'my2base.ui'
**
** Created: Fri Jan 20 20:57:41 2006
** by:The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#ifndef MY2BASE_H
#define MY2BASE_H
#include
#include
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QLineEdit;
class QPushButton;
class my2base : public QWidget
{
Q_OBJECT
public:
my2base( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
~my2base();
QLineEdit* LineEdit1;
QLineEdit* LineEdit2;
QPushButton* PushButton1;
};
#endif // MY2BASE_H
--------------------------------------------------------------------------------------------------------
下面是my2base ..
#1 [tigerchariot 09-05 14:56]
我也碰到了这个问题,到底是什么原因呀
#2 [ediwon 09-05 17:11]
我拿你代码测试了一下.
原因是:
你的主函数中定义my2对像时一定没给参数造成调用了my2的无参构造函数.而无参构造函数没有执行:
connect( PushButton1 , SIGNAL( clicked() ), (QObject*)this, SLOT( copy() ) );
所以你的主函数写成以下就行了:
#include "my2.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
my2 m(0, "hehe", 0); //the parameters is very important
m.show();
return app.exec();
}
#3 [tigerchariot 09-06 09:03]
我试了试,如果在继承的类当中改成connect((QObject*) PushButton1 , SIGNAL( clicked() ), (QObject*)this, SLOT( copy() ) ); 也是可以的
并且在通过uic生成的基类当中(如上面的my2base类)调用connect( PushButton1 , SIGNAL( clicked() ), (QObject*)this, SLOT( copy() ) ); 是没有问题的。
在我的程序当中,集成类是这样写的,其中Form1是通过uic生成的类
class MyForm:public Form1
{
Q_OBJECT
public:
MyForm( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 );
~MyForm();
};
#include
#include "mymainfrm.h"
MyForm::MyForm( QWidget* parent,const char* name, bool modal, WFlags fl )
: Form1( parent, name, modal, fl )
{
//这样编译不能通过
//QObject::connect(startnet, SIGNAL( clicked() ), qApp,SLOT(quit() ) );
//这样编译可以通过
QObject::connect((const QObject*)startnet, SIGNAL( clicked() ), qApp,SLOT(quit() ) );
}
/*
*Destroys the object and frees any allocated resources
*/
MyForm::~MyForm()
{
// no need to delete child widgets, Qt does it all for us
}
#include
#include "mymainfrm.h"
int main(int argc, char **argv)
{
QApplication a(argc, argv);
MyForm *myform = new MyForm;
a.setMainWidget(myform);
myform->show();
return a.exec();
}
#4 [xiaoyuz 11-29 13:30]
谢谢楼上的两位~