QtSerialPort instantiates in wrong thread?
I've got a project I'm working on that needs to read data from the COM port of a particular device. The QtSerialPort project (http://qt-project.org/wiki/QtSerialPort) provides examples, and these examples work properly with the COM device, but it fails to get data in my own application.
The application structure which leads to the COM port being instantiated looks like the following:
- main.cpp called
- MainView instantiated
- HardwareManager instantiated (Inside the MainView Constructor)
- QSerialPort instantiated (Inside the HardwareManager Constructor)
When I create the serial port object, I set all appropriate settings (baud rate, parity, etc.), and can open the port without error. However, the serial port object never properly fires the signal, so my slot never picks up the "readyRead" event. I think it might be a threading issue, because of the following.
I logged the Thread ID of the thread that instantiates the HardwareManager, the thread the HardwareManager is in, and the QSerialPort thread, and the results were:
CALL_THREAD => QThread(0xbf8dbe0) // MainView thread
THIS_THREAD => QThread(0xbf8dbe0) // HardwareManager thread
COM3_THREAD => QThread(0xbfb95f0) // SerialPort thread
As you can see, for some reason, my SerialPort is instantiated in a different thread. When I dumpObjectInfo on the serial port instance, it shows a connection to the proper slot, but the event is never fired. "This blog post":http://huntharo.com/2009/08/qthread-signalsslots-why-your-calls-stay-in-the-main-thread/ talks about a similar issue of cross-thread signals/slots.
Does anyone have any insight as to why my QSerialPort is opened in a different thread? As well as how I might remedy this issue?
So somewhere something you are doing wrong.
Why you create a port in another thread?
To do this, there is a reason?
You lack of performance?
Give the code you use. Otherwise, no one will be able to help you,
because there is no telepaths.
I'm not creating the port in another thread, I don't know why it's being instantiated in that thread. The code that creates my serial port and sets up signals/slots is below:
@ this->serial = new QSerialPort();
connect(this->serial, SIGNAL(readyRead()), this, SLOT(readSerialData()));@
This code happens directly in the constructor of HardwareManager. Why is my QSerialPort in a different thread than the parent object?
How you print thread id's?
But is better - give minimal example (with qDebug() ) that reproduced problem.