提问】为什么无法执行connect ?????
我先从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: *** [my2.o] 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 <qvariant.h>
#include <qwidget.h>
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.cpp
/**************************************************************************
** Form implementation 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!
****************************************************************************/
#include "my2base.h"
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
/*
* Constructs a my2base which is a child of 'parent', with the
* name 'name' and widget flags set to 'f'
*/
my2base::my2base( QWidget* parent, const char* name, WFlags fl )
: QWidget( parent, name, fl )
{
if ( !name )
setName( "my2base" );
resize( 255, 153 );
setCaption( tr( "my2" ) );
LineEdit1 = new QLineEdit( this, "LineEdit1" );
LineEdit1->setGeometry( QRect( 30, 60, 131, 25 ) );
LineEdit2 = new QLineEdit( this, "LineEdit2" );
LineEdit2->setGeometry( QRect( 30, 100, 131, 25 ) );
PushButton1 = new QPushButton( this, "PushButton1" );
PushButton1->setGeometry( QRect( 180, 60, 61, 51 ) );
PushButton1->setText( tr( "PushButton1" ) );
}
/*
* Destroys the object and frees any allocated resources
*/
my2base::~my2base()
{
// no need to delete child widgets, Qt does it all for us
}
--------------------------------------------------------------------------------------------------------
下面是my2.h
#include "my2base.h"
class my2 : public my2base
{
Q_OBJECT
public:
my2(){}
my2(QWidget*, const char* , WFlags);
~my2();
protected slots:
void copy();
};
--------------------------------------------------------------------------------------------------------
下央是my2.cpp
#include "my2.h"
#include<qlineedit.h>
my2::my2( QWidget* parent, const char* name, WFlags fl ):
my2base( parent, name, fl )
{
//signals and slots connections
connect( PushButton1 , SIGNAL( clicked() ), (QObject*)this, SLOT( copy() ) );
}
my2::~my2()
{}
void my2::copy()
{
//PushButton1->validateAndSet( x, 0, 0, 0);
QString x = LineEdit1->text();
LineEdit2->setText(x);
}
方法一:
我拿你代码测试了一下.
原因是:
你的主函数中定义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();
}
方法二:
我试了试,如果在继承的类当中改成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 <qapplication.h>
#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 <qapplication.h>
#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();
}
我用的第二种方法解决的,在第一个参数前面强制转化为( const QObject* )就能编译通过了
根据上面介绍,也解决了我自己的一个connect不成功问题
在qtopia里面,先用designer设计窗口,然后不动窗口,在cpp文件自己写信号与槽,以方便以后修改代码还要打开ui文件。
#include "main_form2.h"
MainForm::MainForm(QWidget * parent, const char * name, WFlags f):TMainFormMain(parent, name, f)
{
//QTextCodec *code=QTextCodec::codecForName("GBK")
//QString uniStr=code -> toUnicode("要显示的中文字符")
//Mywidget-> setFont(QFont("simsun",20,QFont::Bold))
//Mywidget-> setText(uniStr)
//setupUi(this);
connect ((const QObject*)CancelButton, SIGNAL(clicked()), this, SLOT(CancelButton_Clicked()));
//on_PushButton1_Clicked()
}
void MainForm::CancelButton_Clicked()
{
close();
}
在原本缺少(const QObject*)时,提示如下出错,
main_form2.cpp: In constructor 'MainForm::MainForm(QWidget*, const char*, uint)':
main_form2.cpp:13: error: no matching function for call to 'MainForm::connect(QPushButton*&, const char [11], MainForm* const, const char [24])'
/opt/FriendlyARM/mini2440/arm-qtopia/qtopia-2.2.0-FriendlyARM/qt2/include/qobject.h:110: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*)
/opt/FriendlyARM/mini2440/arm-qtopia/qtopia-2.2.0-FriendlyARM/qt2/include/qobject.h:209: note: bool QObject::connect(const QObject*, const char*, const char*) const