LINUX.ORG.RU

История изменений

Исправление RazrFalcon, (текущая версия) :

controller.h

#ifndef CONTROLLER_H
#define CONTROLLER_H

#include <QObject>
#include <QTimer>
#include <QDebug>

class Controller: public QObject
{
    Q_OBJECT

public:
    Controller(QObject *parent = Q_NULLPTR)
        : QObject(parent)
    {
        qDebug() << Q_FUNC_INFO;
    }

    ~Controller()
    {
        qDebug() << Q_FUNC_INFO;
    }

public slots:
    void put(const QString &msg)
    {
        qDebug() << msg;
    }
};

class Writer : public QObject
{
    Q_OBJECT

public:
    Writer(QObject *parent = Q_NULLPTR)
        : QObject(parent),
          m_timer(new QTimer(this))
    {
        qDebug() << Q_FUNC_INFO;
        connect(m_timer, &QTimer::timeout, this, &Writer::onSend);
    }

    ~Writer()
    {
        qDebug() << Q_FUNC_INFO;
    }

signals:
    void send(const QString &msg);
    void taskFinished();

public slots:
    void startSending()
    {
        m_timer->start(5);
    }

private slots:
    void onSend()
    {
        emit send(QString("Package %1").arg(m_pkg++));

        if (m_pkg == 100) {
            m_timer->stop();
            emit taskFinished();
        }
    }

private:
    QTimer *m_timer;
    int m_pkg = 1;
};

#endif // CONTROLLER_H

main.cpp

#include <QThread>
#include <QApplication>

#include "controller.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Controller ctrl;

    QThread pThread;
    Writer pWriter;
    pWriter.moveToThread(&pThread);

    QObject::connect(&pThread, &QThread::started,    &pWriter,    &Writer::startSending);
    QObject::connect(&pWriter, &Writer::send,         &ctrl,      &Controller::put, Qt::QueuedConnection);
    QObject::connect(&pWriter, &Writer::taskFinished, [&a, &pThread](){
        pThread.quit();
        a.quit();
    });

    pThread.start();

    return a.exec();
}

UPD: timer лучше не создавать в конструкторе, но для данного случая и так норм.

Исходная версия RazrFalcon, :

controller.h

#ifndef CONTROLLER_H
#define CONTROLLER_H

#include <QObject>
#include <QTimer>
#include <QDebug>

class Controller: public QObject
{
    Q_OBJECT

public:
    Controller(QObject *parent = Q_NULLPTR)
        : QObject(parent)
    {
        qDebug() << Q_FUNC_INFO;
    }

    ~Controller()
    {
        qDebug() << Q_FUNC_INFO;
    }

public slots:
    void put(const QString &msg)
    {
        qDebug() << msg;
    }
};

class Writer : public QObject
{
    Q_OBJECT

public:
    Writer(QObject *parent = Q_NULLPTR)
        : QObject(parent),
          m_timer(new QTimer(this))
    {
        qDebug() << Q_FUNC_INFO;
        connect(m_timer, &QTimer::timeout, this, &Writer::onSend);
    }

    ~Writer()
    {
        qDebug() << Q_FUNC_INFO;
    }

signals:
    void send(const QString &msg);
    void taskFinished();

public slots:
    void startSending()
    {
        m_timer->start(5);
    }

private slots:
    void onSend()
    {
        emit send(QString("Package %1").arg(m_pkg++));

        if (m_pkg == 100) {
            m_timer->stop();
            emit taskFinished();
        }
    }

private:
    QTimer *m_timer;
    int m_pkg = 1;
};

#endif // CONTROLLER_H

main.cpp

#include <QThread>
#include <QApplication>

#include "controller.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Controller ctrl;

    QThread pThread;
    Writer pWriter;
    pWriter.moveToThread(&pThread);

    QObject::connect(&pThread, &QThread::started,    &pWriter,    &Writer::startSending);
    QObject::connect(&pWriter, &Writer::send,         &ctrl,      &Controller::put, Qt::QueuedConnection);
    QObject::connect(&pWriter, &Writer::taskFinished, [&a, &pThread](){
        pThread.quit();
        a.quit();
    });

    pThread.start();

    return a.exec();
}