qt5:用static_cast连接过载信号和槽函数
qt5: connect overload signal and slot function with static_cast
我的环境:Qt5.5 + QtCreator3.5 + OSX10.11
我知道qt5和qt4的函数connect的语法不一样,查了下文档了解如何在qt5中使用connect()来处理重载signal/slot函数
我按照提示做了,还是不行
newspaper.h
signals:
// overload function
void newPaper(const QString &name) const;
void newPaper(const QString &name, const QDate &date) const;
reader.h
//overload function
void receiveNewspaper(const QString &name) const
{
qDebug() << "overload(name): Receives Newspaper: " << name;
}
//overload function
void receiveNewspaper(const QString &name, const QDate &date) const
{
qDebug() << "overload(name, date): Receives Newspaper: " << name << "\tDate:" << date;
}
main.cpp
//connect for overload signal,slot function
QObject::connect(&newspaper,
static_cast<void (Newspaper:: *)(const QString &)>(&Newspaper::newPaper),
&reader,
static_cast<void (Reader:: *)(const QString &)>(&Reader::receiveNewspaper) );
但是报错如下
./2_16_SignalSlotDeep/main.cpp:16:22: error: address of overloaded function 'newPaper' cannot be static_cast to type 'void (Newspaper::*)(const QString &)'
static_cast<void (Newspaper::*)(const QString &)>(&Newspaper::newPaper),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../2_16_SignalSlotDeep/newspaper.h:28:10: note: candidate function
void newPaper(const QString &name) const;
^
../2_16_SignalSlotDeep/newspaper.h:29:10: note: candidate function
void newPaper(const QString &name, const QDate &date) const;
^
../2_16_SignalSlotDeep/main.cpp:18:22: error: address of overloaded function 'receiveNewspaper' cannot be static_cast to type 'void (Reader::*)(const QString &)'
static_cast<void (Reader::*)(const QString &)>(&Reader::receiveNewspaper) );
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../2_16_SignalSlotDeep/reader.h:16:10: note: candidate function
void receiveNewspaper(const QString &name) const
^
../2_16_SignalSlotDeep/reader.h:21:10: note: candidate function
void receiveNewspaper(const QString &name, const QDate &date) const
其他对我没有帮助的问题我已经搜索过了。如何解决?我们将不胜感激您的建议。
您不能使用 static_cast 丢弃信号的 "const"。尝试删除信号后面的 "const"。
通常信号不需要是常量,因为它们通常在非常量方法中发出。
我的环境:Qt5.5 + QtCreator3.5 + OSX10.11
我知道qt5和qt4的函数connect的语法不一样,查了下文档了解如何在qt5中使用connect()来处理重载signal/slot函数
我按照提示做了,还是不行
newspaper.h
signals:
// overload function
void newPaper(const QString &name) const;
void newPaper(const QString &name, const QDate &date) const;
reader.h
//overload function
void receiveNewspaper(const QString &name) const
{
qDebug() << "overload(name): Receives Newspaper: " << name;
}
//overload function
void receiveNewspaper(const QString &name, const QDate &date) const
{
qDebug() << "overload(name, date): Receives Newspaper: " << name << "\tDate:" << date;
}
main.cpp
//connect for overload signal,slot function
QObject::connect(&newspaper,
static_cast<void (Newspaper:: *)(const QString &)>(&Newspaper::newPaper),
&reader,
static_cast<void (Reader:: *)(const QString &)>(&Reader::receiveNewspaper) );
但是报错如下
./2_16_SignalSlotDeep/main.cpp:16:22: error: address of overloaded function 'newPaper' cannot be static_cast to type 'void (Newspaper::*)(const QString &)'
static_cast<void (Newspaper::*)(const QString &)>(&Newspaper::newPaper),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../2_16_SignalSlotDeep/newspaper.h:28:10: note: candidate function
void newPaper(const QString &name) const;
^
../2_16_SignalSlotDeep/newspaper.h:29:10: note: candidate function
void newPaper(const QString &name, const QDate &date) const;
^
../2_16_SignalSlotDeep/main.cpp:18:22: error: address of overloaded function 'receiveNewspaper' cannot be static_cast to type 'void (Reader::*)(const QString &)'
static_cast<void (Reader::*)(const QString &)>(&Reader::receiveNewspaper) );
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../2_16_SignalSlotDeep/reader.h:16:10: note: candidate function
void receiveNewspaper(const QString &name) const
^
../2_16_SignalSlotDeep/reader.h:21:10: note: candidate function
void receiveNewspaper(const QString &name, const QDate &date) const
其他对我没有帮助的问题我已经搜索过了。如何解决?我们将不胜感激您的建议。
您不能使用 static_cast 丢弃信号的 "const"。尝试删除信号后面的 "const"。
通常信号不需要是常量,因为它们通常在非常量方法中发出。