Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch5 code #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions 05-qt-meta-object-compiler/5-meta-object-compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
16 changes: 16 additions & 0 deletions 05-qt-meta-object-compiler/5-meta-object-compiler/Firefox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "Firefox.h"
#include <QDebug>

Firefox::Firefox(QObject *parent)
: QObject{parent}
{

}

void Firefox::browse(const QString &phrase)
{
qDebug() << "\nDisplaying results for " << phrase
<< "\nResult 1"
<< "\nResult 2"
<< "\nResult 3";
}
17 changes: 17 additions & 0 deletions 05-qt-meta-object-compiler/5-meta-object-compiler/Firefox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef FIREFOX_H
#define FIREFOX_H

#include <QObject>

class Firefox : public QObject
{
Q_OBJECT
public:
explicit Firefox(QObject *parent = nullptr);

public slots:
void browse(const QString &phrase);

};

#endif // FIREFOX_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "InternetExplorer.h"
#include <QDebug>

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...";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef INTERNETEXPLORER_H
#define INTERNETEXPLORER_H

#include <QObject>
#include <QTimer>

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "UserInteractor.h"
#include <QTextStream>
#include <QDebug>

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);
}
25 changes: 25 additions & 0 deletions 05-qt-meta-object-compiler/5-meta-object-compiler/UserInteractor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef USERINTERACTOR_H
#define USERINTERACTOR_H

#include <QObject>

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
27 changes: 27 additions & 0 deletions 05-qt-meta-object-compiler/5-meta-object-compiler/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <QCoreApplication>
#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();
}
26 changes: 26 additions & 0 deletions 05-qt-meta-object-compiler/initial/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
16 changes: 16 additions & 0 deletions 05-qt-meta-object-compiler/initial/Firefox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "Firefox.h"
#include <QDebug>

Firefox::Firefox(QObject *parent)
: QObject{parent}
{

}

void Firefox::browse(const QString &phrase)
{
qDebug() << "\nDisplaying results for " << phrase
<< "\nResult 1"
<< "\nResult 2"
<< "\nResult 3";
}
17 changes: 17 additions & 0 deletions 05-qt-meta-object-compiler/initial/Firefox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef FIREFOX_H
#define FIREFOX_H

#include <QObject>

class Firefox : public QObject
{
Q_OBJECT
public:
explicit Firefox(QObject *parent = nullptr);

public slots:
void browse(const QString &phrase);

};

#endif // FIREFOX_H
17 changes: 17 additions & 0 deletions 05-qt-meta-object-compiler/initial/InternetExplorer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "InternetExplorer.h"
#include <QDebug>

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...";
}
23 changes: 23 additions & 0 deletions 05-qt-meta-object-compiler/initial/InternetExplorer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef INTERNETEXPLORER_H
#define INTERNETEXPLORER_H

#include <QObject>
#include <QTimer>

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
20 changes: 20 additions & 0 deletions 05-qt-meta-object-compiler/initial/UserInteractor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "UserInteractor.h"
#include <QTextStream>
#include <QDebug>

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);
}
}
19 changes: 19 additions & 0 deletions 05-qt-meta-object-compiler/initial/UserInteractor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef USERINTERACTOR_H
#define USERINTERACTOR_H

#include <QObject>

class UserInteractor : public QObject
{
Q_OBJECT
public:
explicit UserInteractor(QObject *parent = nullptr);

void getInput();

signals:
void phraseTyped(const QString &phrase);

};

#endif // USERINTERACTOR_H
21 changes: 21 additions & 0 deletions 05-qt-meta-object-compiler/initial/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <QCoreApplication>
#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();
}