QTimer in a GUI application
-
Hello, everyone!
I'm starting to produce applications with Qt In one of my applications I've come across an unexpected output:
QObject :: startTimer: QTimer can only be used with threads started with QThread.
I'm starting my timer (timer-> start ()) in the class that contains the UI elements (QMainWindow). I do this out of the constructor, in the function below:
void client :: showPorts () {
SettingsDialog showDialogPorts * = new SettingsDialog (this);
serial = new QSerialPort ();
timer = new QTimer ();
connect (time, SIGNAL (timeout ()), this, SLOT (doMyTask ()));
timer-> start (20);
.
.
.
.
.}
What's wrong with using QTimer in the GUI class (QMainWindow)? Thanks!!
-
Hi and welcome to devnet,
Are you sure it's that timer that triggers the message ? Not any thread using it ?
-
[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?
-
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?
-
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 ?
-
Just keep your QMainWindow based class, the only thing that was important was the part related to the 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