Reputation: 393
I have an application using a QSerialPort, the very last thing my program does is call my closePort function which looks like this:
//Closes the port when we're done with it
void SerialCommSession::closePort()
{
connected = false;
if(m_port.isOpen())
{
qDebug() << "closing port";
m_port.close();
}
}
Some amount of the time it works fine, and the program starts up again without any issue. But maybe 75% of the time, when I try to run the program again it fails to open the port, returning error code 2. How long should QSerialPort.close() take to execute? And how can I ensure it completes?
Upvotes: 2
Views: 176
Reputation: 4367
Make your closePort
function a slot and connect it to QCoreApplication::aboutToQuit
:
connect(qApp, &QCoreApplication::aboutToQuit, someCommSessionPointer, &SerialCommSession::closePort)
When the event loop exits and before the application quits, your slot will be invoked. Make sure you have included <QCoreApplication>
or one of its derived classes in order for the qApp
macro to work.
Also:
How long should QSerialPort.close() take to execute? And how can I ensure it completes?
Slots in the same thread are invoked synchronously, so it can take as long as it needs to before control returns to the application. The application won't quit until your slot returns.
Upvotes: 1