30/09/2018, 16:38

Lỗi khi sử dụng QTimer - Qt

Khi click vào Start button để timer->start() thì bị lỗi has stop working.
“The program has unexpectedly finished.”
Mọi người xem giúp em phát. @ltd
Đây là giao diện:

File mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include "time.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    QTimer* timer;
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

signals:

private slots:
    void update();
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;
    time* myTime;
};

#endif // MAINWINDOW_H

File mainwindow.cpp:

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));

    ui->ss->setDigitCount(59);
        ui->mm->setDigitCount(59);
        ui->hh->setDigitCount(59);
}

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

void MainWindow::update()
{
    ui->hh->display(myTime->getH());
    ui->mm->display(myTime->getM());
    ui->ss->display(myTime->getS());
    myTime->increase();
}

void MainWindow::on_pushButton_clicked()
{
    timer->start(1000);
}

void MainWindow::on_pushButton_2_clicked()
{
    timer->stop();
}

File time.h:

#ifndef TIME_H
#define TIME_H

#include <QtCore>

class time : public QObject  {

    Q_OBJECT

public:

    time();
    ~time();
    void resetTime();
    int getH();
    int getM();
    int getS();
    void increase();

signals:

private slots:

private:

    int h;
        int m;
        int s;
};

#endif // TIME_H

Và cuối cùng là file time.cpp:

#include "time.h"

time::time() : h(0),m(0),s(0)
{

}

time::~time()
{
    resetTime();
}

void time::resetTime()
{
    h = 0;
    m = 0;
    s = 0;
}

void time::increase()
{
    s++;
    if(s == 60)
    {
        s = 0;
        m++;
        if(m == 60)
        {
            m = 0;
            h++;
        }
    }
}

int time::getH()
{
    return h;
}

int time::getM()
{
    return m;
}

int time::getS()
{
    return s;
}
... viết 18:50 ngày 30/09/2018

Đã sửa được rồi ạ Chỉnh sửa lại cái signals và slot là ok

File mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include "time.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    QTimer* timer;
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

signals:

public slots:
    void update();
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;
    time* myTime;
};

#endif // MAINWINDOW_H

File mainwindow.cpp:

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timer = new QTimer(this);
    myTime = new time();
    connect(timer,SIGNAL(timeout()),myTime,SLOT(increase()));
    connect(myTime,SIGNAL(getIncrease()),this,SLOT(update()));
}

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

void MainWindow::update()
{
    ui->hh->display(myTime->getH());
    ui->mm->display(myTime->getM());
    ui->ss->display(myTime->getS());
}

void MainWindow::on_pushButton_clicked()
{
    timer->start(1000);
}

void MainWindow::on_pushButton_2_clicked()
{
    timer->stop();
}

File time.h:

#ifndef TIME_H
#define TIME_H

#include <QtCore>

class time : public QObject  {

    Q_OBJECT

public:

    time();
    ~time();
    void resetTime();
    int getH();
    int getM();
    int getS();


signals:
    void getIncrease();

public slots:
    void increase();

private:

    int h;
    int m;
    int s;
};

#endif // TIME_H

Và time.cpp:

#include "time.h"

time::time() : h(0),m(0),s(0)
{

}

time::~time()
{
    resetTime();
}

void time::resetTime()
{
    h = 0;
    m = 0;
    s = 0;
}

void time::increase()
{
    s++;
    if(s == 60)
    {
        s = 0;
        m++;
        if(m == 60)
        {
            m = 0;
            h++;
        }
    }
    emit getIncrease();
}

int time::getH()
{
    return h;
}

int time::getM()
{
    return m;
}

int time::getS()
{
    return s;
}
Bài liên quan
0