-
-
Save yossi-tahara/db8bd13024f9b1a17bab6a564fb1314c to your computer and use it in GitHub Desktop.
第8回 Qtの重要な仕組みSignalとSlot(スレッド編2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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()"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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()"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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