Skip to content

Instantly share code, notes, and snippets.

@yossi-tahara

yossi-tahara/Common.h Secret

Last active Nov 1, 2020
Embed
What would you like to do?
第8回 Qtの重要な仕組みSignalとSlot(スレッド編2)
#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 <QDebug>
#include "Common.h"
#include "Containment.h"
Containment::Containment()
{
log("start Containment()");
connect(this, &Containment::testSignal, this, &Containment::testSlot, Qt::QueuedConnection);
connect(this, &Containment::autoConnectionSignal, this, &Containment::autoConnectionSlot);
moveToThread(&mThread);
mThread.start();
log("end Containment()");
}
Containment::~Containment()
{
log("start ~Containment()");
mThread.exit();
mThread.wait(3000);
log("end ~Containment()");
}
void Containment::testStart()
{
log("start testStart()");
emit autoConnectionSignal();
emit testSignal();
log("end testStart()");
}
void Containment::testSlot()
{
log("start testSlot()");
emit autoConnectionSignal();
log("end testSlot()");
}
void Containment::autoConnectionSlot()
{
log(" autoConnectionSlot()");
}
#ifndef SUBTHREAD_H
#define SUBTHREAD_H
#include <QObject>
#include <QThread>
class Containment : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(Containment)
public:
Containment();
~Containment();
void testStart();
signals:
void testSignal();
void autoConnectionSignal();
private slots:
void testSlot();
void autoConnectionSlot();
private:
QThread mThread;
};
#endif // SUBTHREAD_H
#include <QCoreApplication>
#include <QThread>
#include "Common.h"
#include "Inheritance.h"
Inheritance::Inheritance()
{
log("start Inheritance()");
connect(this, &Inheritance::queuedSignal, this, &Inheritance::queuedSlot, Qt::QueuedConnection);
// moveToThread(this);
start();
log("end Inheritance()");
}
Inheritance::~Inheritance()
{
log("start ~Inheritance()");
exit();
wait(3000);
log("end ~Inheritance()");
}
void Inheritance::run()
{
exec();
}
void Inheritance::testStart()
{
log("start testStart()");
emit queuedSignal();
log("end testStart()");
}
void Inheritance::queuedSlot()
{
log(" queuedSlot()");
}
#ifndef INHERITANCE_H
#define INHERITANCE_H
#include <QThread>
class Inheritance : public QThread
{
Q_OBJECT
Q_DISABLE_COPY(Inheritance)
public:
Inheritance();
~Inheritance();
void testStart();
signals:
void queuedSignal();
private slots:
void queuedSlot();
private:
void run();
};
#endif // INHERITANCE_H
#include <QCoreApplication>
#include <QTimer>
#include "Common.h"
#include "Inheritance.h"
#include "Containment.h"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
Inheritance aInheritance;
QTimer::singleShot(100,
[&aInheritance]()
{
log("---------------------- Inheritance");
aInheritance.testStart();
});
Containment aContainment;
QTimer::singleShot(200,
[&aContainment]()
{
log("---------------------- Containment");
aContainment.testStart();
});
QTimer::singleShot(300,
[&app]()
{
log("---------------------- finish");
app.exit();
});
return app.exec();
}
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 += \
Inheritance.cpp \
Containment.cpp \
main.cpp
HEADERS += \
Common.h \
Inheritance.h \
Containment.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.