connect(m_tcpSocket,&QUdpSocket::readyRead,this, &Connect::on_receiveUDP);
connect(m_udpSocket,SIGNAL(readyRead()),this,SLOT(on_receiveUDP()));
這兩種按說效果是一樣的,但是上面的就不能出發槽函式運行,下面的可以,這是怎么回事呢?
uj5u.com熱心網友回復:
connect(m_tcpSocket,&QUdpSocket::readyRead,this, &Connect::on_receiveUDP);m_tcpSocket是一個QTcpSocket物件,當然找不到QUdpSocket::readyRead這個函式指標了。
改成如下試試:
connect(m_tcpSocket,&QTcpSocket::readyRead,this, &Connect::on_receiveUDP);
uj5u.com熱心網友回復:
這兩種方式是有區別的。你可以去看看相關技術檔案。上邊使用函式指標模式模式要求信號和槽的引數嚴格匹配!而使用宏定義的方式允許信號和槽引數不嚴格匹配。舉個簡單例子:按鈕的clicked信號其實是有一個引數的,但是如果你使用宏的信號槽連接,你可以定義一個無引數的slot函式,并連接成功。所以雖然你上邊的寫法看上去也對,其實信號和槽的引數不匹配uj5u.com熱心網友回復:
請參考如下技術檔案:Differences between String-Based and Functor-Based Connections
From Qt 5.0 onwards, Qt offers two different ways to write signal-slot connections in C++: The string-based connection syntax and the functor-based connection syntax. There are pros and cons to both syntaxes. The table below summarizes their differences.
String-based
Functor-based
Type checking is done at...
Run-time
Compile-time
Can perform implicit type conversions
Y
Can connect signals to lambda expressions
Y
Can connect signals to slots which have more arguments than the signal (using default parameters)
Y
Can connect C++ functions to QML functions
Y
The following sections explain these differences in detail and demonstrate how to use the features unique to each connection syntax.
Type Checking and Implicit Type Conversions
String-based connections type-check by comparing strings at run-time. There are three limitations with this approach:
Connection errors can only be detected after the program has started running.
Implicit conversions cannot be done between signals and slots.
Typedefs and namespaces cannot be resolved.
Limitations 2 and 3 exist because the string comparator does not have access to C++ type information, so it relies on exact string matching.
In contrast, functor-based connections are checked by the compiler. The compiler catches errors at compile-time, enables implicit conversions between compatible types, and recognizes different names of the same type.
For example, only the functor-based syntax can be used to connect a signal that carries an int to a slot that accepts a double. A QSlider holds an int value while a QDoubleSpinBox holds a double value. The following snippet shows how to keep them in sync:
uj5u.com熱心網友回復:
贊成二樓,connect函式第一引數是QTcpSocket,那么第二個引數應該是QTcpSocket::readyRead()uj5u.com熱心網友回復:
應該是不小心寫錯了轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/106870.html
標籤:Qt
