qt5:使用static_cast连接过载信号和插槽功能

时间:2016-05-30 13:11:23

标签: qt5 connect signals-slots overloading static-cast

我的环境:Qt5.5 + QtCreator3.5 + OSX10.11

我知道qt5和qt4中函数connect的语法不同,查找Document以了解如何在qt5中使用connect()来处理重载信号/槽函数。

我按照建议做,但它仍然无法正常工作。

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

我已经搜索了对我没用的其他问题。怎么解决?您的建议将不胜感激。

other question link1

other question link2

1 个答案:

答案 0 :(得分:0)

你无法抛弃&#34; const&#34;使用static_cast的信号。尝试删除&#34; const&#34;信号背后。

通常信号不需要是const,因为它们通常以非常量方法发出。