• 5806阅读
  • 4回复

【提问】为什么无法执行connect ????? [复制链接]

上一主题 下一主题
离线xiaoyuz
 
只看楼主 倒序阅读 楼主  发表于: 2006-01-20
我先从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);
 
}
[ 此贴被XChinux在2006-01-20 23:51重新编辑 ]
离线tigerchariot
只看该作者 1楼 发表于: 2006-09-05
我也碰到了这个问题,到底是什么原因呀
离线ediwon
只看该作者 2楼 发表于: 2006-09-05
我拿你代码测试了一下.
原因是:
你的主函数中定义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();
}
BLOG:
http://www.ediwon.com
-------------------------------
离线tigerchariot
只看该作者 3楼 发表于: 2006-09-06
我试了试,如果在继承的类当中改成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();
}
离线xiaoyuz
只看该作者 4楼 发表于: 2006-11-29
谢谢楼上的两位~
快速回复
限100 字节
 
上一个 下一个