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..a8b2212 --- /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(5-meta-object-compiler 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..38e6f39 --- /dev/null +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.cpp @@ -0,0 +1,33 @@ +#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()) { + 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 new file mode 100644 index 0000000..ba9e7cf --- /dev/null +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.h @@ -0,0 +1,25 @@ +#ifndef USERINTERACTOR_H +#define USERINTERACTOR_H + +#include + +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 new file mode 100644 index 0000000..6555db5 --- /dev/null +++ b/05-qt-meta-object-compiler/5-meta-object-compiler/main.cpp @@ -0,0 +1,27 @@ +#include +#include "UserInteractor.h" + +#include "Firefox.h" +#include "InternetExplorer.h" +#include "qvariant.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + UserInteractor interactor; + Firefox firefox; + InternetExplorer internetExplorer; + + 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(); + + 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(); +}