'Qt Connect Signal with parameter in Lambda Function
I am new to Qt, I am working with lambda functions. I have a serial port object Serial. That I am reading and writing and also I want to do operations when it's ready to read or when bytes are written.
This works perfectly for readyRead signal:
QObject::connect(Serial, &QSerialPort::readyRead, [=]() {
/* DO SOMETHING 1*/
}
My problem is that this doesn't work, even when I know that I received for sure a signal bytesWritten. I never go inside /* DO SOMETHING 2*/ section.
QObject::connect(Serial, &QSerialPort::bytesWritten, [=]() {
/* DO SOMETHING 2*/
});
Did I do a mistake? I also noticed that there is a difference between the two signals bytesWritten and readyRead in syntax as shown next.
void QIODevice::readyRead()
void QIODevice::bytesWritten(qint64 bytes)
Maybe it has something to do with the fact that bytesWritten has a parameter that I didn't add (didn't know where to add).
Thank you for your help if you notice any problem!
Solution 1:[1]
Your lambda needs to have the same signature as the signal it's connecting to. So yes, you need to add the parameter. To do that, simply pass it in the parentheses:
QObject::connect(Serial, &QSerialPort::bytesWritten, [=](qint64 bytes) {
/* DO SOMETHING 2*/
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JarMan |
