Results 1 to 5 of 5

Thread: Why my object stay in main thread?

  1. #1
    Join Date
    Jan 2013
    Posts
    21
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Why my object stay in main thread?

    I wanted to create a serial port monitor thread. I used QT4.8.6 + QSerialPort(latest) on Windows XP.

    I derived a CSerialPort subclass from QObject class and a subclass of QThread at first.
    Qt Code:
    1. class CSerialPort : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. CSerialPort();
    7. ~CSerialPort();
    8.  
    9. void printThreadID();
    10.  
    11. signals:
    12.  
    13. public slots:
    14.  
    15. private:
    16. QSerialPort *m_spSerialPort; // serial port object
    17. QTimer *m_tQueryTimer;
    18. };
    19.  
    20. class CMonitorThread : public QThread
    21. {
    22. Q_OBJECT
    23.  
    24. public:
    25. CMonitorThread( QObject *parent = NULL );
    26. ~CMonitorThread();
    27. void run();
    28.  
    29. public slots:
    30. signals:
    31.  
    32. private:
    33.  
    34. };
    35.  
    36. CSerialPort::CSerialPort()
    37. {
    38. // Create serial port object
    39. m_spSerialPort = new QSerialPort();
    40. // 1. print the thead where m_spSerialPort lived in
    41. printThreadID();
    42.  
    43. // Timer for send command
    44. m_tQueryTimer = new QTimer;
    45.  
    46. }
    To copy to clipboard, switch view to plain text mode 
    The CMonitorThread did nothing just run its event loop
    Qt Code:
    1. CMonitorThread::CMonitorThread(QObject *parent) : QThread(parent)
    2. {
    3.  
    4. }
    5.  
    6. CMonitorThread::~CMonitorThread()
    7. {
    8.  
    9. }
    10.  
    11. void CMonitorThread::run()
    12. {
    13. this->exec();
    14. }
    To copy to clipboard, switch view to plain text mode 
    Then, I declared pointers to CMonitorThread and CSerialPort in the mainwindow.
    Qt Code:
    1. class CAnalyzerWindow : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. CAnalyzerWindow(QWidget * = NULL);
    7. ~CAnalyzerWindow();
    8.  
    9. signals:
    10.  
    11. public slots:
    12.  
    13. private:
    14. CSerialPort *m_spDevPort;
    15. CMonitorThread *m_mtPortMonitorThread;
    16.  
    17. };
    To copy to clipboard, switch view to plain text mode 

    In the constructor of the mainwindow, I created the instance of the CSerialPort and the CMonitorThread
    Qt Code:
    1. CAnalyzerWindow::CAnalyzerWindow( QWidget *parent ) : QWidget( parent )
    2. {
    3. /* Initialize widgets and layout */
    4. InitReceiveGroupBox();
    5. InitSettingGroupBox();
    6. InitScopeGroupBox();
    7.  
    8. QGridLayout *mainLayout = new QGridLayout;
    9. mainLayout->addWidget( m_gbScope, 0, 0, 1, 2 );
    10. mainLayout->addWidget( m_gbSetting, 1, 0 );
    11. mainLayout->addWidget( m_gbReceive, 1, 1 );
    12.  
    13. setLayout( mainLayout );
    14. update();
    15.  
    16. /* Enumerate available serial port */
    17. if (!enumAvailableSerialPort())
    18. {
    19. qDebug() << "No available ports!";
    20. return;
    21. }
    22.  
    23. /* Create serial port object */
    24. m_spDevPort = new CSerialPort();
    25. qDebug() << m_spDevPort->thread();// 2. print the thead where m_spDevPort lived in
    26.  
    27. /* Create serial port monitor thread */
    28. m_mtPortMonitorThread = new CMonitorThread();
    29.  
    30. /* Attach serial port object to monitor thread */
    31. m_spDevPort->moveToThread(m_mtPortMonitorThread);
    32. /* Start thread */
    33. m_mtPortMonitorThread->start();
    34.  
    35. // 3. print the thead where m_mtPortMonitorThread lived in
    36. qDebug() << m_mtPortMonitorThread->currentThread();
    37. // 4. print the thead where m_spDevPort lived in
    38. qDebug() << m_spDevPort->thread();
    39. // 5. print the thead where m_spSerialPort lived in
    40. m_spDevPort->printThreadID();
    41.  
    42.  
    43. connect( m_pbStart, SIGNAL( clicked() ), this, SLOT( slt_pbStart_Clicked() ) );
    44.  
    45. }
    To copy to clipboard, switch view to plain text mode 

    Then, I got the following output.
    1.QThread(0x9b7d68)
    2.QThread(0x9b7d68)
    3.QThread(0x9b7d68)
    4.CMonitorThread(0xff79e0)
    5.QThread(0x9b7d68)

    I puzzled with 4 and 5. I have used moveToThread function to move the m_spDevPort to the monitor thread, but its member m_spSerialPort still stayed in the main thread.

    So, when I tried to open the serial port using signal and slot, I got the following message
    "Cannot create children for a parent that is in a different thread."(only occur when I tried to open serial port)

    Could anybody tell me what is the problem?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Why my object stay in main thread?

    I puzzled with 4 and 5. I have used moveToThread function to move the m_spDevPort to the monitor thread, but its member m_spSerialPort still stayed in the main thread.
    your problem is that m_spSerialPort's parent is not set.

    Qt Code:
    1. CSerialPort::CSerialPort()
    2. {
    3. // Create serial port object
    4. m_spSerialPort = new QSerialPort(this); //<<<<<<<<<<<<<<<< Pass the parent while object creation.
    5. // 1. print the thead where m_spSerialPort lived in
    6. printThreadID();
    7.  
    8. // Timer for send command
    9. m_tQueryTimer = new QTimer;
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    honestapple (2nd April 2013)

  4. #3
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why my object stay in main thread?

    I wanted to create a serial port monitor thread. I used QT4.8.6 + QSerialPort(latest) on Windows XP.
    Using the QSerialPort library is not good idea.

    Is better to use - QtSerialPort library.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Why my object stay in main thread?

    Using threads is probably unnecessary also.

  6. #5
    Join Date
    Jan 2013
    Posts
    21
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why my object stay in main thread?

    Yes, it is QtSerialPort library.


    Added after 26 minutes:


    Yes, I knew that you have mention that. I am doing a USB DAQ GUI part on PC using QT4.8.6+QWT6.0.2 under Windows XP. I should receive data from device and display waveform, frequency spectrum, etc. I have made a demo without threads which could receive and send command( using timer ) at a interval, but I could not display the wavefrom in realtime. In fact, it was not that the waveform could not be displayed completly. It is sometimes good, sometimes bad. When it was bad, I knew that there are something wrong with the QwtCPointerData. Because there are many operation in main GUI thread, I wanted to tried to use monitor thread to release GUI thread from doing data receiving and sending command.
    Last edited by honestapple; 3rd April 2013 at 04:29.

Similar Threads

  1. Replies: 11
    Last Post: 27th January 2013, 12:47
  2. Replies: 1
    Last Post: 28th March 2012, 18:58
  3. Replies: 5
    Last Post: 22nd February 2011, 21:21
  4. Replies: 9
    Last Post: 28th November 2009, 20:31
  5. Replies: 16
    Last Post: 7th October 2009, 08:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.