QTimer in a GUI application
-
Hi and welcome to devnet,
Are you sure it's that timer that triggers the message ? Not any thread using it ?
-
Hey, man! You are fast, thanks!! I'm not sure, but I want to control my event from a timer that I have control, so I used the QTimer * timer = new QTimer ();
-
It seems to me that there is some problem when I use in class that contains access to UI, am I right?
-
[quote author="HamiltonQ" date="1421974393"]It seems to me that there is some problem when I use in class that contains access to UI, am I right?[/quote]No, that's not a problem.
How many threads does your program have?
-
I don't initiated any thread purposely. I do not know if the application automatically starts some thread to call some method or to start some object.
-
I just want to start a count to trigger a function every 20 ms in the same class that contains the call to the UI. It's possible?
-
It should not be a problem unless doMyTask () can initiate another call to showPorts () in which cay you may keep creating timers until system is exhausted
Alex.
-
[quote author="alex_malyu" date="1421985237"]It should not be a problem unless doMyTask () can initiate another call to showPorts () in which cay you may keep creating timers until system is exhausted
Alex.[/quote]
Thanks, Alex!
But, why doMyTask() have to call showPorts()? I just wanna call doMyTask every 20 ms.
-
I changed of place the connect ( connect (time, SIGNAL (timeout ()), this, SLOT (doMyTask ())); ), I put him on constructor, but nothing changed, I keep getting the output:
QObject :: startTimer: QTimer can only be used with threads started with QThread.
-
do you have an QApplication instance?
-
Hi!
QApplication instance is in the main.cpp file, but I'm starting the Timer on client.cpp file, which contains the call to the UI
-
Then you can google by the "QTimer can only be used with threads started with QThread." pattern. ;)
-
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(); }
}@