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. how to properly construct derived class...
QtWS25 Last Chance

how to properly construct derived class...

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 6 Posters 961 Views
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    ...so as to not get this message when using a method:

    QObject: Cannot create children for a parent that is in a different thread.
    

    My derived class:

    class SerialPort : public QSerialPort
    {
        Q_OBJECT
    
    private:
        QByteArray buffer;
    public:
        SerialPort();
    ...
    SerialPort::SerialPort()
    {
    ...
    

    Thanks...

    J.HilkJ jsulmJ 2 Replies Last reply
    0
    • mzimmersM mzimmers

      ...so as to not get this message when using a method:

      QObject: Cannot create children for a parent that is in a different thread.
      

      My derived class:

      class SerialPort : public QSerialPort
      {
          Q_OBJECT
      
      private:
          QByteArray buffer;
      public:
          SerialPort();
      ...
      SerialPort::SerialPort()
      {
      ...
      

      Thanks...

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @mzimmers
      all QObject derived classed (QSerialPort is one) should (need to) have a QObject *parent and a proper initialisation in your case:

      class SerialPort : public QSerialPort
      {
          Q_OBJECT
      
      private:
          QByteArray buffer;
      public:
          explicit SerialPort(QObject *parent = nullptr) : QSerialPort(parent) {
          
          }
      ...
      SerialPort::SerialPort()
      {
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      4
      • mzimmersM mzimmers

        ...so as to not get this message when using a method:

        QObject: Cannot create children for a parent that is in a different thread.
        

        My derived class:

        class SerialPort : public QSerialPort
        {
            Q_OBJECT
        
        private:
            QByteArray buffer;
        public:
            SerialPort();
        ...
        SerialPort::SerialPort()
        {
        ...
        

        Thanks...

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @mzimmers Don't pass an object as parent to the child if the parent was constructed in a different thread. This is what the warning is saying. Without seing the code it is impossible to say what exactly you're doing wrongly.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #4

          Thanks for the replies. J.Hilk: I've made the corrections you listed, and that seems to have fixed it. jsulm: the code that threw the error is:

          bool SerialPort::openComPort(const QString &portName)
          {
              if (portName != nullptr)
              {
                  if (isOpen())
                  {
                      close();  // in case we've opened a different one.
                  }
                  setPortName(portName);
                  rc = open(QIODevice::ReadWrite); // the error occurs here
          

          The SerialPort object is created in the worker object/thread. Seems like an odd error to get in this situation.

          J.HilkJ E 2 Replies Last reply
          0
          • mzimmersM mzimmers

            Thanks for the replies. J.Hilk: I've made the corrections you listed, and that seems to have fixed it. jsulm: the code that threw the error is:

            bool SerialPort::openComPort(const QString &portName)
            {
                if (portName != nullptr)
                {
                    if (isOpen())
                    {
                        close();  // in case we've opened a different one.
                    }
                    setPortName(portName);
                    rc = open(QIODevice::ReadWrite); // the error occurs here
            

            The SerialPort object is created in the worker object/thread. Seems like an odd error to get in this situation.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @mzimmers said in how to properly construct derived class...:

            Thanks for the replies. J.Hilk: I've made the corrections you listed, and that seems to have fixed it. jsulm: the code that threw the error is:

            bool SerialPort::openComPort(const QString &portName)
            {
                if (portName != nullptr)
                {
                    if (isOpen())
                    {
                        close();  // in case we've opened a different one.
                    }
                    setPortName(portName);
                    rc = open(QIODevice::ReadWrite); // the error occurs here
            

            The SerialPort object is created in the worker object/thread. Seems like an odd error to get in this situation.

            It‘s actually not, without a parent, the Serialport is created in the mainthread and not moved with ‚‘moveToThread‘ together with its parent.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            mzimmersM 1 Reply Last reply
            3
            • J.HilkJ J.Hilk

              @mzimmers said in how to properly construct derived class...:

              Thanks for the replies. J.Hilk: I've made the corrections you listed, and that seems to have fixed it. jsulm: the code that threw the error is:

              bool SerialPort::openComPort(const QString &portName)
              {
                  if (portName != nullptr)
                  {
                      if (isOpen())
                      {
                          close();  // in case we've opened a different one.
                      }
                      setPortName(portName);
                      rc = open(QIODevice::ReadWrite); // the error occurs here
              

              The SerialPort object is created in the worker object/thread. Seems like an odd error to get in this situation.

              It‘s actually not, without a parent, the Serialport is created in the mainthread and not moved with ‚‘moveToThread‘ together with its parent.

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #6

              @J.Hilk said in how to properly construct derived class...:

              @mzimmers said in how to properly construct derived class...:

              The SerialPort object is created in the worker object/thread. Seems like an odd error to get in this situation.

              It‘s actually not, without a parent, the Serialport is created in the mainthread and not moved with ‚‘moveToThread‘ together with its parent.

              Oh...I didn't realize that. Is that because the serialPort object is created in the c'tor of the worker? If the worker created the object after it had been moved to its own thread, then the object would exist within the new thread, correct?

              JKSHJ 1 Reply Last reply
              0
              • mzimmersM mzimmers

                Thanks for the replies. J.Hilk: I've made the corrections you listed, and that seems to have fixed it. jsulm: the code that threw the error is:

                bool SerialPort::openComPort(const QString &portName)
                {
                    if (portName != nullptr)
                    {
                        if (isOpen())
                        {
                            close();  // in case we've opened a different one.
                        }
                        setPortName(portName);
                        rc = open(QIODevice::ReadWrite); // the error occurs here
                

                The SerialPort object is created in the worker object/thread. Seems like an odd error to get in this situation.

                E Offline
                E Offline
                elfring
                wrote on last edited by
                #7
                bool SerialPort::openComPort(const QString &portName)
                {
                     if (portName != nullptr)
                

                I find this condition check suspicious for a reference input parameter.
                How do you think about to omit it?

                mzimmersM 1 Reply Last reply
                0
                • E elfring
                  bool SerialPort::openComPort(const QString &portName)
                  {
                       if (portName != nullptr)
                  

                  I find this condition check suspicious for a reference input parameter.
                  How do you think about to omit it?

                  mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #8

                  @elfring openComPort() is called from a slot that's responding to a change in a selection from a QComboBox. This is just a mild safeguard against something going wrong in the selection. If anything, I probably should check that it resolves to a valid port name.

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

                    Hi,

                    You should rather use !portName.isEmpty(). portName can't be a null pointer in any case.

                    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
                    1
                    • mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by
                      #10

                      Hi SGaist: noted...thanks.

                      1 Reply Last reply
                      0
                      • mzimmersM mzimmers

                        @J.Hilk said in how to properly construct derived class...:

                        @mzimmers said in how to properly construct derived class...:

                        The SerialPort object is created in the worker object/thread. Seems like an odd error to get in this situation.

                        It‘s actually not, without a parent, the Serialport is created in the mainthread and not moved with ‚‘moveToThread‘ together with its parent.

                        Oh...I didn't realize that. Is that because the serialPort object is created in the c'tor of the worker? If the worker created the object after it had been moved to its own thread, then the object would exist within the new thread, correct?

                        JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #11

                        @mzimmers said in how to properly construct derived class...:

                        Oh...I didn't realize that. Is that because the serialPort object is created in the c'tor of the worker? If the worker created the object after it had been moved to its own thread, then the object would exist within the new thread, correct?

                        Correct

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        1

                        • Login

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