Results 1 to 8 of 8

Thread: QTimer can only be used with threads started with QThread

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default QTimer can only be used with threads started with QThread

    I want to wait for 2 seconds n then trigger my SLOT using SingleShot(). Though it fails, here's the error:
    QObject::startTimer:QTimer can only be used with threads started with QThread
    Qt Code:
    1. class FuelSystem : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit FuelSystem(QObject *parent = 0);
    6. void init();
    7.  
    8. signals:
    9. bool DSLFL_SendToRS485(QByteArray );
    10. void DSLFL_SendToProducer(QByteArray);
    11.  
    12. public slots:
    13. void updateMyData();
    14. void DSLFL_catchInitSignal();
    15. void DSLFL_DataReceive(QByteArray );
    16. ....
    17. QTimer *pumpLifePeriodTimer;
    18. QTimer *pump83PercLifePeriod;
    19. QTimer *waterEvacuateTimer;
    20. QTimer *updateDataTimer;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. if(data.at(3) == FUEL_SYSTEM)
    2. {
    3. qDebug() << "FUEL_SYSTEM";
    4.  
    5. if(data.at(4) == 0x01)
    6. {
    7. qDebug() << "Lower Sensor= 0x01";
    8. qDebug() << "STARTING PUMP..";
    9. command_startPump.append(FUEL_SYSTEM);
    10. command_startPump.append(FUEL_PUMP);
    11. command_startPump.append(0x01);
    12.  
    13. emit DSLFL_SendToRS485(command_startPump);
    14.  
    15. qDebug() << "OPENING SV_15";
    16.  
    17. command_SV15.append(FUEL_SYSTEM);
    18. command_SV15.append(DSLFL_ST_SV_15);
    19. command_SV15.append(0x01);
    20.  
    21. emit DSLFL_SendToRS485(command_SV15);
    22. }
    To copy to clipboard, switch view to plain text mode 
    Well, the timers of every object are being initialized inside their own .cpp class definition file
    My class only inherits QObject and not QThread. Though, I don't think that might be relevant.
    I also tried passing an EventLoop as the singleshot parameter, I another error:
    something like, QEvent needs Q::exec
    though, as m a fresher i don't really understand EventLoop, Anyway,
    To solve the first error, any helps would be appreciable
    Last edited by saman_artorious; 13th June 2012 at 19:40. Reason: updated contents

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer can only be used with threads started with QThread

    1) What class is that code in?
    2) Where in your code is the class instance from 1) created?
    3) Show how this 'where' from 2) relates to the threads in your code.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTimer can only be used with threads started with QThread

    Quote Originally Posted by amleto View Post
    1) What class is that code in?
    2) Where in your code is the class instance from 1) created?
    3) Show how this 'where' from 2) relates to the threads in your code.
    I updated my previous post. You may have a look at the updated code.
    about question No. 2, every object is defined in main: like for the above case:

    fuelSys *fue.. = new Fue...()

    about question 3), I did not define my objects as QThreads, They are simply OBJECTS Inheriting QObject only, as it enables the objects to communicate
    via emission and reception.

    So, you have any idea how I can make all singleShots inside my objects work?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer can only be used with threads started with QThread

    Did you remember to create a QApplication object?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer can only be used with threads started with QThread

    Quote Originally Posted by saman_artorious View Post
    I updated my previous post. You may have a look at the updated code.

    about question No. 2, every object is defined in main: like for the above case:
    Unusual. Please show your main.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTimer can only be used with threads started with QThread

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. // Producer *producer = new Producer();
    6. // Consumer *consumer = new Consumer();
    7. // FuelSystem *fuelSystem = new FuelSystem();
    8.  
    9. // producer->start();
    10. // consumer->start();
    11. // producer->wait();
    12. // consumer->wait();
    13.  
    14.  
    15. // QObject::connect(fuelSystem, SIGNAL(DSLFL_SendToRS485(QByteArray)), producer, SLOT(ProducerWritePacket(QByteArray)));
    16. // fuelSystem->init();
    17.  
    18. rs485 *rs_485 = new rs485();
    19. //FuelSystem *fuelSystem = new FuelSystem();
    20. exhaustSystem *MyExhaust = new exhaustSystem();
    21.  
    22. QByteArray packet;
    23.  
    24. packet.append(0x04);
    25.  
    26. QObject::connect(MyExhaust, SIGNAL(EXHAUST_SendToRS485(QByteArray)), rs_485, SLOT(rs485DataAquisition(QByteArray)));
    27. QObject::connect(rs_485, SIGNAL(rs485sendData(QByteArray)),MyExhaust, SLOT(EXHAUST_DataReceive(QByteArray)));
    28.  
    29. rs_485->rs485DataAquisition(packet);
    30. return a.exec()
    31. }
    To copy to clipboard, switch view to plain text mode 
    rs485 is a class which open the serial port using Linux system calls, write to and reads from serial as well.

    Added after 11 minutes:


    I tried singleShot once more, it works , I think that's because return a.exec() and QCoreApp... a were commented.
    Last edited by wysota; 14th June 2012 at 09:25. Reason: missing [code] tags

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer can only be used with threads started with QThread

    And where do you start those timers?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTimer can only be used with threads started with QThread

    I tried singleShot once more, it works , I think that's because return a.exec() and QCoreApp... a were commented.

    those timers are also initialized in the class consturctor.

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Using QTimer in a QThread
    By Cruz in forum Qt Programming
    Replies: 38
    Last Post: 25th April 2011, 11:29
  3. QTimer within QThread
    By Eos Pengwern in forum Qt Programming
    Replies: 6
    Last Post: 23rd February 2011, 20:00
  4. QThread and QTimer
    By sivrisinek in forum Qt Programming
    Replies: 4
    Last Post: 30th April 2009, 16:41
  5. QThread & QTimer
    By hosseinyounesi in forum Qt Programming
    Replies: 5
    Last Post: 13th April 2009, 08:22

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.