Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTimer in a GUI application
Qt 6.11 is out! See what's new in the release blog

QTimer in a GUI application

Scheduled Pinned Locked Moved General and Desktop
25 Posts 5 Posters 15.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HamiltonQ
    wrote on last edited by
    #9

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

    1 Reply Last reply
    0
    • H Offline
      H Offline
      HamiltonQ
      wrote on last edited by
      #10

      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.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on last edited by
        #11

        do you have an QApplication instance?

        1 Reply Last reply
        0
        • H Offline
          H Offline
          HamiltonQ
          wrote on last edited by
          #12

          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

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kuzulis
            Qt Champions 2020
            wrote on last edited by
            #13

            Then you can google by the "QTimer can only be used with threads started with QThread." pattern. ;)

            1 Reply Last reply
            -1
            • H Offline
              H Offline
              HamiltonQ
              wrote on last edited by
              #14

              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

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #15

                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 ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  HamiltonQ
                  wrote on last edited by
                  #16

                  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!

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #17

                    Just keep your QMainWindow based class, the only thing that was important was the part related to the QTimer

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      HamiltonQ
                      wrote on last edited by
                      #18

                      I have to change the base class to use QTimer?

                      1 Reply Last reply
                      0
                      • JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #19

                        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.

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #20

                          No you don't, QMainWindow is a QWidget as well

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            HamiltonQ
                            wrote on last edited by
                            #21

                            [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();
                            
                            }
                            

                            }@

                            1 Reply Last reply
                            0
                            • H Offline
                              H Offline
                              HamiltonQ
                              wrote on last edited by
                              #22

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

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #23

                                [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);
                                @
                                ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • H Offline
                                  H Offline
                                  HamiltonQ
                                  wrote on last edited by
                                  #24

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

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #25

                                    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 ?

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved