0

I want to create a program by using Qt framework. The aim is to write a program which uses QThread to show a simple digital clock. but nothing happened when running.

This is the subclass of Qthread for running

paytamtimers.h

#ifndef PAYTAMTIMERS_H
#define PAYTAMTIMERS_H
#include <QThread>

class PaytamTimers:public QThread
{
    Q_OBJECT
public:
    PaytamTimers();
QString now;
protected:
    virtual void run();
private:
    QMutex mutex;
    QThread *thread;

signals:
    void mySignal(QString);
};

#endif // PAYTAMTIMERS_H

and this is the implementation of this.class paytamtimers.cpp

#include "paytamtimers.h"
#include <QTime>

PaytamTimers::PaytamTimers()
{
    this->now="";
    this->thread=new QThread(0);
}

void PaytamTimers::run(){
forever{
    mutex.lock();
    this->now=QTime::currentTime().toString();
    this->thread->sleep(1000);
    emit mySignal(this->now);
    mutex.unlock();

}
}

and this is the implementation of GUI form. This for consist of QLabel and an instance of paytamtimers,just for simplity

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "paytamtimers.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    t=new PaytamTimers();
    t->start();
    connect(t,SIGNAL(t->mySignal(QString)),this,SLOT(this->now(const QString &string)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::now(const QString &string){
    this->ui->label->setText(t->now);
}
László Papp
  • 51,870
  • 39
  • 111
  • 135
paytam
  • 327
  • 6
  • 15
  • Look at this example:http://www.codeprogress.com/cpp/libraries/qt/countDownClockwithQTimer.php#.UijZWMaMm8M – user2672165 Sep 05 '13 at 19:21
  • Also, follow http://stackoverflow.com/questions/4093159/what-is-the-correct-way-to-implement-a-qthread-example-please for the correct way to use QThread. Subclassing from QThread just to implement a worker is not the correct way to use threads – g19fanatic Sep 05 '13 at 19:24
  • [Something you might want to read](http://qt-project.org/wiki/Threads_Events_QObjects#c2e33c61ced29e1eb1bee9a1671f202e). – thuga Sep 06 '13 at 06:52

2 Answers2

1
  1. You should not hold the mutex while sleeping in the thread. In fact, your mutex is completely unnecessary.

  2. Your connect statement is wrong, as noted by hyde. The this parameter is implied, so you could simply say:

    connect(t, SIGNAL(mySignal(QString)), SLOT(now(QString)));
    
  3. You don't need to use a thread in order to emit periodic time updates.

Your MainWindow could look like below. It'll take care to fire the timer event as close to full second as possible.

class MainWindow : public QWidget {
    Q_OBJECT
    QBasicTimer m_timer;
    void timerEvent(QTimerEvent * ev) {
        if (ev->timerId() == m_timer.timerId()) {
            QTime t = QTime::currentTime();
            m_timer.start(1000 - t.msec(), this);
            // round to nearest second
            if (t.msec() < 500) t = t.addMsecs(-t.msec()); else t = t.addMSecs(1000-t.msec());
            now(t.toString());
        }
    }
    void now(const QString &);
    ...
public:
    MainWindow(QWidget *parent = 0) : QWidget(parent) {
        m_timer.start(1000 - QTime::currentTime().msec(), this);
    }
};
Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

Your connect statement is wrong, it should be:

connect(t, SIGNAL(mySignal(QString)), this, SLOT(now(QString)));

To catch problems like this, first of all check console output, there you should see a warning about failing connect. And another thing, use Qt Creator autocompletion to fill in SIGNAL and SLOT macros, because it is very easy to make stupid mistakes, which will not be spotted by compiler.

There are a few other funny things, such as the seemingly useless extra QThread* member variable in your QThread subclass, and an unnecessary mutex, but these shouldn't stop things from working.

In general, it seems you're just learning Qt. I'd leave threads for later, they have a lot of small gotchas and details, and worrying about them while also learning the Qt basics will not make life easy.

hyde
  • 60,639
  • 21
  • 115
  • 176