Important: Please read the Qt Code of Conduct -

[SOLVED] QSerialPort blocked by GUI

  • Hi,

    i'm developing a GUI application with a serial communication.
    The program has to send data to a device connected to the serial port every 500ms. (keepalive)
    So i created a new class with a QTimer inside which calls a slot when its timeout() signal occurs.
    I move this class to a new thread (moveToThread()) to have it decoupled from the GUI thread.
    In the slot that is called from the QTimer i'm sending my data. This works as designed as long as i don't move my GUI window.

    As soon as i click and hold the GUI window to move it the communication stops for the time i'm holding the mouse button. Like this the user could "move" the window for a very long time and disturb the communication.

    What am I doing wrong? Am I using the QThread in a wrong way?
    Thank you.

    class SerialWorker : public QObject {

    SerialWorker(QObject * ObjectToTriggerOnTimer, qint16 CallTime);
    void SendString(void);
    QSerialPort SerialPort;
    public slots:
    void OnReceiveTimer(void);
    void finished();
    QTimer * ReceiveTimer;



    SerialWorker::SerialWorker() {
    this->ReceiveTimer = new QTimer(this);
    connect(ReceiveTimer, SIGNAL(timeout()), this, SLOT(OnReceiveTimer()));



    void SerialWorker::SendString(void){
    char data[]={1,2,3};

  • First of all:

    Are you sure it is QtSerialPort specific? Have you tried with general QIODevice like writing a file periodically, etc, or even dropping QIODevice, but do something else?

    Also, does it happen on a particular OS, or it is a cross-platform behavior you see? Do you use Qt 4 or 5, etc? Much more information is needed, sorry. :-)

  • Sorry, i totally forgot about that:
    I'm using Qt 5.1.1 on Windows 7 Prof 64bit

    I don't know if it is QSerialPort specific. I have not tried it yet with a QIODevice because my first thought was i'm doing something wrong with the QThread.

  • Well, that is why I would try to get rid of QtSerialPort, and try a generic Qt worker thread example to see if the problem persists. If not, try adding QIODevice through QFile for instance, if that does not reproduce the issue, you can add QtSerialPort. At least, that is how I would track this issue down without further clue. Also, I would try Linux and Mac as well just in case. It is a bit hard to proceed without localizing the issue. Furthermore, does it also happen for reading? Also, I am not sure why you have used moveToThread() for this scenario. Could you please comment on that?

  • Why not use moveToThread? I thought this would be a correct way in Qt.

    But i just saw i use the moveToThread()/creation of the Thread wrong. I'm creating my QTimer within the constructor, it doesn't work that way.

  • Qt Champions 2020

    Seems, this problem is similar to:

  • Thanks kuzulls (again). I will check this tomorrow, the main problem is i didn't know how to work with threads correctly. I tried it with QSerialPort in the worker class and outside in the parent but always in the wrong way (creating the QTimer in the constructor of the worker, therefore the QTimer wasn't running in his own thread. It was still running in the parent thread). Shame on me.

  • Hi,

    moving the serialport correctly to a own QThread solved my problem or most of my problem. I now can reseize and move the window of my application as long as i want and it still sends data.

    Another problem I found in this context is the following:
    In Windows7 having classic style (Win2000 style) activated and checked the option "Animate windows when minimizing and maximizing" in the "Performance Options" the whole system will freeze while doing this animation. So running my program and minimizing or maximizing any (even of other processes) window will result in blocking my program. (it also seems to block the whole system)

    I think this has nothing to do with Qt at all, this is OS related :-(

Log in to reply