Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort and QThread

QSerialPort and QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 3.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sergio
    wrote on last edited by Sergio
    #1

    I have the main MainWindow class that has instances of the class QThread and another is in charge of communications through the serial port. In the main class I initialize instances in .h as :

    Communications * com ;
    QThread * thread ;

    And in the constructor of the main class .cpp the initialize :

    com = new Comunications();
    thread = new QThread(this);

    I shake my communications class I created the thread above with
    com- > moveToThread (thread) ;

    Also in this class i have the connections of signals and slots.
    When I go to write to the serial port jumps me an error:

    " QObject : Can not create children for a parent That is in a different thread. "

    Can someone help me??

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yoavmil
      wrote on last edited by
      #2

      yeah, happend to me too.
      the thing is, the any QObject 'knows' who are his childred, so the QSerialPort is alreay listed at the thread that created it, and you are trying to change it.
      try reordering the calls. first moveToThread(), then com = new Comunications();

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        @yoavmil Are you trying to suggest to call moveToThread on an unallocated object ?

        @Sergio I guess your Communications class contains a QSerialPort ? If so, did you give a parent to your QSerialPort instance (i.e. _serialPort = new QSerialPort(this))? From the error message, probably not, hence it's not moved along with your Communications instance.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • TheBadgerT Offline
          TheBadgerT Offline
          TheBadger
          wrote on last edited by TheBadger
          #4

          @Sergio

          To follow up on what @SGaist said, what I normally do is not to create the serial port in the constructor of the Comunications class (assuming you do), but connect a slot in Communications to the QThread::started() signal, and in there create the serial port. That would ensure that when the serial port class is created that it will be created in the thread that needs to use it.

          class Comunications : public QObject {
          public slots:
              void threadStarted() { 
                  d_serialPort = new QSerialPort();
                  // ... <- set up
                  d_serialPort.open();
              }
              void threadStopped() { 
                  d_serialPort.close(); // Might not work/be needed
                  delete d_serialPort;
                  d_serialPort = nullptr;
              }
          private:
              QSerialPort* d_serialPort;
          }; 
          

          and then in your constructor

          com = new Comunications();
          thread = new QThread(this);
          connect(thread, &QThread::started, com, &Comunications::threadStarted);
          thread->start();
          

          As @SGaist pointed out, make sure to clean up objects created:

          connect(thread, &QThread::finished, com, &Comunications::threadStopped);
          

          PS: Please put your code into code blocks when creating posts
          [edit: added example code for cleanup]


          Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @TheBadger Do you also delete it when your thread stops ? Otherwise you have a memory leak here

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            TheBadgerT 1 Reply Last reply
            0
            • SGaistS SGaist

              @TheBadger Do you also delete it when your thread stops ? Otherwise you have a memory leak here

              TheBadgerT Offline
              TheBadgerT Offline
              TheBadger
              wrote on last edited by
              #6

              @SGaist

              you also delete...

              Yes definitely, I did not add that part but it should be deleted, will add a possible snippet to the code above, thanks


              Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved