Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

My Qt thread is not working!!!



  • 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);
    }
    
    

  • Lifetime Qt Champion

    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.



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


Log in to reply