Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. My Qt thread is not working!!!
Qt 6.11 is out! See what's new in the release blog

My Qt thread is not working!!!

Scheduled Pinned Locked Moved Solved Mobile and Embedded
3 Posts 2 Posters 1.3k 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.
  • N Offline
    N Offline
    NTCYP
    wrote on last edited by
    #1

    Hi,
    My Qt thread is not working? Somewhere I am having a problem and cannot see where is it? Any help please.

    MainWindowDialog.h

    #include "ThreadWifi.h"
    #include "ThreadPil.h"
    #include "ThreadSaat.h"
    
    namespace Ui {
    class MainWindow;
    }
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        ThreadWifi *wifiThread;
        ThreadPil *pilThread;
        ThreadSaat *saatThread;
    
    public slots:
            QString ReplaceDate(QString value);
    
            // Wifi Thread
            void onWifiChanged(bool);
    
            // Pil Thread
            void onPilChanged(int);
    
            // Saat Thread
            void onSaatChanged(QString);
    
            // QTimers Connect
            void showTimeStatus();
            void showbatteryPercent();
            void showWifiLogo();
    
    private slots:
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    
    
    

    **MainWindowDialog.cpp **

       saatThread = new ThreadSaat(this);
       connect(saatThread,SIGNAL(SaatChanged(bool)), this, SLOT(onSaatChanged(bool)));
    
       saatTimer = new QTimer(this);
       connect(saatTimer, SIGNAL(timeout()),this, SLOT(showTimeStatus()));
       saatTimer->start(1000);
    
    

    onSaatChanged

    void MainWindow::onSaatChanged(QString saatNow)
    {
         ui->labelTime->setText("");
        ui->labelTime->setText(saatNow);
    }
    

    Thread Start

    void MainWindow::showTimeStatus()
    {
        saatThread->start();
    }
    

    My threadSaat.h

    #include <QObject>
    #include <QThread>
    
    class ThreadSaat : public QThread
    {
        Q_OBJECT
    public:
        explicit ThreadSaat(QObject *parent = 0);
        ~ThreadSaat();
    
        void run();
        bool SaatStop;
    
    signals:
        void SaatChanged(QString);
    
    public slots:
    };
    

    My threadSaat.cpp

    #include "ThreadSaat.h"
    #include <QtCore>
    
    ThreadSaat::ThreadSaat(QObject *parent) : QThread(parent)
    {
    }
    
    ThreadSaat::~ThreadSaat()
    {
    }
    
    void ThreadSaat::run()
    {
        QMutex mutex;
        mutex.lock();
        if(this->SaatStop == true)
        {
            return;
        }
        QString saatNow;
        QTime time = QTime::currentTime();
        saatNow = (time.toString("hh:mm:ss"));
    
        mutex.unlock();
       
        emit SaatChanged(saatNow);
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Several things that doesn't look good:

      • You don't initialize SaatStop to false
      • You're locking mutex but if SaatStop == true you don't unlock it, so if it start initialized to true, the second time your timer times out. The thread will wait on a locked mutex forever.

      For what ThreadSaat is doing, I really don't see the need of a QThread with run re-implemeted based class. A slot connected to a QTimer would be really simpler and less error prone.

      Before splitting everything in threads in your application, is it really needed ? Depending on what the other classes are doing, you should also take a look at the worker object implementation or even QtConcurrent.

      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
      1
      • N Offline
        N Offline
        NTCYP
        wrote on last edited by
        #3

        Thank you @SGaist,

        Now is working. It was run in QTimer before on main screen and I had another 2 QTimer. My app runs in embedded Linux and with 3 QTimer the buttons seems to be less responsive.

        When I move into the thread and I use QTimer to start a tread makes my embedded Linux device buttons more and quick responsive.

        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