PDA

View Full Version : QTimer can only be used with threads started with QThread



saman_artorious
13th June 2012, 16:33
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



class FuelSystem : public QObject
{
Q_OBJECT
public:
explicit FuelSystem(QObject *parent = 0);
void init();

signals:
bool DSLFL_SendToRS485(QByteArray );
void DSLFL_SendToProducer(QByteArray);

public slots:
void updateMyData();
void DSLFL_catchInitSignal();
void DSLFL_DataReceive(QByteArray );
....
QTimer *pumpLifePeriodTimer;
QTimer *pump83PercLifePeriod;
QTimer *waterEvacuateTimer;
QTimer *updateDataTimer;




if(data.at(3) == FUEL_SYSTEM)
{
qDebug() << "FUEL_SYSTEM";

if(data.at(4) == 0x01)
{
qDebug() << "Lower Sensor= 0x01";
qDebug() << "STARTING PUMP..";
command_startPump.append(FUEL_SYSTEM);
command_startPump.append(FUEL_PUMP);
command_startPump.append(0x01);

emit DSLFL_SendToRS485(command_startPump);

qDebug() << "OPENING SV_15";

command_SV15.append(FUEL_SYSTEM);
command_SV15.append(DSLFL_ST_SV_15);
command_SV15.append(0x01);

emit DSLFL_SendToRS485(command_SV15);
}


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

amleto
13th June 2012, 18:13
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.

saman_artorious
13th June 2012, 20:44
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?

wysota
13th June 2012, 21:25
Did you remember to create a QApplication object?

amleto
13th June 2012, 22:44
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.

saman_artorious
14th June 2012, 09:48
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

// Producer *producer = new Producer();
// Consumer *consumer = new Consumer();
// FuelSystem *fuelSystem = new FuelSystem();

// producer->start();
// consumer->start();
// producer->wait();
// consumer->wait();


// QObject::connect(fuelSystem, SIGNAL(DSLFL_SendToRS485(QByteArray)), producer, SLOT(ProducerWritePacket(QByteArray)));
// fuelSystem->init();

rs485 *rs_485 = new rs485();
//FuelSystem *fuelSystem = new FuelSystem();
exhaustSystem *MyExhaust = new exhaustSystem();

QByteArray packet;

packet.append(0x04);

QObject::connect(MyExhaust, SIGNAL(EXHAUST_SendToRS485(QByteArray)), rs_485, SLOT(rs485DataAquisition(QByteArray)));
QObject::connect(rs_485, SIGNAL(rs485sendData(QByteArray)),MyExhaust, SLOT(EXHAUST_DataReceive(QByteArray)));

rs_485->rs485DataAquisition(packet);
return a.exec()
}


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.

wysota
14th June 2012, 09:49
And where do you start those timers?

saman_artorious
14th June 2012, 10:19
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.