From fd408c5496b587d9785a8bd6b02f5db286ec3e43 Mon Sep 17 00:00:00 2001 From: Yihua Liu Date: Fri, 20 Oct 2023 15:49:34 +0800 Subject: [PATCH 1/2] Init ch5: copy ch3 to ch5 --- .../5-meta-object-compiler/CMakeLists.txt | 26 +++++++++++++++++++ .../5-meta-object-compiler/Firefox.cpp | 16 ++++++++++++ .../5-meta-object-compiler/Firefox.h | 17 ++++++++++++ .../InternetExplorer.cpp | 17 ++++++++++++ .../5-meta-object-compiler/InternetExplorer.h | 23 ++++++++++++++++ .../5-meta-object-compiler/UserInteractor.cpp | 20 ++++++++++++++ .../5-meta-object-compiler/UserInteractor.h | 19 ++++++++++++++ .../5-meta-object-compiler/main.cpp | 21 +++++++++++++++ .../initial/CMakeLists.txt | 26 +++++++++++++++++++ .../initial/Firefox.cpp | 16 ++++++++++++ 05-qt-meta-object-compiler/initial/Firefox.h | 17 ++++++++++++ .../initial/InternetExplorer.cpp | 17 ++++++++++++ .../initial/InternetExplorer.h | 23 ++++++++++++++++ .../initial/UserInteractor.cpp | 20 ++++++++++++++ .../initial/UserInteractor.h | 19 ++++++++++++++ 05-qt-meta-object-compiler/initial/main.cpp | 21 +++++++++++++++ 16 files changed, 318 insertions(+) create mode 100644 05-qt-meta-object-compiler/5-meta-object-compiler/CMakeLists.txt create mode 100644 05-qt-meta-object-compiler/5-meta-object-compiler/Firefox.cpp create mode 100644 05-qt-meta-object-compiler/5-meta-object-compiler/Firefox.h create mode 100644 05-qt-meta-object-compiler/5-meta-object-compiler/InternetExplorer.cpp create mode 100644 05-qt-meta-object-compiler/5-meta-object-compiler/InternetExplorer.h create mode 100644 05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.cpp create mode 100644 05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.h create mode 100644 05-qt-meta-object-compiler/5-meta-object-compiler/main.cpp create mode 100644 05-qt-meta-object-compiler/initial/CMakeLists.txt create mode 100644 05-qt-meta-object-compiler/initial/Firefox.cpp create mode 100644 05-qt-meta-object-compiler/initial/Firefox.h create mode 100644 05-qt-meta-object-compiler/initial/InternetExplorer.cpp create mode 100644 05-qt-meta-object-compiler/initial/InternetExplorer.h create mode 100644 05-qt-meta-object-compiler/initial/UserInteractor.cpp create mode 100644 05-qt-meta-object-compiler/initial/UserInteractor.h create mode 100644 05-qt-meta-object-compiler/initial/main.cpp diff --git a/05-qt-meta-object-compiler/5-meta-object-compiler/CMakeLists.txt b/05-qt-meta-object-compiler/5-meta-object-compiler/CMakeLists.txt new file mode 100644 index 0000000..e248971 --- /dev/null +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.14) + +project(3-signals-and-slots-in-qt LANGUAGES CXX) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(CMAKE_AUTOUIC ON) +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core) +find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core) + +add_executable(3-signals-and-slots-in-qt + main.cpp + UserInteractor.h UserInteractor.cpp + Firefox.h Firefox.cpp + InternetExplorer.h InternetExplorer.cpp +) +target_link_libraries(3-signals-and-slots-in-qt Qt${QT_VERSION_MAJOR}::Core) + +install(TARGETS 3-signals-and-slots-in-qt + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/05-qt-meta-object-compiler/5-meta-object-compiler/Firefox.cpp b/05-qt-meta-object-compiler/5-meta-object-compiler/Firefox.cpp new file mode 100644 index 0000000..4cefc1b --- /dev/null +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/Firefox.cpp @@ -0,0 +1,16 @@ +#include "Firefox.h" +#include + +Firefox::Firefox(QObject *parent) + : QObject{parent} +{ + +} + +void Firefox::browse(const QString &phrase) +{ + qDebug() << "\nDisplaying results for " << phrase + << "\nResult 1" + << "\nResult 2" + << "\nResult 3"; +} diff --git a/05-qt-meta-object-compiler/5-meta-object-compiler/Firefox.h b/05-qt-meta-object-compiler/5-meta-object-compiler/Firefox.h new file mode 100644 index 0000000..ab06347 --- /dev/null +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/Firefox.h @@ -0,0 +1,17 @@ +#ifndef FIREFOX_H +#define FIREFOX_H + +#include + +class Firefox : public QObject +{ + Q_OBJECT +public: + explicit Firefox(QObject *parent = nullptr); + +public slots: + void browse(const QString &phrase); + +}; + +#endif // FIREFOX_H diff --git a/05-qt-meta-object-compiler/5-meta-object-compiler/InternetExplorer.cpp b/05-qt-meta-object-compiler/5-meta-object-compiler/InternetExplorer.cpp new file mode 100644 index 0000000..388c096 --- /dev/null +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/InternetExplorer.cpp @@ -0,0 +1,17 @@ +#include "InternetExplorer.h" +#include + +InternetExplorer::InternetExplorer(QObject *parent) + : QObject{parent} +{ + m_timer.setInterval(10000); + m_timer.setSingleShot(true); + + connect(this, &InternetExplorer::browseRequested, &m_timer, qOverload<>(&QTimer::start)); + connect(&m_timer, &QTimer::timeout, this, &InternetExplorer::browse); +} + +void InternetExplorer::browse() +{ + qDebug() << "Internet explorer is not responding..."; +} diff --git a/05-qt-meta-object-compiler/5-meta-object-compiler/InternetExplorer.h b/05-qt-meta-object-compiler/5-meta-object-compiler/InternetExplorer.h new file mode 100644 index 0000000..6073031 --- /dev/null +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/InternetExplorer.h @@ -0,0 +1,23 @@ +#ifndef INTERNETEXPLORER_H +#define INTERNETEXPLORER_H + +#include +#include + +class InternetExplorer : public QObject +{ + Q_OBJECT +public: + explicit InternetExplorer(QObject *parent = nullptr); + +public slots: + void browse(); + +signals: + void browseRequested(const QString &phrase); + +private: + QTimer m_timer; +}; + +#endif // INTERNETEXPLORER_H diff --git a/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.cpp b/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.cpp new file mode 100644 index 0000000..89f0d2e --- /dev/null +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.cpp @@ -0,0 +1,20 @@ +#include "UserInteractor.h" +#include +#include + +UserInteractor::UserInteractor(QObject *parent) + : QObject{parent} +{ +} + +void UserInteractor::getInput() +{ + qDebug() << "\nType in your search phrase:\n"; + + QTextStream s(stdin); + const auto &phrase = s.readLine(); + + if (!phrase.isEmpty()) { + emit phraseTyped(phrase); + } +} diff --git a/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.h b/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.h new file mode 100644 index 0000000..87dd322 --- /dev/null +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.h @@ -0,0 +1,19 @@ +#ifndef USERINTERACTOR_H +#define USERINTERACTOR_H + +#include + +class UserInteractor : public QObject +{ + Q_OBJECT +public: + explicit UserInteractor(QObject *parent = nullptr); + + void getInput(); + +signals: + void phraseTyped(const QString &phrase); + +}; + +#endif // USERINTERACTOR_H diff --git a/05-qt-meta-object-compiler/5-meta-object-compiler/main.cpp b/05-qt-meta-object-compiler/5-meta-object-compiler/main.cpp new file mode 100644 index 0000000..a8bea43 --- /dev/null +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/main.cpp @@ -0,0 +1,21 @@ +#include +#include "UserInteractor.h" + +#include "Firefox.h" +#include "InternetExplorer.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + UserInteractor interactor; + Firefox firefox; + InternetExplorer internetExplorer; + + QObject::connect(&interactor, &UserInteractor::phraseTyped, &firefox, &Firefox::browse); + QObject::connect(&interactor, &UserInteractor::phraseTyped, &internetExplorer, &InternetExplorer::browseRequested); + + interactor.getInput(); + + return a.exec(); +} diff --git a/05-qt-meta-object-compiler/initial/CMakeLists.txt b/05-qt-meta-object-compiler/initial/CMakeLists.txt new file mode 100644 index 0000000..e248971 --- /dev/null +++ b/05-qt-meta-object-compiler/initial/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.14) + +project(3-signals-and-slots-in-qt LANGUAGES CXX) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(CMAKE_AUTOUIC ON) +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core) +find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core) + +add_executable(3-signals-and-slots-in-qt + main.cpp + UserInteractor.h UserInteractor.cpp + Firefox.h Firefox.cpp + InternetExplorer.h InternetExplorer.cpp +) +target_link_libraries(3-signals-and-slots-in-qt Qt${QT_VERSION_MAJOR}::Core) + +install(TARGETS 3-signals-and-slots-in-qt + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/05-qt-meta-object-compiler/initial/Firefox.cpp b/05-qt-meta-object-compiler/initial/Firefox.cpp new file mode 100644 index 0000000..4cefc1b --- /dev/null +++ b/05-qt-meta-object-compiler/initial/Firefox.cpp @@ -0,0 +1,16 @@ +#include "Firefox.h" +#include + +Firefox::Firefox(QObject *parent) + : QObject{parent} +{ + +} + +void Firefox::browse(const QString &phrase) +{ + qDebug() << "\nDisplaying results for " << phrase + << "\nResult 1" + << "\nResult 2" + << "\nResult 3"; +} diff --git a/05-qt-meta-object-compiler/initial/Firefox.h b/05-qt-meta-object-compiler/initial/Firefox.h new file mode 100644 index 0000000..ab06347 --- /dev/null +++ b/05-qt-meta-object-compiler/initial/Firefox.h @@ -0,0 +1,17 @@ +#ifndef FIREFOX_H +#define FIREFOX_H + +#include + +class Firefox : public QObject +{ + Q_OBJECT +public: + explicit Firefox(QObject *parent = nullptr); + +public slots: + void browse(const QString &phrase); + +}; + +#endif // FIREFOX_H diff --git a/05-qt-meta-object-compiler/initial/InternetExplorer.cpp b/05-qt-meta-object-compiler/initial/InternetExplorer.cpp new file mode 100644 index 0000000..388c096 --- /dev/null +++ b/05-qt-meta-object-compiler/initial/InternetExplorer.cpp @@ -0,0 +1,17 @@ +#include "InternetExplorer.h" +#include + +InternetExplorer::InternetExplorer(QObject *parent) + : QObject{parent} +{ + m_timer.setInterval(10000); + m_timer.setSingleShot(true); + + connect(this, &InternetExplorer::browseRequested, &m_timer, qOverload<>(&QTimer::start)); + connect(&m_timer, &QTimer::timeout, this, &InternetExplorer::browse); +} + +void InternetExplorer::browse() +{ + qDebug() << "Internet explorer is not responding..."; +} diff --git a/05-qt-meta-object-compiler/initial/InternetExplorer.h b/05-qt-meta-object-compiler/initial/InternetExplorer.h new file mode 100644 index 0000000..6073031 --- /dev/null +++ b/05-qt-meta-object-compiler/initial/InternetExplorer.h @@ -0,0 +1,23 @@ +#ifndef INTERNETEXPLORER_H +#define INTERNETEXPLORER_H + +#include +#include + +class InternetExplorer : public QObject +{ + Q_OBJECT +public: + explicit InternetExplorer(QObject *parent = nullptr); + +public slots: + void browse(); + +signals: + void browseRequested(const QString &phrase); + +private: + QTimer m_timer; +}; + +#endif // INTERNETEXPLORER_H diff --git a/05-qt-meta-object-compiler/initial/UserInteractor.cpp b/05-qt-meta-object-compiler/initial/UserInteractor.cpp new file mode 100644 index 0000000..89f0d2e --- /dev/null +++ b/05-qt-meta-object-compiler/initial/UserInteractor.cpp @@ -0,0 +1,20 @@ +#include "UserInteractor.h" +#include +#include + +UserInteractor::UserInteractor(QObject *parent) + : QObject{parent} +{ +} + +void UserInteractor::getInput() +{ + qDebug() << "\nType in your search phrase:\n"; + + QTextStream s(stdin); + const auto &phrase = s.readLine(); + + if (!phrase.isEmpty()) { + emit phraseTyped(phrase); + } +} diff --git a/05-qt-meta-object-compiler/initial/UserInteractor.h b/05-qt-meta-object-compiler/initial/UserInteractor.h new file mode 100644 index 0000000..87dd322 --- /dev/null +++ b/05-qt-meta-object-compiler/initial/UserInteractor.h @@ -0,0 +1,19 @@ +#ifndef USERINTERACTOR_H +#define USERINTERACTOR_H + +#include + +class UserInteractor : public QObject +{ + Q_OBJECT +public: + explicit UserInteractor(QObject *parent = nullptr); + + void getInput(); + +signals: + void phraseTyped(const QString &phrase); + +}; + +#endif // USERINTERACTOR_H diff --git a/05-qt-meta-object-compiler/initial/main.cpp b/05-qt-meta-object-compiler/initial/main.cpp new file mode 100644 index 0000000..a8bea43 --- /dev/null +++ b/05-qt-meta-object-compiler/initial/main.cpp @@ -0,0 +1,21 @@ +#include +#include "UserInteractor.h" + +#include "Firefox.h" +#include "InternetExplorer.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + UserInteractor interactor; + Firefox firefox; + InternetExplorer internetExplorer; + + QObject::connect(&interactor, &UserInteractor::phraseTyped, &firefox, &Firefox::browse); + QObject::connect(&interactor, &UserInteractor::phraseTyped, &internetExplorer, &InternetExplorer::browseRequested); + + interactor.getInput(); + + return a.exec(); +} From ff875c01bca742945df91f4cfdf8e78872e309aa Mon Sep 17 00:00:00 2001 From: Yihua Liu Date: Fri, 20 Oct 2023 16:56:19 +0800 Subject: [PATCH 2/2] add ch5 moc code --- .../5-meta-object-compiler/CMakeLists.txt | 2 +- .../5-meta-object-compiler/UserInteractor.cpp | 15 ++++++++++++++- .../5-meta-object-compiler/UserInteractor.h | 6 ++++++ .../5-meta-object-compiler/main.cpp | 10 ++++++++-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/05-qt-meta-object-compiler/5-meta-object-compiler/CMakeLists.txt b/05-qt-meta-object-compiler/5-meta-object-compiler/CMakeLists.txt index e248971..a8b2212 100644 --- a/05-qt-meta-object-compiler/5-meta-object-compiler/CMakeLists.txt +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.14) -project(3-signals-and-slots-in-qt LANGUAGES CXX) +project(5-meta-object-compiler LANGUAGES CXX) set(CMAKE_INCLUDE_CURRENT_DIR ON) diff --git a/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.cpp b/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.cpp index 89f0d2e..38e6f39 100644 --- a/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.cpp +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.cpp @@ -15,6 +15,19 @@ void UserInteractor::getInput() const auto &phrase = s.readLine(); if (!phrase.isEmpty()) { - emit phraseTyped(phrase); + setPhrase(phrase); } } + +QString UserInteractor::phrase() const +{ + return m_phrase; +} + +void UserInteractor::setPhrase(const QString &newPhrase) +{ + if (m_phrase == newPhrase) + return; + m_phrase = newPhrase; + emit phraseTyped(m_phrase); +} diff --git a/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.h b/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.h index 87dd322..ba9e7cf 100644 --- a/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.h +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.h @@ -6,14 +6,20 @@ class UserInteractor : public QObject { Q_OBJECT + Q_PROPERTY(QString phrase READ phrase WRITE setPhrase NOTIFY phraseTyped) public: explicit UserInteractor(QObject *parent = nullptr); void getInput(); + QString phrase() const; + void setPhrase(const QString &newPhrase); + signals: void phraseTyped(const QString &phrase); +private: + QString m_phrase; }; #endif // USERINTERACTOR_H diff --git a/05-qt-meta-object-compiler/5-meta-object-compiler/main.cpp b/05-qt-meta-object-compiler/5-meta-object-compiler/main.cpp index a8bea43..6555db5 100644 --- a/05-qt-meta-object-compiler/5-meta-object-compiler/main.cpp +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/main.cpp @@ -3,6 +3,7 @@ #include "Firefox.h" #include "InternetExplorer.h" +#include "qvariant.h" int main(int argc, char *argv[]) { @@ -12,8 +13,13 @@ int main(int argc, char *argv[]) Firefox firefox; InternetExplorer internetExplorer; - QObject::connect(&interactor, &UserInteractor::phraseTyped, &firefox, &Firefox::browse); - QObject::connect(&interactor, &UserInteractor::phraseTyped, &internetExplorer, &InternetExplorer::browseRequested); + firefox.setProperty("version", "2.12.12"); + + QObject::connect(&interactor, &UserInteractor::phraseTyped, &a, [&]() { + QObject *obj = &interactor; + qDebug() << obj->property("phrase"); + qDebug() << firefox.property("version"); + }); interactor.getInput();