Skip to content

Instantly share code, notes, and snippets.

@yossi-tahara
Last active Oct 31, 2020
Embed
What would you like to do?
第7回 Qtの重要な仕組みSignalとSlot(マルチスレッド編1)
#ifndef COMMON_H
#define COMMON_H
#include <QDebug>
#include <QDateTime>
#include <QThread>
inline void log(QString const& iMessage)
{
qDebug().noquote() << QDateTime::currentDateTime().toString("HH:mm:ss.zzz") << QThread::currentThreadId() << iMessage;
}
#endif // COMMON_H
#include <QCoreApplication>
#include "MainThread.h"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
MainThread aMainThread;
return app.exec();
}
#include <QCoreApplication>
#include <QThread>
#include "Common.h"
#include "MainThread.h"
MainThread::MainThread()
{
log("start MainThread()");
connect(&mSubThread, &SubThread::directSignal, this, &MainThread::directSlot, Qt::DirectConnection);
connect(&mSubThread, &SubThread::queuedSignal, this, &MainThread::queuedSlot, Qt::QueuedConnection);
connect(&mSubThread, &SubThread::blockingQueuedSignal, this, &MainThread::blockingQueuedSlot, Qt::BlockingQueuedConnection);
connect(&mSubThread, &SubThread::finished, this, &MainThread::finished, Qt::QueuedConnection);
mSubThread.start();
log("end MainThread()");
}
void MainThread::directSlot()
{
log("start directSlot()");
QThread::msleep(1000);
log("start directSlot()");
}
void MainThread::queuedSlot()
{
log("start queuedSlot()");
QThread::msleep(1000);
log("end queuedSlot()");
}
void MainThread::blockingQueuedSlot(QString const& iMessage)
{
log("start blockingQueuedSlot(): "+iMessage);
QThread::msleep(1000);
log("end blockingQueuedSlot(): "+iMessage);
}
void MainThread::finished()
{
log("start finished()");
qApp->quit();
log("end finished()");
}
#ifndef MAINTHREAD_H
#define MAINTHREAD_H
#include <QObject>
#include "SubThread.h"
class MainThread : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(MainThread)
public:
MainThread();
private slots:
void directSlot();
void queuedSlot();
void blockingQueuedSlot(QString const& iMessage);
void finished();
private:
SubThread mSubThread;
};
#endif // MAINTHREAD_H
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
MainThread.cpp \
SubThread.cpp \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
Common.h \
MainThread.h \
SubThread.h
#include <QDebug>
#include "Common.h"
#include "SubThread.h"
SubThread::SubThread()
{
log("start SubThread()");
connect(this, &SubThread::startSignal, this, &SubThread::startSlot, Qt::QueuedConnection);
this->moveToThread(&mThread);
mThread.start();
log("end SubThread()");
}
SubThread::~SubThread()
{
log("start ~SubThread()");
mThread.exit();
mThread.wait(3000);
log("end ~SubThread()");
}
void SubThread::start()
{
log("start start()");
emit startSignal();
log("end start()");
}
void SubThread::startSlot()
{
log("start startSlot()");
emit directSignal();
log(" startSlot(1)");
emit queuedSignal();
log(" startSlot(2)");
{
QString aMessage="This is local variable in SubThread";
emit blockingQueuedSignal(aMessage);
}
log(" startSlot(3)");
emit finished();
log("end startSlot()");
}
#ifndef SUBTHREAD_H
#define SUBTHREAD_H
#include <QObject>
#include <QThread>
class SubThread : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(SubThread)
public:
SubThread();
~SubThread();
void start();
signals:
void startSignal();
void finished();
void directSignal();
void queuedSignal();
void blockingQueuedSignal(QString const& iMessage);
private slots:
void startSlot();
private:
QThread mThread;
};
#endif // SUBTHREAD_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment