Qt5 uses function pointers to differentiate Signals

Source: Internet
Author: User

The connection between the CITIC number and the slot in Qt5 has some changes to Qt4.

The connection method in Qt4 is similar to this:

QObject::connect(const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoConnection)

Qt5 is like this:

QObject::connect(const QObject * sender, PointerToMemberFunction signal, const QObject * receiver, PointerToMemberFunction method, Qt::ConnectionType type)

In Qt5, class names are used directly to use signal/slot functions.

Assume that two signals are defined in my class:

signals:                 void mysignal(int iNum);           void mysignal(int iNum, const QString& str);

We can see that these two signal functions are actually overload functions.

In Qt4, this does not cause any problems:

connect(this, SIGNAL(mysignal(int)), this, SLOT(mySlot()));connect(this, SIGNAL(mysignal(int, QString)), this, SLOT(mySlot()));

He can specify which signal we send.

But in Qt5, it is different:

connect(this, &XXX::mysignal, this, &XXX::mySlot);

In this way, what we send is

mysignal(int iNum);

Or

mysignal(int iNum, const QString& str);

This will lead to ambiguity. How can we solve this problem?

We can use the function pointer to differentiate the two signal functions:

void (XXX::*mynewsignal) (int iNum, const QString& str) = &XXX::mysignal;

Call again:

connect(this, &XXX::mysignal, this, &XXX::mySlot);connect(this, &XXX::mynewsignal, this, &XXX::mySlot);

In this way, we can identify which signal we send.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.