QTimer in a GUI application
-
For three days I am trying to solve this. I have seen numerous topics, but none of them do what I want to do, which is to use the QTimer in the class that contains the UI. The example that the documentation give is for NON-GUI application. But I wanna exactly this
-
Then let's first get back to the basics.
Do you still see this message if you create a default Qt Widget application and add
@
mainwindow.hclass MainWindow: public QWidget
{
// default codepublic slots:
void testTimer();
}mainwindow.cpp
MainWindow:MainWindow(QWidget *parent)
: QWidget(parent)
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(testTimer()));timer->start(20);
}void MainWindow::testTimer()
{
qDebug() << "Timed out !";
}
@to the default code ?
-
Hi, Samuel!
This works, I tested. But my base class is QMainWindow. I tried to change the base class for QWiget but it happened several errors.
Thanks, man!
-
Just keep your QMainWindow based class, the only thing that was important was the part related to the QTimer
-
I have to change the base class to use QTimer?
-
Can you show us your code for doWork()?
Note that in newer versions of Qt, the actual message is "QObject::startTimer: Timers can only be used with threads started with QThread". ("Timers", not "QTimer"!)
All QObjects have built-in timers. I don't think the message came from your QTimer, but rather from your QSerialPort.
-
No you don't, QMainWindow is a QWidget as well
-
[quote author="JKSH" date="1422056561"]Can you show us your code for doWork()?
Note that in newer versions of Qt, the actual message is "QObject::startTimer: Timers can only be used with threads started with QThread". ("Timers", not "QTimer"!)
All QObjects have built-in timers. I don't the message came from your QTimer, but rather from your QSerialPort.[/quote]
@void client::readFlightData(){
//QMessageBox::information(this,"", "txtReadFlightData"); serial->write("gFD;"); if (serial->waitForBytesWritten(timeout)) { if(serial->waitForReadyRead(timeout)) { txtReadFlightData = serial->readAll(); listReadFlightData = txtReadFlightData.split(";", QString::SkipEmptyParts); if(listReadFlightData.size()==7) { if(ui->SelectUnitsAmerican->isChecked()==1) // Unidades no SI { ui->txtPitch->setText(listReadFlightData[0]); ui->txtRoll->setText(listReadFlightData[1]); ui->txtHeightGPS->setText(QString::number(listReadFlightData[2].toDouble())); ui->txtHeightBarometer->setText(QString::number(listReadFlightData[3].toDouble())); ui->txtSpeedPitot->setText(QString::number(listReadFlightData[4].toDouble())); ui->txtSpeedGPS->setText(QString::number(listReadFlightData[5].toDouble())); ui->txtVerticalSpeed->setText(QString::number(listReadFlightData[6].toDouble())); countLostSerial = 0; listReadFlightData.clear(); }else{ // U.S. Customary ui->txtPitch->setText(listReadFlightData[0]); ui->txtRoll->setText(listReadFlightData[1]); ui->txtHeightGPS->setText(QString::number(listReadFlightData[2].toDouble()*3.28084)); // Feet ui->txtHeightBarometer->setText(QString::number(listReadFlightData[3].toDouble()*3.28084)); // Feet ui->txtSpeedPitot->setText(QString::number(listReadFlightData[4].toDouble()*2.2369)); // Miles per hour ui->txtSpeedGPS->setText(QString::number(listReadFlightData[5].toDouble()*2.2369)); // Miles per hour ui->txtVerticalSpeed->setText(QString::number(listReadFlightData[6].toDouble()*2.2369)); // Miles per hour countLostSerial = 0; listReadFlightData.clear(); } }else countLostSerial += 1; } } if(countLostSerial == 250){ countLostSerial = 0; disconnectSerial(); }
}@
-
[quote author="JKSH" date="1422056561"]Can you show us your code for doWork()?
Note that in newer versions of Qt, the actual message is "QObject::startTimer: Timers can only be used with threads started with QThread". ("Timers", not "QTimer"!)
All QObjects have built-in timers. I don't the message came from your QTimer, but rather from your QSerialPort.[/quote]
Hey man, thanks for the help!
I think that's the problem. I did it:
@QThread * thread = new QThread ();
QSerialPort * = new serial QSerialPort ();
Serial-> moveToThread (thread);@But nothing has changed. I keep getting:
@QObject :: startTimer: QTimer can only be used with threads started with QThread.@
Can you tell me what should I do? I searched for some topics but nothing they say solved the problem. Thanks a lot!!
-
[quote author="HamiltonQ" date="1422400475"]
@QThread * thread = new QThread ();
QSerialPort * = new serial QSerialPort ();
Serial-> moveToThread (thread);@
[/quote]Did you mean:
@
QThread * thread = new QThread ();
QSerialPort *serial = new QSerialPort ();
serial-> moveToThread (thread);
@
? -
[quote author="SGaist" date="1422405461"][quote author="HamiltonQ" date="1422400475"]
@QThread * thread = new QThread ();
QSerialPort * = new serial QSerialPort ();
Serial-> moveToThread (thread);@
[/quote]Did you mean:
@
QThread * thread = new QThread ();
QSerialPort *serial = new QSerialPort ();
serial-> moveToThread (thread);
@
?[/quote]Hi SGaist!
Yes. I ended up writing wrong. I did it but did not work. Do you have any suggestion? Thaks!
-
Just to be sure I'm understanding your code correctly: you are moving your QSerialPort instance to a thread and then using the blocking functions in your widget code ?