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!!


  • Lifetime Qt Champion

    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?


  • Moderators

    [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


  • Lifetime Qt Champion

    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.h

    class MainWindow: public QWidget
    {
    // default code

    public 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!


  • Lifetime Qt Champion

    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?


  • Moderators

    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.


  • Lifetime Qt Champion

    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!!


  • Lifetime Qt Champion

    [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!


  • Lifetime Qt Champion

    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 ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.