在 Qt 檔案中是這樣寫的:
注意:這是私人信號。它可以用于信號連接,但不能由用戶發射。
注意:激活的信號在這個類中被多載。為了使用函式指標語法連接到這個信號,Qt 提供了一個方便的助手來獲取函式指標,如下例所示:
connect(socketNotifier, QOverload<QSocketDescriptor, QSocketNotifier::Type>::of(&QSocketNotifier::activated),
[=](QSocketDescriptor socket, QSocketNotifier::Type type){ /* ... */ });
當我使用它時,我遇到了這個問題:
error: no matching function for call to 'of'
/usr/include/qt5/QtCore/qglobal.h:1224: candidate template ignored: could not match 'type-parameter-0-0 (QSocketDescriptor, QSocketNotifier::Type) const' against 'void (QSocketDescriptor, QSocketNotifier::Type, QSocketNotifier::QPrivateSignal)'
/usr/include/qt5/QtCore/qglobal.h:1212: candidate template ignored: failed template argument deduction
/usr/include/qt5/QtCore/qglobal.h:1241: candidate template ignored: could not match 'R (*)(QSocketDescriptor, QSocketNotifier::Type)' against 'void (QSocketNotifier::*)(QSocketDescriptor, QSocketNotifier::Type, QSocketNotifier::QPrivateSignal)'
我只是復制粘貼了這個例子,并且包含了<QSocketDescriptor>和<QSocketNotifier>標題。我錯過了什么?
uj5u.com熱心網友回復:
如果您可以使用 C 14,則可以使用輔助函式:
template<class T> auto privateOverload(void ( QSocketNotifier::* s)( QSocketDescriptor,QSocketNotifier::Type,T ) ){return s;}
然后你可以使用
QObject::connect(socketNotifier, privateOverload(&QSocketNotifier::activated),/*...*/);
如果您可以使用 C 20,您也可以直接在 connect 陳述句中使用 lambda:
QObject::connect(socketNotifier, []<class T>(void ( QSocketNotifier::* s)( QSocketDescriptor,QSocketNotifier::Type,T ) ){return s;}(&QSocketNotifier::activated),/*...*/);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/313885.html
