• 9817阅读
  • 13回复

connect是否只能在构造函数内定义 [复制链接]

上一主题 下一主题
离线newdc
 

只看楼主 倒序阅读 楼主  发表于: 2008-09-23
— 本帖被 XChinux 执行加亮操作(2008-09-23) —
connect是否只能在构造函数内定义?
离线破烂石头
只看该作者 1楼 发表于: 2008-09-23
不是
离线sbtree
只看该作者 2楼 发表于: 2008-09-23
引用楼主newdc于2008-09-23 19:37发表的 connect是否只能在构造函数内定义 :
connect是否只能在构造函数内定义?

你是想问"connect是否只能在构造函数内被调用"吧?
windows 7 + VC++2008 + Qt4.5.2
离线newdc

只看该作者 3楼 发表于: 2008-09-24
#include <QApplication>
#include "f1.h"
#include "f2.h"
int main(int argc, char *argv[])
{
      QApplication app(argc, argv);
      Cf1 f1;
      Cf2 f2;
      f1.show();
      connect(f1.pushButton,SIGNAL(clicked()),f2,SLOT(show()));
      return app.exec();
}

cf1和cf2是采用多继承方式的两个自定义窗体,希望用cf1窗体中的按钮启动显示Cf2窗体,编译后出现如下错误。
main.cpp:32: error: 'connect' was not declared in this scope
离线sbtree
只看该作者 4楼 发表于: 2008-09-24
引用第3楼newdc于2008-09-24 00:03发表的  :
#include <QApplication>
#include "f1.h"
#include "f2.h"
int main(int argc, char *argv[])
{
.......

这样调用
QObject::connect(f1.pushButton,SIGNAL(clicked()),f2,SLOT(show()));
windows 7 + VC++2008 + Qt4.5.2
离线newdc

只看该作者 5楼 发表于: 2008-09-24
main.cpp:32: error: no matching function for call to 'QObject::connect(QPushButton*&, const char [11], Cf2&, const char [8])'
/usr/local/Trolltech/QtEmbedded-4.4.1/include/QtCore/qobject.h:198: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
/usr/local/Trolltech/QtEmbedded-4.4.1/include/QtCore/qobject.h:303: note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
make: *** [main.o] Error 1
make: Target `first' not remade because of errors.
*** 退出状态:2 ***
可以了,不过又出现新的错误,呵呵,头好大。
离线sbtree
只看该作者 6楼 发表于: 2008-09-24
再试一试,用指针调用,不是引用或值传递
QObject::connect(&(f1.pushButton),SIGNAL(clicked()),&f2,SLOT(show()));
windows 7 + VC++2008 + Qt4.5.2
离线newdc

只看该作者 7楼 发表于: 2008-09-24
main.cpp:32: error: no matching function for call to 'QObject::connect(QPushButton**, const char [11], Cf2*, const char [8])'
/usr/local/Trolltech/QtEmbedded-4.4.1/include/QtCore/qobject.h:198: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
/usr/local/Trolltech/QtEmbedded-4.4.1/include/QtCore/qobject.h:303: note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
make: *** [main.o] Error 1
make: Target `first' not remade because of errors.
*** 退出状态:2 ***
好像错误是一样的
离线sbtree
只看该作者 8楼 发表于: 2008-09-24
再改
QObject::connect(f1.pushButton,SIGNAL(clicked()),&f2,SLOT(show()));
windows 7 + VC++2008 + Qt4.5.2
离线newdc

只看该作者 9楼 发表于: 2008-09-24
呵呵,谢谢sbtree,搞定了,实在太高兴了,不过不太明白为什么。另怎样根据错误提示来判断呢?
[ 此贴被newdc在2008-09-24 10:11重新编辑 ]
离线newdc

只看该作者 10楼 发表于: 2008-09-24
呵呵,谢谢,搞定了,实在太高兴了,不过不太明白为什么。另怎样根据错误提示来判断呢?
离线破烂石头
只看该作者 11楼 发表于: 2008-09-24
main.cpp:32: error: no matching function for call to 'QObject::connect(QPushButton**, const char [11], Cf2*, const char [8])'
这句是说没有相匹配的函数,即函数形参与实参不匹配
/usr/local/Trolltech/QtEmbedded-4.4.1/include/QtCore/qobject.h:198: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
/usr/local/Trolltech/QtEmbedded-4.4.1/include/QtCore/qobject.h:303: note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
这两句是编译器提供的它找到的函数声明
connect是QObject中的一个静态方法,一般的调用为connect(pobject1, SIGNAL(sig(arg)), pobject2, SLOT(slot(arg)));
pobject1, pobject2必须是QObject的指针(也可以是继承了QObject的子类)。
离线newdc

只看该作者 12楼 发表于: 2008-09-24
引用第11楼破烂石头于2008-09-24 15:13发表的  :
main.cpp:32: error: no matching function for call to 'QObject::connect(QPushButton**, const char [11], Cf2*, const char [8])'
这句是说没有相匹配的函数,即函数形参与实参不匹配
/usr/local/Trolltech/QtEmbedded-4.4.1/include/QtCore/qobject.h:198: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
/usr/local/Trolltech/QtEmbedded-4.4.1/include/QtCore/qobject.h:303: note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
这两句是编译器提供的它找到的函数声明
.......

了解
离线hzxthzxt

只看该作者 13楼 发表于: 2008-09-25
不是
adaddddddddddddddddddddddddddssssssssssweeeeeaaaaaaaaaaafdaaaaaaaaaaaaaaaffff
快速回复
限100 字节
 
上一个 下一个