├── qt6e-27 └── readme.txt ├── qt6e-4 ├── drawing │ ├── moc.png │ └── moc.xml ├── test.cpp ├── test.h ├── main.cpp └── CMakeLists.txt ├── qt6e-5 ├── cat.cpp ├── cat.h ├── CMakeLists.txt └── main.cpp ├── qt6e-15 ├── kitten.cpp ├── kitten.h ├── CMakeLists.txt └── main.cpp ├── qt6e-12 ├── test.cpp ├── test.h ├── CMakeLists.txt └── main.cpp ├── qt6e-28 ├── main.cpp ├── dialog.h ├── dialog.cpp ├── CMakeLists.txt └── dialog.ui ├── qt6e-7 ├── test.cpp ├── test.h ├── CMakeLists.txt └── main.cpp ├── qt6e-6 ├── test.h ├── test.cpp ├── CMakeLists.txt └── main.cpp ├── qt6e-8 ├── test.cpp ├── test.h ├── CMakeLists.txt └── main.cpp ├── README.md ├── qt6e-21 ├── worker.h ├── worker.cpp ├── CMakeLists.txt └── main.cpp ├── qt6e-23 ├── main.cpp ├── manager.h ├── worker.h ├── CMakeLists.txt ├── worker.cpp └── manager.cpp ├── qt6e-13 ├── cat.h ├── cat.cpp ├── CMakeLists.txt └── main.cpp ├── qt6e-3 ├── CMakeLists.txt └── main.cpp ├── qt6e-9 ├── CMakeLists.txt └── main.cpp ├── qt6e-10 ├── CMakeLists.txt └── main.cpp ├── qt6e-11 ├── CMakeLists.txt └── main.cpp ├── qt6e-14 ├── CMakeLists.txt └── main.cpp ├── qt6e-16 ├── CMakeLists.txt └── main.cpp ├── qt6e-17 ├── CMakeLists.txt └── main.cpp ├── qt6e-18 ├── CMakeLists.txt └── main.cpp ├── qt6e-19 ├── CMakeLists.txt └── main.cpp ├── qt6e-22 ├── CMakeLists.txt ├── worker.h ├── main.cpp └── worker.cpp ├── qt6e-25 ├── CMakeLists.txt ├── worker.h ├── worker.cpp └── main.cpp ├── qt6e-26 ├── worker.h ├── CMakeLists.txt ├── worker.cpp └── main.cpp ├── qt6e-20 ├── CMakeLists.txt ├── inventory.h ├── main.cpp └── inventory.cpp └── qt6e-24 ├── CMakeLists.txt └── main.cpp /qt6e-27/readme.txt: -------------------------------------------------------------------------------- 1 | There is no code for this video :) 2 | -------------------------------------------------------------------------------- /qt6e-4/drawing/moc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/voidrealms/qt6/HEAD/qt6e-4/drawing/moc.png -------------------------------------------------------------------------------- /qt6e-5/cat.cpp: -------------------------------------------------------------------------------- 1 | #include "cat.h" 2 | 3 | Cat::Cat(QObject *parent) : QObject(parent) 4 | { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /qt6e-15/kitten.cpp: -------------------------------------------------------------------------------- 1 | #include "kitten.h" 2 | 3 | Kitten::Kitten(QObject *parent) : QObject(parent) 4 | { 5 | 6 | } 7 | 8 | void Kitten::meow() 9 | { 10 | qWarning() << "I am hungry"; 11 | } 12 | -------------------------------------------------------------------------------- /qt6e-4/test.cpp: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | Test::Test(QObject *parent) : QObject(parent) 4 | { 5 | 6 | } 7 | 8 | void Test::dostuff() 9 | { 10 | qInfo() << "Doing stuff!"; 11 | emit close(); 12 | } 13 | -------------------------------------------------------------------------------- /qt6e-12/test.cpp: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | Test::Test(QObject *parent) : QObject(parent) 4 | { 5 | qInfo() << this << "Constructed"; 6 | } 7 | 8 | Test::~Test() 9 | { 10 | qInfo() << this << "Deconstructed"; 11 | } 12 | -------------------------------------------------------------------------------- /qt6e-28/main.cpp: -------------------------------------------------------------------------------- 1 | #include "dialog.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | Dialog w; 9 | w.show(); 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /qt6e-7/test.cpp: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | Test::Test(QObject *parent) : QObject(parent) 4 | { 5 | qInfo() << this << "Constructed" << parent; 6 | } 7 | 8 | Test::~Test() 9 | { 10 | qInfo() << this << "Deconstructed" << parent(); 11 | } 12 | -------------------------------------------------------------------------------- /qt6e-5/cat.h: -------------------------------------------------------------------------------- 1 | #ifndef CAT_H 2 | #define CAT_H 3 | 4 | #include 5 | 6 | class Cat : public QObject 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit Cat(QObject *parent = nullptr); 11 | 12 | signals: 13 | 14 | }; 15 | 16 | #endif // CAT_H 17 | -------------------------------------------------------------------------------- /qt6e-12/test.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_H 2 | #define TEST_H 3 | 4 | #include 5 | #include 6 | 7 | class Test : public QObject 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit Test(QObject *parent = nullptr); 12 | ~Test(); 13 | signals: 14 | 15 | }; 16 | 17 | #endif // TEST_H 18 | -------------------------------------------------------------------------------- /qt6e-6/test.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_H 2 | #define TEST_H 3 | 4 | #include 5 | #include 6 | 7 | class Test : public QObject 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit Test(QObject *parent = nullptr); 12 | ~Test(); 13 | signals: 14 | 15 | }; 16 | 17 | #endif // TEST_H 18 | -------------------------------------------------------------------------------- /qt6e-7/test.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_H 2 | #define TEST_H 3 | 4 | #include 5 | #include 6 | 7 | class Test : public QObject 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit Test(QObject *parent = nullptr); 12 | ~Test(); 13 | 14 | signals: 15 | 16 | }; 17 | 18 | #endif // TEST_H 19 | -------------------------------------------------------------------------------- /qt6e-15/kitten.h: -------------------------------------------------------------------------------- 1 | #ifndef KITTEN_H 2 | #define KITTEN_H 3 | 4 | #include 5 | #include 6 | 7 | class Kitten : public QObject 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit Kitten(QObject *parent = nullptr); 12 | 13 | void meow(); 14 | 15 | signals: 16 | 17 | }; 18 | 19 | #endif // KITTEN_H 20 | -------------------------------------------------------------------------------- /qt6e-8/test.cpp: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | Test::Test(QObject *parent) : QObject(parent) 4 | { 5 | qInfo() << this << "Constructed" << parent; 6 | } 7 | 8 | Test::~Test() 9 | { 10 | qInfo() << this << "Deconstructed" << parent(); 11 | } 12 | 13 | void Test::message(QString value) 14 | { 15 | qInfo() << this << value; 16 | } 17 | -------------------------------------------------------------------------------- /qt6e-8/test.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_H 2 | #define TEST_H 3 | 4 | #include 5 | #include 6 | 7 | class Test : public QObject 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit Test(QObject *parent = nullptr); 12 | ~Test(); 13 | 14 | void message(QString value = ""); 15 | 16 | signals: 17 | 18 | }; 19 | 20 | #endif // TEST_H 21 | -------------------------------------------------------------------------------- /qt6e-4/test.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_H 2 | #define TEST_H 3 | 4 | #include 5 | #include 6 | 7 | class Test : public QObject 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit Test(QObject *parent = nullptr); 12 | 13 | signals: 14 | void close(); 15 | 16 | public slots: 17 | void dostuff(); 18 | 19 | }; 20 | 21 | #endif // TEST_H 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # qt6 2 | 3 | Qt 6 youtube videos by Bryan Cairns 4 | 5 | Web: http://www.voidrealms.com
6 | Github: https://github.com/voidrealms
7 | Facebook: https://www.facebook.com/groups/1400884323467285
8 | Udemy: https://www.udemy.com/user/bryan-cairns/
9 | Youtube: https://www.youtube.com/playlist?list=PLUbFnGajtZlXbrbdlraCe3LMC_YH5abao
10 | -------------------------------------------------------------------------------- /qt6e-6/test.cpp: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | Test::Test(QObject *parent) : QObject(parent) 4 | { 5 | qInfo() << this << "Constructed" << parent; 6 | } 7 | 8 | Test::~Test() 9 | { 10 | foreach(QObject* child, children()) 11 | { 12 | qInfo() << this << " - child - " << child; 13 | } 14 | 15 | qInfo() << this << "Deconstructed" << parent(); 16 | } 17 | -------------------------------------------------------------------------------- /qt6e-21/worker.h: -------------------------------------------------------------------------------- 1 | #ifndef WORKER_H 2 | #define WORKER_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Worker : public QObject 9 | { 10 | Q_OBJECT 11 | public: 12 | explicit Worker(QObject *parent = nullptr); 13 | 14 | ~Worker(); 15 | 16 | signals: 17 | 18 | public slots: 19 | void run(); 20 | 21 | }; 22 | 23 | #endif // WORKER_H 24 | -------------------------------------------------------------------------------- /qt6e-23/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Thread pools 4 | Fast 5 | Efficient 6 | Easy 7 | 8 | */ 9 | 10 | #include 11 | #include 12 | #include "manager.h" 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | QCoreApplication a(argc, argv); 17 | 18 | QThread::currentThread()->setObjectName("Main Thread"); 19 | Manager manager; 20 | manager.start(); 21 | 22 | return a.exec(); 23 | } 24 | -------------------------------------------------------------------------------- /qt6e-13/cat.h: -------------------------------------------------------------------------------- 1 | #ifndef CAT_H 2 | #define CAT_H 3 | 4 | #include 5 | #include 6 | 7 | class Cat : public QObject 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit Cat(QObject *parent = nullptr); 12 | ~Cat(); 13 | 14 | QString name() const; 15 | void setName(const QString &name); 16 | 17 | int age() const; 18 | void setAge(int age); 19 | 20 | signals: 21 | 22 | private: 23 | QString m_name; 24 | int m_age; 25 | 26 | }; 27 | 28 | #endif // CAT_H 29 | -------------------------------------------------------------------------------- /qt6e-4/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "test.h" 4 | #include 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | QCoreApplication a(argc, argv); 9 | 10 | Test test; 11 | 12 | //Connect the signal and the slot 13 | QObject::connect(&test,&Test::close,&a,&QCoreApplication::quit,Qt::QueuedConnection); 14 | 15 | test.dostuff(); 16 | 17 | int value = a.exec(); 18 | 19 | qInfo() << "Exit value:" << value; 20 | 21 | return value; 22 | } 23 | -------------------------------------------------------------------------------- /qt6e-23/manager.h: -------------------------------------------------------------------------------- 1 | #ifndef MANAGER_H 2 | #define MANAGER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "worker.h" 10 | 11 | class Manager : public QObject 12 | { 13 | Q_OBJECT 14 | public: 15 | explicit Manager(QObject *parent = nullptr); 16 | 17 | signals: 18 | void work(); //not the best way 19 | 20 | public slots: 21 | void start(); 22 | void started(); 23 | void finished(); 24 | 25 | }; 26 | 27 | #endif // MANAGER_H 28 | -------------------------------------------------------------------------------- /qt6e-3/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6-e3 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6-e3 18 | main.cpp 19 | ) 20 | target_link_libraries(qt6-e3 Qt${QT_VERSION_MAJOR}::Core) 21 | -------------------------------------------------------------------------------- /qt6e-9/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-9 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-9 18 | main.cpp 19 | ) 20 | target_link_libraries(qt6e-9 Qt${QT_VERSION_MAJOR}::Core) 21 | -------------------------------------------------------------------------------- /qt6e-10/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-10 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-10 18 | main.cpp 19 | ) 20 | target_link_libraries(qt6e-10 Qt${QT_VERSION_MAJOR}::Core) 21 | -------------------------------------------------------------------------------- /qt6e-11/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-11 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-11 18 | main.cpp 19 | ) 20 | target_link_libraries(qt6e-11 Qt${QT_VERSION_MAJOR}::Core) 21 | -------------------------------------------------------------------------------- /qt6e-13/cat.cpp: -------------------------------------------------------------------------------- 1 | #include "cat.h" 2 | 3 | Cat::Cat(QObject *parent) : QObject(parent) 4 | { 5 | qInfo() << this << "Constructed"; 6 | } 7 | 8 | Cat::~Cat() 9 | { 10 | qInfo() << this << "Deconstructed"; 11 | } 12 | 13 | QString Cat::name() const 14 | { 15 | return m_name; 16 | } 17 | 18 | void Cat::setName(const QString &name) 19 | { 20 | m_name = name; 21 | setObjectName(m_name); 22 | } 23 | 24 | int Cat::age() const 25 | { 26 | return m_age; 27 | } 28 | 29 | void Cat::setAge(int age) 30 | { 31 | m_age = age; 32 | } 33 | -------------------------------------------------------------------------------- /qt6e-14/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-14 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-14 18 | main.cpp 19 | ) 20 | target_link_libraries(qt6e-14 Qt${QT_VERSION_MAJOR}::Core) 21 | -------------------------------------------------------------------------------- /qt6e-16/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-16 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-16 18 | main.cpp 19 | ) 20 | target_link_libraries(qt6e-16 Qt${QT_VERSION_MAJOR}::Core) 21 | -------------------------------------------------------------------------------- /qt6e-17/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-17 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-17 18 | main.cpp 19 | ) 20 | target_link_libraries(qt6e-17 Qt${QT_VERSION_MAJOR}::Core) 21 | -------------------------------------------------------------------------------- /qt6e-18/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-18 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-18 18 | main.cpp 19 | ) 20 | target_link_libraries(qt6e-18 Qt${QT_VERSION_MAJOR}::Core) 21 | -------------------------------------------------------------------------------- /qt6e-19/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-19 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-19 18 | main.cpp 19 | ) 20 | target_link_libraries(qt6e-19 Qt${QT_VERSION_MAJOR}::Core) 21 | -------------------------------------------------------------------------------- /qt6e-21/worker.cpp: -------------------------------------------------------------------------------- 1 | #include "worker.h" 2 | 3 | Worker::Worker(QObject *parent) : QObject(parent) 4 | { 5 | qInfo() << this << "Constructed" << QThread::currentThread(); 6 | } 7 | 8 | Worker::~Worker() 9 | { 10 | qInfo() << this << "Deconstructed" << QThread::currentThread(); 11 | } 12 | 13 | void Worker::run() 14 | { 15 | for(int i = 0; i < 10; i++) 16 | { 17 | qInfo() << "Working" << QString::number(i) << QThread::currentThread(); 18 | QThread::currentThread()->msleep(500); 19 | } 20 | 21 | this->deleteLater(); 22 | } 23 | -------------------------------------------------------------------------------- /qt6e-23/worker.h: -------------------------------------------------------------------------------- 1 | #ifndef WORKER_H 2 | #define WORKER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class Worker : public QObject, public QRunnable 10 | { 11 | Q_OBJECT 12 | public: 13 | explicit Worker(QObject *parent = nullptr); 14 | ~Worker(); 15 | 16 | signals: 17 | void started(); 18 | void finished(); 19 | 20 | public slots: 21 | void work(); 22 | 23 | 24 | // QRunnable interface 25 | public: 26 | void run() Q_DECL_OVERRIDE; 27 | }; 28 | 29 | #endif // WORKER_H 30 | -------------------------------------------------------------------------------- /qt6e-28/dialog.h: -------------------------------------------------------------------------------- 1 | #ifndef DIALOG_H 2 | #define DIALOG_H 3 | 4 | #include 5 | #include 6 | 7 | QT_BEGIN_NAMESPACE 8 | namespace Ui { class Dialog; } 9 | QT_END_NAMESPACE 10 | 11 | class Dialog : public QDialog 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | Dialog(QWidget *parent = nullptr); 17 | ~Dialog(); 18 | 19 | private slots: 20 | void on_cmbType_currentIndexChanged(int index); 21 | 22 | void on_btnCalulate_clicked(); 23 | 24 | private: 25 | Ui::Dialog *ui; 26 | QMap m_types; 27 | }; 28 | #endif // DIALOG_H 29 | -------------------------------------------------------------------------------- /qt6e-5/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qte-5 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qte-5 18 | main.cpp 19 | cat.h 20 | cat.cpp 21 | ) 22 | target_link_libraries(qte-5 Qt${QT_VERSION_MAJOR}::Core) 23 | -------------------------------------------------------------------------------- /qt6e-12/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-12 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-12 18 | main.cpp 19 | test.h 20 | test.cpp 21 | ) 22 | target_link_libraries(qt6e-12 Qt${QT_VERSION_MAJOR}::Core) 23 | -------------------------------------------------------------------------------- /qt6e-13/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-13 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-13 18 | main.cpp 19 | cat.h 20 | cat.cpp 21 | ) 22 | target_link_libraries(qt6e-13 Qt${QT_VERSION_MAJOR}::Core) 23 | -------------------------------------------------------------------------------- /qt6e-15/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-15 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-15 18 | main.cpp 19 | kitten.cpp 20 | kitten.h 21 | ) 22 | target_link_libraries(qt6e-15 Qt${QT_VERSION_MAJOR}::Core) 23 | -------------------------------------------------------------------------------- /qt6e-21/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-21 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-21 18 | main.cpp 19 | worker.cpp 20 | worker.h 21 | ) 22 | target_link_libraries(qt6e-21 Qt${QT_VERSION_MAJOR}::Core) 23 | -------------------------------------------------------------------------------- /qt6e-22/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-22 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-22 18 | main.cpp 19 | worker.cpp 20 | worker.h 21 | ) 22 | target_link_libraries(qt6e-22 Qt${QT_VERSION_MAJOR}::Core) 23 | -------------------------------------------------------------------------------- /qt6e-25/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-25 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-25 18 | main.cpp 19 | worker.cpp 20 | worker.h 21 | ) 22 | target_link_libraries(qt6e-25 Qt${QT_VERSION_MAJOR}::Core) 23 | -------------------------------------------------------------------------------- /qt6e-26/worker.h: -------------------------------------------------------------------------------- 1 | #ifndef WORKER_H 2 | #define WORKER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | class Worker : public QObject, public QRunnable 13 | { 14 | Q_OBJECT 15 | public: 16 | explicit Worker(QObject *parent = nullptr); 17 | ~Worker(); 18 | 19 | signals: 20 | void finished(); 21 | 22 | public: 23 | void run() Q_DECL_OVERRIDE; 24 | 25 | public slots: 26 | void work(); 27 | 28 | }; 29 | 30 | #endif // WORKER_H 31 | -------------------------------------------------------------------------------- /qt6e-4/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6-ep-4 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6-ep-4 18 | main.cpp 19 | test.h 20 | test.cpp 21 | ) 22 | target_link_libraries(qt6-ep-4 Qt${QT_VERSION_MAJOR}::Core) 23 | -------------------------------------------------------------------------------- /qt6e-6/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6-ep-6 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6-ep-6 18 | main.cpp 19 | test.h 20 | test.cpp 21 | ) 22 | target_link_libraries(qt6-ep-6 Qt${QT_VERSION_MAJOR}::Core) 23 | -------------------------------------------------------------------------------- /qt6e-7/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6-ep-7 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6-ep-7 18 | main.cpp 19 | test.h 20 | test.cpp 21 | ) 22 | target_link_libraries(qt6-ep-7 Qt${QT_VERSION_MAJOR}::Core) 23 | -------------------------------------------------------------------------------- /qt6e-8/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6-ep-8 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6-ep-8 18 | main.cpp 19 | test.h 20 | test.cpp 21 | ) 22 | target_link_libraries(qt6-ep-8 Qt${QT_VERSION_MAJOR}::Core) 23 | -------------------------------------------------------------------------------- /qt6e-20/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-20 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-20 18 | main.cpp 19 | inventory.cpp 20 | inventory.h 21 | ) 22 | target_link_libraries(qt6e-20 Qt${QT_VERSION_MAJOR}::Core) 23 | -------------------------------------------------------------------------------- /qt6e-24/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-24 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Concurrent REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Concurrent REQUIRED) 16 | 17 | add_executable(qt6e-24 18 | main.cpp 19 | ) 20 | target_link_libraries(qt6e-24 Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Concurrent) 21 | -------------------------------------------------------------------------------- /qt6e-5/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cat.h" 4 | #include 5 | 6 | void test(Cat &cat) 7 | { 8 | qInfo() << "Addr " << &cat; 9 | } 10 | 11 | void test2(Cat *cat) 12 | { 13 | qInfo() << "Ptr " << cat; 14 | } 15 | 16 | int main(int argc, char *argv[]) 17 | { 18 | QCoreApplication a(argc, argv); 19 | 20 | Cat kitty; 21 | kitty.setObjectName("kitty"); 22 | 23 | Cat death; 24 | death.setObjectName("Death"); 25 | 26 | test(kitty); 27 | test2(&kitty); 28 | 29 | test(death); 30 | test2(&death); 31 | 32 | return a.exec(); 33 | } 34 | -------------------------------------------------------------------------------- /qt6e-20/inventory.h: -------------------------------------------------------------------------------- 1 | #ifndef INVENTORY_H 2 | #define INVENTORY_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class Inventory : public QObject 11 | { 12 | Q_OBJECT 13 | public: 14 | explicit Inventory(QObject *parent = nullptr); 15 | 16 | void add(QString name, int qty); 17 | void remove(QString name, int qty); 18 | void list(); 19 | 20 | signals: 21 | 22 | public slots: 23 | void save(); 24 | void load(); 25 | 26 | private: 27 | QMap m_items; 28 | 29 | }; 30 | 31 | #endif // INVENTORY_H 32 | -------------------------------------------------------------------------------- /qt6e-23/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-23 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED) 16 | 17 | add_executable(qt6e-23 18 | main.cpp 19 | worker.cpp 20 | worker.h 21 | manager.cpp 22 | manager.h 23 | ) 24 | target_link_libraries(qt6e-23 Qt${QT_VERSION_MAJOR}::Core) 25 | -------------------------------------------------------------------------------- /qt6e-25/worker.h: -------------------------------------------------------------------------------- 1 | #ifndef WORKER_H 2 | #define WORKER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class Worker : public QObject, public QRunnable 12 | { 13 | Q_OBJECT 14 | public: 15 | explicit Worker(QObject *parent = nullptr); 16 | 17 | signals: 18 | 19 | public: 20 | void run() Q_DECL_OVERRIDE; 21 | 22 | void setCount(int *newCount); 23 | 24 | void setMutex(QMutex *newMutex); 25 | 26 | private: 27 | int *m_count; 28 | QMutex *m_mutex; 29 | }; 30 | 31 | #endif // WORKER_H 32 | -------------------------------------------------------------------------------- /qt6e-26/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | 3 | project(qt6e-26 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Concurrent REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Concurrent REQUIRED) 16 | 17 | add_executable(qt6e-26 18 | main.cpp 19 | worker.cpp 20 | worker.h 21 | ) 22 | target_link_libraries(qt6e-26 Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Concurrent) 23 | -------------------------------------------------------------------------------- /qt6e-22/worker.h: -------------------------------------------------------------------------------- 1 | #ifndef WORKER_H 2 | #define WORKER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class Worker : public QObject 11 | { 12 | Q_OBJECT 13 | public: 14 | explicit Worker(QObject *parent = nullptr); 15 | 16 | bool producer() const; 17 | void setProducer(bool newProducer); 18 | 19 | signals: 20 | void produced(int value); 21 | void finished(); 22 | 23 | public slots: 24 | void consume(int value); 25 | void start(); 26 | void quit(); 27 | 28 | private: 29 | QTimer *m_timer; //Timers can not be started from another thread 30 | bool m_producer; 31 | int m_count; 32 | 33 | void timeout(); 34 | 35 | }; 36 | 37 | #endif // WORKER_H 38 | -------------------------------------------------------------------------------- /qt6e-25/worker.cpp: -------------------------------------------------------------------------------- 1 | #include "worker.h" 2 | 3 | Worker::Worker(QObject *parent) : QObject(parent) 4 | { 5 | 6 | } 7 | 8 | 9 | void Worker::run() 10 | { 11 | //Threadpool runs this code 12 | 13 | for(int i = 0; i < 10; i++) 14 | { 15 | //m_mutex->lock(); 16 | QMutexLocker locker(m_mutex); //Lock - pay attention to SCOPE 17 | int value = *m_count; 18 | value++; 19 | *m_count = value; //DANGER!!!! 20 | 21 | qInfo() << QThread::currentThread() << *m_count; 22 | //unlock 23 | //m_mutex->unlock(); 24 | } 25 | 26 | qInfo() << QThread::currentThread() << "Done"; 27 | } 28 | 29 | void Worker::setCount(int *newCount) 30 | { 31 | m_count = newCount; 32 | } 33 | 34 | void Worker::setMutex(QMutex *newMutex) 35 | { 36 | m_mutex = newMutex; 37 | } 38 | -------------------------------------------------------------------------------- /qt6e-23/worker.cpp: -------------------------------------------------------------------------------- 1 | #include "worker.h" 2 | 3 | Worker::Worker(QObject *parent) : QObject(parent) 4 | { 5 | qInfo() << "Created" << this << QThread::currentThread(); 6 | } 7 | 8 | Worker::~Worker() 9 | { 10 | qInfo() << "Destroyed" << this << QThread::currentThread(); 11 | } 12 | 13 | void Worker::work() 14 | { 15 | qInfo() << "Work" << this << QThread::currentThread(); 16 | } 17 | 18 | void Worker::run() 19 | { 20 | //Starting in the thread 21 | qInfo() << "Starting" << this << QThread::currentThread(); 22 | emit started(); 23 | 24 | for(int i = 0; i < 5; i++) 25 | { 26 | qInfo() << "Running" << this << QThread::currentThread(); 27 | QThread::currentThread()->msleep(1000); 28 | } 29 | 30 | qInfo() << "Finishing" << this << QThread::currentThread(); 31 | emit finished(); 32 | //Finishing in the thread 33 | } 34 | -------------------------------------------------------------------------------- /qt6e-6/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Qt memory managment 3 | Parent child relationship 4 | */ 5 | 6 | #include 7 | #include 8 | #include "test.h" 9 | 10 | //Setting in the constructor 11 | Test* getTest(QObject* parent) 12 | { 13 | return new Test(parent); 14 | } 15 | 16 | //Setting up after constructor 17 | Test* getTest() 18 | { 19 | return new Test(); //No parent 20 | } 21 | 22 | int main(int argc, char *argv[]) 23 | { 24 | QCoreApplication a(argc, argv); 25 | 26 | QTimer timer; 27 | timer.singleShot(3000,&a,&QCoreApplication::quit); 28 | 29 | //Setting in the constructor 30 | Test* dog = getTest(&a); 31 | dog->setObjectName("Spot"); 32 | 33 | //Setting up after constructor 34 | Test* cat = getTest(); 35 | cat->setObjectName("Princess"); 36 | cat->setParent(dog); 37 | 38 | int value = a.exec(); 39 | qInfo() << "Exit code:" << value; 40 | return value; 41 | } 42 | -------------------------------------------------------------------------------- /qt6e-25/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Mutex and Mutex locker 4 | 5 | When threads share resources 6 | Because some people want to watch the world burn 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "worker.h" 15 | 16 | int main(int argc, char *argv[]) 17 | { 18 | QCoreApplication a(argc, argv); 19 | 20 | int count = 0; //Shared resource 21 | int max = 5000; //Number of threads 22 | 23 | QMutex mutex; 24 | 25 | for(int i = 0; i < max; i++) 26 | { 27 | Worker *worker = new Worker(); 28 | worker->setCount(&count); 29 | worker->setMutex(&mutex); 30 | worker->setAutoDelete(true); 31 | 32 | QThreadPool::globalInstance()->start(worker); 33 | } 34 | 35 | QThreadPool::globalInstance()->waitForDone(); 36 | qInfo() << "Count:" << count; 37 | 38 | 39 | return a.exec(); 40 | } 41 | -------------------------------------------------------------------------------- /qt6e-4/drawing/moc.xml: -------------------------------------------------------------------------------- 1 | 3Zddb5swFIZ/DZeTzEcovWxp0k1qtErRtGsrnII3w0HGlLBfPxNsiGU1zaRoaZOLCL8+h2O/fo4lvDAtd4+C1sUaM+BeQLKdFz54QXCTBOp/EPpRiHx/FHLBslE6EDbsD2iRaLVlGTRWoETkktW2uMWqgq20NCoEdnbYC3K7ak1zcITNlnJX/ckyWYxqsiCz/hVYXpjKPtEzJTXBWmgKmmF3IIVLL0wFohyfyl0KfPDO+DLmrd6YnRYmoJKnJHwjd8+LxcN9UKc/HvtMNuu76ks4vuWV8lZvWC9W9sYByJQheohCFphjRflyVu8FtlUGQxmiRnPME2KtRF+Jv0DKXp8ubSUqqZAl17NjzaHQm3vTUoOt2MKRDRlGqMhBHokLphNQ5AKWIEWv8gRwKtmrvQ6qGcqnuNlm9aCd/gfXfcf1zbivgKSqgZwjsA3uCiZhU9O9D53qOtvMF8Z5ihzFPjckZKV+g46VNOdoFgBCwu64565HOiHWXOvGNh3RzV3iG/SLgw4xaWc3dXFtKAcnohxdEuXAcf1ZQC1wC02jEPwUJH84lG+uDeXoRJTjS6IcOa6vv6eO8e8wa7tuk+oQ7SerFUkmq88KsX97aYpvr43i+ESKk0tSHDuup1jWjIN7GZ8T5elyPgPKUfTB7uPE8fSJVb8/kaPx/3NUDedvmv3cwYdhuPwL -------------------------------------------------------------------------------- /qt6e-24/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | void test() 9 | { 10 | qInfo() << "Test" << QThread::currentThread(); 11 | } 12 | 13 | int testRandom(int max) 14 | { 15 | qInfo() << "Random" << QThread::currentThread(); 16 | QThread::currentThread()->msleep(5000); 17 | return QRandomGenerator::global()->bounded(max); 18 | } 19 | 20 | int main(int argc, char *argv[]) 21 | { 22 | QCoreApplication a(argc, argv); 23 | 24 | QThread::currentThread()->setObjectName("Main Thread"); 25 | qInfo() << "Starting" << QThread::currentThread(); 26 | 27 | QFuture f1 = QtConcurrent::run(test); 28 | f1.waitForFinished(); 29 | 30 | QFuture f2 = QtConcurrent::run(testRandom,100); 31 | 32 | qInfo() << "Random: " << f2.result(); 33 | 34 | qInfo() << "Finished" << QThread::currentThread(); 35 | 36 | return a.exec(); 37 | } 38 | -------------------------------------------------------------------------------- /qt6e-3/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | 8 | void do_cpp() 9 | { 10 | std::string name; 11 | std::cout << "Please enter your name:"; 12 | std::getline(std::cin,name); 13 | std::cout << "Hello " << name << "\n"; 14 | } 15 | 16 | void do_qt() 17 | { 18 | QTextStream qin(stdin); 19 | QTextStream qout(stdout); 20 | 21 | qout << "Please enter your name:"; 22 | qout.flush(); 23 | QString name = qin.readLine(); 24 | qout << "Hello " << name << "\n"; 25 | qout.flush(); 26 | } 27 | 28 | void do_mixed() 29 | { 30 | QTextStream qin(stdin); 31 | qInfo() << "Please enter your name"; 32 | QString name = qin.readLine(); 33 | qInfo() << "Hello" << name; 34 | } 35 | 36 | 37 | int main(int argc, char *argv[]) 38 | { 39 | QCoreApplication a(argc, argv); 40 | 41 | //do_cpp(); 42 | //do_qt(); 43 | do_mixed(); 44 | 45 | return a.exec(); 46 | } 47 | -------------------------------------------------------------------------------- /qt6e-14/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | QTextStream 3 | Viewer request: please show the Qt way of doing user input and output 4 | Facebook group: voidrealms 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | QCoreApplication a(argc, argv); 13 | 14 | //--- QTextStream 15 | QTextStream qin(stdin); 16 | QTextStream qout(stdout); 17 | 18 | //--- endl 19 | qout << "Please enter your name:" << Qt::endl; 20 | 21 | //---Reading 22 | QString name = qin.readLine(); 23 | 24 | //--- Flushing 25 | qout << "You entered: " << name << "\n"; 26 | qout << "Enter your age:\n"; 27 | qout.flush(); 28 | 29 | //---Example 30 | bool ok; 31 | int age = qin.readLine().toInt(&ok); 32 | 33 | if(ok) 34 | { 35 | qout << "You are " << age << " years old"; 36 | } 37 | else 38 | { 39 | qout << "You have failed to enter a valid number!!!!!!!"; 40 | } 41 | qout.flush(); 42 | 43 | return a.exec(); 44 | } 45 | -------------------------------------------------------------------------------- /qt6e-28/dialog.cpp: -------------------------------------------------------------------------------- 1 | #include "dialog.h" 2 | #include "./ui_dialog.h" 3 | 4 | Dialog::Dialog(QWidget *parent) 5 | : QDialog(parent) 6 | , ui(new Ui::Dialog) 7 | { 8 | ui->setupUi(this); 9 | 10 | m_types.insert("Fish",2); 11 | m_types.insert("Cat",7); 12 | m_types.insert("Dog",5); 13 | m_types.insert("Mice",15); 14 | 15 | ui->cmbType->addItems(m_types.keys()); 16 | ui->cmbType->setCurrentIndex(0); 17 | 18 | } 19 | 20 | Dialog::~Dialog() 21 | { 22 | delete ui; 23 | } 24 | 25 | void Dialog::on_cmbType_currentIndexChanged(int index) 26 | { 27 | Q_UNUSED(index); 28 | int m = m_types.value(ui->cmbType->currentText()); 29 | ui->leMultiplier->setText(QString::number(m)); 30 | } 31 | 32 | 33 | void Dialog::on_btnCalulate_clicked() 34 | { 35 | bool ok = false; 36 | int v = ui->leValue->text().toInt(&ok); 37 | if(!ok) ui->leValue->setText("0"); 38 | 39 | int m = ui->leMultiplier->text().toInt(&ok); 40 | if(!ok) ui->leMultiplier->setText("0"); 41 | 42 | int r = v * m; 43 | ui->leResult->setText(QString::number(r)); 44 | } 45 | 46 | -------------------------------------------------------------------------------- /qt6e-26/worker.cpp: -------------------------------------------------------------------------------- 1 | #include "worker.h" 2 | 3 | Worker::Worker(QObject *parent) : QObject(parent) 4 | { 5 | qInfo() << this << Q_FUNC_INFO << QThread::currentThread(); 6 | } 7 | 8 | Worker::~Worker() 9 | { 10 | qInfo() << this << Q_FUNC_INFO << QThread::currentThread(); 11 | } 12 | 13 | void Worker::run() 14 | { 15 | qInfo() << this << Q_FUNC_INFO << QThread::currentThread(); 16 | 17 | qInfo() << this << "Starting..." << QThread::currentThread(); 18 | 19 | QScopedPointer loop(new QEventLoop); 20 | QScopedPointer timer(new QTimer); 21 | 22 | timer->setInterval(5000); 23 | 24 | connect(timer.data(),&QTimer::timeout, this,&Worker::work, Qt::DirectConnection); 25 | connect(this,&Worker::finished, loop.data(),&QEventLoop::quit, Qt::DirectConnection); 26 | 27 | timer->start(); 28 | loop->exec(); //Block 29 | 30 | qInfo() << this << "Finished" << QThread::currentThread(); 31 | } 32 | 33 | void Worker::work() 34 | { 35 | qInfo() << this << Q_FUNC_INFO << QThread::currentThread(); 36 | 37 | //TO DO - do something cool here 38 | 39 | emit finished(); 40 | } 41 | -------------------------------------------------------------------------------- /qt6e-21/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Threaded Exmaple 4 | Create a QThread 5 | Move a class to it 6 | Can not have a parent! 7 | 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include "worker.h" 14 | 15 | int main(int argc, char *argv[]) 16 | { 17 | QCoreApplication a(argc, argv); 18 | 19 | QThread::currentThread()->setObjectName("Main Thread"); 20 | 21 | qInfo() << "Starting" << QThread::currentThread(); 22 | //Worker worker; //no issues 23 | 24 | Worker *worker = new Worker(); //No Parent!!! 25 | 26 | QThread thread; 27 | thread.setObjectName("Worker Thread"); 28 | worker->moveToThread(&thread); 29 | QObject::connect(&thread,&QThread::started,worker,&Worker::run); 30 | 31 | thread.start(); 32 | 33 | qInfo() << "Doing stuff" << QThread::currentThread(); 34 | for(int i = 0; i < 10; i++) 35 | { 36 | qInfo() << "Working: " << QString::number(i) << QThread::currentThread(); 37 | QThread::currentThread()->msleep(500); 38 | } 39 | 40 | qInfo() << "Finished" << QThread::currentThread(); 41 | 42 | return a.exec(); 43 | } 44 | -------------------------------------------------------------------------------- /qt6e-22/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Threaded Exmaple 4 | Communicating between threads 5 | Super simple 6 | 7 | */ 8 | 9 | #include 10 | #include 11 | #include "worker.h" 12 | 13 | int main(int argc, char *argv[]) 14 | { 15 | QCoreApplication a(argc, argv); 16 | 17 | QThread pThread; 18 | QThread cThread; 19 | 20 | Worker producer; 21 | Worker consumer; 22 | 23 | producer.moveToThread(&pThread); 24 | consumer.moveToThread(&cThread); 25 | 26 | producer.setProducer(true); 27 | 28 | pThread.setObjectName("Producer Thread"); 29 | cThread.setObjectName("Consumer Thread"); 30 | producer.setObjectName("Producer"); 31 | consumer.setObjectName("Consumer"); 32 | 33 | QObject::connect(&pThread,&QThread::started, &producer,&Worker::start); 34 | QObject::connect(&cThread,&QThread::started, &consumer,&Worker::start); 35 | 36 | QObject::connect(&producer,&Worker::produced,&consumer,&Worker::consume); 37 | QObject::connect(&producer,&Worker::finished,&consumer,&Worker::quit); 38 | 39 | cThread.start(); 40 | pThread.start(); 41 | 42 | return a.exec(); 43 | } 44 | -------------------------------------------------------------------------------- /qt6e-7/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Qt memory managment 3 | qDeleteall 4 | 5 | Note the STL functions starting with Qt 5 6 | https://doc.qt.io/archives/qt-5.9/qtalgorithms.html#qDeleteAll 7 | */ 8 | 9 | #include 10 | #include "test.h" 11 | 12 | //Get a list of pointers 13 | typedef QList testList; 14 | 15 | testList getList() 16 | { 17 | testList list; 18 | for(int i = 0; i < 5; i++) 19 | { 20 | list.append(new Test()); 21 | list.last()->setObjectName("Test " + QString::number(i)); 22 | } 23 | 24 | return list; 25 | } 26 | 27 | //Display a list of pointers 28 | void display(testList list) 29 | { 30 | foreach(Test* item, list) 31 | { 32 | qInfo() << item; 33 | } 34 | } 35 | 36 | int main(int argc, char *argv[]) 37 | { 38 | QCoreApplication a(argc, argv); 39 | 40 | //In action... 41 | 42 | testList list = getList(); 43 | display(list); 44 | 45 | qInfo() << "Deleting..."; 46 | //qDeleteAll(list); //the entire list 47 | qDeleteAll(list.begin(),list.end()); 48 | list.clear(); 49 | 50 | display(list); 51 | 52 | 53 | return a.exec(); 54 | } 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /qt6e-23/manager.cpp: -------------------------------------------------------------------------------- 1 | #include "manager.h" 2 | 3 | Manager::Manager(QObject *parent) : QObject(parent) 4 | { 5 | qInfo() << "Created" << this << QThread::currentThread(); 6 | } 7 | 8 | void Manager::start() 9 | { 10 | for(int i = 0; i < 1; i++) 11 | { 12 | Worker *worker = new Worker(); 13 | worker->setAutoDelete(true); 14 | 15 | connect(worker,&Worker::started,this,&Manager::started,Qt::QueuedConnection); 16 | connect(worker,&Worker::finished,this,&Manager::finished, Qt::QueuedConnection); 17 | connect(this,&Manager::work,worker,&Worker::work,Qt::QueuedConnection); 18 | 19 | QThreadPool::globalInstance()->start(worker); 20 | } 21 | } 22 | 23 | void Manager::started() 24 | { 25 | Worker* worker = qobject_cast(sender()); 26 | if(!worker) return; 27 | 28 | qInfo() << "Started" << worker << QThread::currentThread(); 29 | 30 | //tell the worker to work 31 | //worker->work(); //BAD 32 | //emit work(); 33 | } 34 | 35 | void Manager::finished() 36 | { 37 | Worker* worker = qobject_cast(sender()); 38 | if(!worker) return; 39 | 40 | qInfo() << "Finished" << worker << QThread::currentThread(); 41 | } 42 | -------------------------------------------------------------------------------- /qt6e-8/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Qt memory managment 3 | QScopedPointer 4 | QSharedPointer 5 | 6 | QT Smart pointers 7 | https://www.qt.io/blog/2009/08/25/count-with-me-how-many-smart-pointer-classes-does-qt-have 8 | 9 | C++ STD Smart Pointers along with boost are there as well, but see the article 10 | 11 | */ 12 | 13 | #include 14 | #include 15 | #include "test.h" 16 | 17 | void testScope() 18 | { 19 | QScopedPointer ptr(new Test()); 20 | ptr->message("Hello"); 21 | } 22 | 23 | void finish(QSharedPointer ptr) 24 | { 25 | ptr->message("Finished"); 26 | } 27 | 28 | void work(QSharedPointer ptr) 29 | { 30 | ptr->message("Working"); 31 | } 32 | 33 | void step(QSharedPointer ptr) 34 | { 35 | ptr->message("Stepping"); 36 | for(int i = 0; i < 3; i++) 37 | { 38 | work(ptr); 39 | } 40 | finish(ptr); 41 | } 42 | 43 | void testShared(Test* test) 44 | { 45 | QSharedPointer ptr(test); 46 | ptr->message("Starting"); 47 | step(ptr); 48 | } 49 | 50 | int main(int argc, char *argv[]) 51 | { 52 | QCoreApplication a(argc, argv); 53 | 54 | testShared(new Test()); 55 | 56 | //testScope(); 57 | 58 | return a.exec(); 59 | } 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /qt6e-22/worker.cpp: -------------------------------------------------------------------------------- 1 | #include "worker.h" 2 | 3 | Worker::Worker(QObject *parent) : QObject(parent) 4 | { 5 | m_producer = false; 6 | m_count = 0; 7 | } 8 | 9 | bool Worker::producer() const 10 | { 11 | return m_producer; 12 | } 13 | 14 | void Worker::setProducer(bool newProducer) 15 | { 16 | m_producer = newProducer; 17 | } 18 | 19 | void Worker::consume(int value) 20 | { 21 | qInfo() << this << "Consuming:" << value << QThread::currentThread(); 22 | } 23 | 24 | void Worker::start() 25 | { 26 | qInfo() << this << "Starting" << QThread::currentThread(); 27 | m_timer = new QTimer(this); 28 | connect(m_timer, &QTimer::timeout,this,&Worker::timeout); 29 | 30 | if(m_producer) 31 | { 32 | qInfo() << this << "Starting timer" << QThread::currentThread(); 33 | m_timer->setInterval(1000); 34 | m_timer->start(); 35 | } 36 | } 37 | 38 | void Worker::quit() 39 | { 40 | qInfo() << this << "Quitting" << QThread::currentThread(); 41 | m_timer->stop(); 42 | emit finished(); 43 | } 44 | 45 | void Worker::timeout() 46 | { 47 | int value = QRandomGenerator::global()->bounded(1000); 48 | qInfo() << this << "Producing:" << value << QThread::currentThread(); 49 | emit produced(value); 50 | m_count++; 51 | if(m_count == 5) quit(); 52 | } 53 | -------------------------------------------------------------------------------- /qt6e-17/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | What 3 | Getting information about the storage devices 4 | 5 | Why 6 | Its the foundation of the file system 7 | 8 | Example 9 | QStorageInfo 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | //--- Storage Details 17 | void display(QStorageInfo &storage, QString title) 18 | { 19 | qInfo() << "---" << title << "---------"; 20 | qInfo() << "Name:" << storage.name(); 21 | qInfo() << "Display:" << storage.displayName(); 22 | qInfo() << "Root:" << storage.isRoot(); 23 | qInfo() << "Path" << storage.rootPath(); 24 | qInfo() << "Device" << storage.device(); 25 | qInfo() << "File System:" << storage.fileSystemType(); 26 | qInfo() << "ReadOnly:" << storage.isReadOnly(); 27 | qInfo() << "Ready:" << storage.isReady(); 28 | qInfo() << "Size:" << storage.bytesTotal()/1000/1000 << "MB"; 29 | qInfo() << "Available:" << storage.bytesAvailable()/1000/1000 << "MB"; 30 | qInfo() << "Free:" << storage.bytesFree()/1000/1000 << "MB"; 31 | qInfo() << "------------"; 32 | } 33 | 34 | int main(int argc, char *argv[]) 35 | { 36 | QCoreApplication a(argc, argv); 37 | 38 | //--- Getting the root ( / or C:\) 39 | QStorageInfo root = QStorageInfo::root(); 40 | display(root,"Root"); 41 | 42 | //--- Mounted Devices 43 | QList devices = QStorageInfo::mountedVolumes(); 44 | foreach(QStorageInfo device, devices) 45 | { 46 | display(device,device.name()); 47 | } 48 | 49 | return a.exec(); 50 | } 51 | -------------------------------------------------------------------------------- /qt6e-16/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Determining the OS 4 | 5 | At Compile Time 6 | Q_OS_WIN 7 | Q_OS_MACOS 8 | Q_OS_LINUX 9 | https://doc.qt.io/qt-6/qtglobal.html 10 | 11 | At Run Time 12 | QSystemInfo 13 | https://doc.qt.io/qt-6/qsysinfo.html 14 | 15 | 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | void compileTime() 23 | { 24 | #ifdef Q_OS_MACOS 25 | qDebug() << "This is Mac"; 26 | #endif 27 | 28 | #ifdef Q_OS_LINUX 29 | qDebug() << "This is Linux"; 30 | #endif 31 | 32 | #ifdef Q_OS_WIN 33 | qDebug() << "This is Windows"; 34 | 35 | #ifdef #Q_OS_WIN32 36 | qDebug() << "This is Windows 32 bit"; 37 | #endif 38 | 39 | #ifdef #Q_OS_WIN64 40 | qDebug() << "This is Windows 64 bit"; 41 | #endif 42 | #endif 43 | } 44 | 45 | void runTime() 46 | { 47 | QSysInfo info; 48 | qInfo() << "prettyProductName:" << info.prettyProductName(); 49 | qInfo() << "productType:" << info.productType(); 50 | qInfo() << "productVersion:" << info.productVersion(); 51 | qInfo() << "machineHostName:" << info.machineHostName(); 52 | qInfo() << "machineUniqueId:" << info.machineUniqueId(); 53 | qInfo() << "currentCpuArchitecture:" << info.currentCpuArchitecture(); 54 | qInfo() << "buildCpuArchitecture:" << info.buildCpuArchitecture(); 55 | qInfo() << "buildAbi:" << info.buildAbi(); 56 | 57 | } 58 | 59 | int main(int argc, char *argv[]) 60 | { 61 | QCoreApplication a(argc, argv); 62 | 63 | //--- At Compile time 64 | compileTime(); 65 | 66 | 67 | //--- At Run time 68 | runTime(); 69 | 70 | return a.exec(); 71 | } 72 | -------------------------------------------------------------------------------- /qt6e-10/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | QStringView 3 | https://doc.qt.io/qt-5/qstringview.html#details 4 | 5 | A QStringView references a contiguous portion of a UTF-16 string it does not own. 6 | It acts as an interface type to all kinds of UTF-16 string, without the 7 | need to construct a QString first. 8 | 9 | Why: Its faster than copying a QString, and its read only! 10 | */ 11 | 12 | 13 | #include 14 | 15 | void readname(QString &name) //address of is access of 16 | { 17 | name.insert(0,"Mr."); 18 | qInfo() << "Changed" << name; 19 | } 20 | 21 | void readonly(QStringView name) 22 | { 23 | qInfo() << "name" << name.data() << name; 24 | } 25 | 26 | int main(int argc, char *argv[]) 27 | { 28 | QCoreApplication a(argc, argv); 29 | 30 | QString firstname = "Bryan"; 31 | QString lastname = "Cairns"; 32 | 33 | //--- Copy and modification issues 34 | //readname(firstname); 35 | qInfo() << "Original" << firstname; 36 | 37 | //---Read only, no copy 38 | qInfo() << "name" << firstname.data() << firstname; 39 | readonly(firstname); 40 | 41 | //--- Basic parsing 42 | 43 | QString fullname = firstname + " " + lastname; 44 | 45 | foreach(QStringView part, QStringView(fullname).split(QChar(' '))) 46 | { 47 | qInfo() << "part" << part; 48 | //Data types can get a bit annoying 49 | if(part.startsWith(QStringView(firstname),Qt::CaseInsensitive)) 50 | { 51 | qInfo() << "~First name detected~"; 52 | readonly(QStringView(firstname).mid(1,3)); 53 | } 54 | } 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | return a.exec(); 64 | } 65 | -------------------------------------------------------------------------------- /qt6e-28/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | 3 | project(qt6e-28 VERSION 0.1 LANGUAGES CXX) 4 | 5 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 6 | 7 | set(CMAKE_AUTOUIC ON) 8 | set(CMAKE_AUTOMOC ON) 9 | set(CMAKE_AUTORCC ON) 10 | 11 | set(CMAKE_CXX_STANDARD 11) 12 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 13 | 14 | find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED) 15 | find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED) 16 | 17 | set(PROJECT_SOURCES 18 | main.cpp 19 | dialog.cpp 20 | dialog.h 21 | dialog.ui 22 | ) 23 | 24 | if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) 25 | qt_add_executable(qt6e-28 26 | MANUAL_FINALIZATION 27 | ${PROJECT_SOURCES} 28 | ) 29 | # Define target properties for Android with Qt 6 as: 30 | # set_property(TARGET qt6e-28 APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR 31 | # ${CMAKE_CURRENT_SOURCE_DIR}/android) 32 | # For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation 33 | else() 34 | if(ANDROID) 35 | add_library(qt6e-28 SHARED 36 | ${PROJECT_SOURCES} 37 | ) 38 | # Define properties for Android with Qt 5 after find_package() calls as: 39 | # set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android") 40 | else() 41 | add_executable(qt6e-28 42 | ${PROJECT_SOURCES} 43 | ) 44 | endif() 45 | endif() 46 | 47 | target_link_libraries(qt6e-28 PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) 48 | 49 | set_target_properties(qt6e-28 PROPERTIES 50 | MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com 51 | MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} 52 | MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} 53 | ) 54 | 55 | if(QT_VERSION_MAJOR EQUAL 6) 56 | qt_finalize_executable(qt6e-28) 57 | endif() 58 | -------------------------------------------------------------------------------- /qt6e-26/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Testing different ways of threading 4 | Pros and cons of each 5 | GUI Exmaple 6 | 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "worker.h" 16 | 17 | void testMain() 18 | { 19 | qInfo() << "Testing on main"; 20 | 21 | Worker* worker = new Worker(); 22 | worker->run(); 23 | worker->deleteLater(); 24 | qInfo() << "Testing..."; 25 | 26 | } 27 | 28 | void testThread() 29 | { 30 | QThread* thread = new QThread(); 31 | thread->setObjectName("Worker Thread"); 32 | qInfo() << "Testing on Worker Thread"; 33 | Worker* worker = new Worker(); 34 | 35 | worker->moveToThread(thread); 36 | 37 | QObject::connect(thread,&QThread::started,worker,&Worker::run); 38 | QObject::connect(worker,&Worker::finished,worker,&Worker::deleteLater); 39 | QObject::connect(worker,&Worker::finished,thread,&QThread::quit); 40 | QObject::connect(thread,&QThread::finished,thread,&QThread::deleteLater); 41 | 42 | thread->start(); 43 | 44 | qInfo() << "Testing..."; 45 | } 46 | 47 | void testPool() 48 | { 49 | qInfo() << "Testing on Thread Pool"; 50 | Worker* worker = new Worker(); 51 | worker->setAutoDelete(true); 52 | QThreadPool::globalInstance()->start(worker); 53 | 54 | qInfo() << "Testing..."; 55 | } 56 | 57 | void testConcurrent() 58 | { 59 | qInfo() << "Testing on Concurrent"; 60 | QScopedPointer worker(new Worker); 61 | worker->run(); 62 | qInfo() << "Testing..."; 63 | } 64 | 65 | int main(int argc, char *argv[]) 66 | { 67 | QCoreApplication a(argc, argv); 68 | 69 | QThread::currentThread()->setObjectName("Main Thread"); 70 | 71 | //testMain(); 72 | //testThread(); 73 | //testPool(); 74 | QtConcurrent::run(&testConcurrent); 75 | 76 | 77 | qInfo() << "Finished in" << QThread::currentThread(); 78 | 79 | return a.exec(); 80 | } 81 | -------------------------------------------------------------------------------- /qt6e-20/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Simple inventory application 4 | Using the skills the have covered so far 5 | 6 | Make a class 7 | Fill in the code 8 | Monitor input 9 | Focous on QDataStream 10 | 11 | */ 12 | 13 | #include 14 | #include 15 | #include "inventory.h" 16 | 17 | 18 | int main(int argc, char *argv[]) 19 | { 20 | QCoreApplication a(argc, argv); 21 | 22 | Inventory inventory; 23 | 24 | qInfo() << "Available commands:"; 25 | qInfo() << "add "; 26 | qInfo() << "remove "; 27 | qInfo() << "save"; 28 | qInfo() << "load"; 29 | qInfo() << "list"; 30 | qInfo() << "quit"; 31 | 32 | QTextStream stream(stdin); 33 | 34 | while(true) 35 | { 36 | qInfo() << "Enter a command:"; 37 | QString line = stream.readLine(); 38 | QStringList list = line.split(" "); 39 | if(list.size() <= 0) break; 40 | 41 | QString command = list.at(0).toUpper(); 42 | if(command == "QUIT") 43 | { 44 | //a.exit(0); 45 | break; 46 | } 47 | 48 | if(command == "LIST") inventory.list(); 49 | if(command == "LOAD") inventory.load(); 50 | if(command == "SAVE") inventory.save(); 51 | 52 | if(command == "ADD" || command == "REMOVE") 53 | { 54 | if(list.size() < 3) 55 | { 56 | qWarning() << "Not enough info"; 57 | continue; 58 | } 59 | 60 | QString name = list.at(1); 61 | bool ok; 62 | int qty; 63 | qty = list.at(2).toInt(&ok); 64 | if(!ok) 65 | { 66 | qWarning() << "Invalid quantity!"; 67 | continue; 68 | } 69 | 70 | if(command == "ADD") inventory.add(name,qty); 71 | if(command == "REMOVE") inventory.remove(name,qty); 72 | 73 | } 74 | 75 | } 76 | 77 | inventory.save(); 78 | qInfo() << "Complete"; 79 | 80 | //return a.exec(); 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /qt6e-19/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | What 4 | Reading and writing text files 5 | 6 | Why 7 | We want to make plain text files 8 | 9 | How 10 | QFile and QTextStream 11 | 12 | */ 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | void basics() 19 | { 20 | QFile file("test.txt"); 21 | if(!file.open(QIODevice::ReadWrite)) 22 | { 23 | qCritical() << "Could not open file!"; 24 | qCritical() << file.errorString(); 25 | return; 26 | } 27 | 28 | qInfo() << "Writing file..."; 29 | file.write(QByteArray("Hello World")); 30 | file.flush(); // pushes the data to the disk 31 | 32 | qInfo() << "Reading file..."; 33 | file.seek(0); 34 | qInfo() << file.readAll(); 35 | 36 | file.close(); 37 | } 38 | 39 | bool writeFile(QString filename) 40 | { 41 | QFile file(filename); 42 | if(!file.open(QIODevice::WriteOnly)) 43 | { 44 | qCritical() << file.errorString(); 45 | return false; 46 | } 47 | 48 | QTextStream stream(&file); 49 | for(int i = 0; i < 5; i++) 50 | { 51 | stream << QString::number(i) << " Hello World\r\n"; 52 | } 53 | 54 | file.close(); 55 | return true; 56 | } 57 | 58 | void readFile(QString filename) 59 | { 60 | QFile file(filename); 61 | if(!file.exists()) 62 | { 63 | qCritical() << "File not found"; 64 | return; 65 | } 66 | 67 | if(!file.open(QIODevice::ReadOnly)) 68 | { 69 | qCritical() << file.errorString(); 70 | return; 71 | } 72 | 73 | QTextStream stream(&file); 74 | //QString data = stream.readAll(); 75 | 76 | while (!stream.atEnd()) { 77 | QString line = stream.readLine(); 78 | qInfo() << line; 79 | } 80 | 81 | file.close(); 82 | } 83 | 84 | int main(int argc, char *argv[]) 85 | { 86 | QCoreApplication a(argc, argv); 87 | 88 | //--- Basics 89 | //basics(); 90 | 91 | //--- Writing 92 | writeFile("test.txt"); 93 | 94 | //--- Reading 95 | readFile("testssdsad.txt"); 96 | 97 | return a.exec(); 98 | } 99 | -------------------------------------------------------------------------------- /qt6e-9/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | QString - Qts String class 3 | Also touch on QChar 4 | 5 | https://doc.qt.io/qt-5/qstring.html 6 | 7 | Escape sequences 8 | https://en.cppreference.com/w/cpp/language/escape 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | void test(QString data) 15 | { 16 | qInfo() << data; 17 | } 18 | 19 | int main(int argc, char *argv[]) 20 | { 21 | QCoreApplication a(argc, argv); 22 | 23 | //--- Creating --- 24 | QString line("Hello World"); 25 | qInfo() << line; 26 | 27 | QString name; 28 | name = "Bryan Cairns"; 29 | qInfo() << name; 30 | 31 | int pos = 504; 32 | int max = 7439; 33 | QString status = QString("Processing file %1 of %2").arg(pos).arg(max); 34 | qInfo() << status; 35 | 36 | 37 | //--- Reading each char --- 38 | for(int i = 0; i < line.length(); i++) 39 | { 40 | QChar c = line.at(i); 41 | qInfo() << c; 42 | } 43 | 44 | //--- Comparing and searching --- 45 | qInfo() << "Compare" << line.compare("hello world",Qt::CaseSensitivity::CaseSensitive); 46 | qInfo() << "Starts" << line.startsWith("hello",Qt::CaseInsensitive); 47 | qInfo() << "Ends" << line.endsWith("world",Qt::CaseInsensitive); 48 | qInfo() << "Contains" << line.contains("world"); 49 | qInfo() << "Index" << line.indexOf("World"); 50 | 51 | //--- Modifying and parsing --- 52 | line.append("\r\nHow are you?"); 53 | qInfo() << "Escape" << line; 54 | 55 | line.append("\r\nThisis HTML
"); 56 | qInfo() << "Html" << line.toHtmlEscaped(); 57 | 58 | line.replace("?","!"); 59 | qInfo() << "Replaced" << line; 60 | 61 | qInfo() << "Upper" << line.toUpper(); 62 | qInfo() << "Lower" << line.toLower(); 63 | qInfo() << "Mid" << line.mid(3,5); 64 | 65 | QStringList list = line.split("\n"); 66 | foreach(QString item, list) 67 | { 68 | qInfo() << "item" << item.trimmed(); 69 | } 70 | 71 | //--- Conversion --- 72 | std::cout << "std " << line.toStdString() << std::endl; 73 | 74 | qInfo() << "UTF8" << line.toUtf8(); 75 | qInfo() << "Base64" << line.toUtf8().toBase64(); 76 | qInfo() << "Hex" << line.toUtf8().toHex(); 77 | 78 | 79 | //--- Not a QObject --- 80 | test(line); 81 | 82 | 83 | 84 | 85 | 86 | return a.exec(); 87 | } 88 | -------------------------------------------------------------------------------- /qt6e-28/dialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Dialog 4 | 5 | 6 | 7 | 0 8 | 0 9 | 303 10 | 192 11 | 12 | 13 | 14 | Dialog 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | Type 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | Value 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | Multiplier 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | Result 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | Qt::Horizontal 67 | 68 | 69 | 70 | 40 71 | 20 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | Calulate 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /qt6e-18/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | What 4 | Directories 5 | 6 | Why 7 | We need to use the file system 8 | 9 | How 10 | QDir 11 | 12 | */ 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | //*** Current folder 20 | void test_current() 21 | { 22 | qInfo() << QDir::currentPath(); 23 | 24 | QDir dir; 25 | qInfo() << dir.absolutePath(); 26 | 27 | } 28 | 29 | //*** Listing 30 | void test_list(QString path, bool recursive = false) 31 | { 32 | 33 | QDir root(path); 34 | if(!root.exists()) 35 | { 36 | qWarning() << "Path not found" << path; 37 | return; 38 | } 39 | 40 | QFileInfoList list = root.entryInfoList(QDir::Filter::NoDotAndDotDot | QDir::Filter::AllEntries); 41 | 42 | foreach(QFileInfo fi, list) 43 | { 44 | qInfo() << "--------------------"; 45 | qInfo() << "Name" << fi.fileName(); 46 | qInfo() << "Path" << fi.filePath(); 47 | qInfo() << "Absolute" << fi.absoluteFilePath(); 48 | qInfo() << "Created" << fi.birthTime().toString(); 49 | qInfo() << "Modified" << fi.lastModified().toString(); 50 | qInfo() << "Size" << fi.size(); 51 | 52 | QString type = "Uknown"; 53 | fi.isDir() ? type = "Dir" : type = "File"; 54 | qInfo() << "Type" << type; 55 | 56 | if(recursive && fi.isDir()) test_list(fi.absoluteFilePath()); 57 | } 58 | 59 | 60 | 61 | } 62 | 63 | //*** Modifying 64 | void test_modify(QDir root) 65 | { 66 | if(root.exists("test")) 67 | { 68 | qInfo() << "Test folder already exists"; 69 | return; 70 | } 71 | 72 | if(root.mkdir("test")) 73 | { 74 | QDir dir("test"); 75 | qInfo() << "Created" << dir.absolutePath(); 76 | 77 | if(root.rename("test","test2")) 78 | { 79 | qInfo() << "Renamed" << dir.absolutePath(); //notice this points to a dir that no longer exists! 80 | qInfo() << "Test" << dir.exists(); 81 | 82 | if(root.exists("test2")) 83 | { 84 | if(!root.rmdir("test2")) 85 | { 86 | qInfo() << "Failed - could not remove the directory!"; 87 | } 88 | } 89 | } 90 | } 91 | } 92 | 93 | int main(int argc, char *argv[]) 94 | { 95 | QCoreApplication a(argc, argv); 96 | //test_current(); 97 | //test_list(QDir::currentPath(), true); 98 | test_modify(QDir::current()); 99 | 100 | return a.exec(); 101 | } 102 | -------------------------------------------------------------------------------- /qt6e-20/inventory.cpp: -------------------------------------------------------------------------------- 1 | #include "inventory.h" 2 | 3 | Inventory::Inventory(QObject *parent) : QObject(parent) 4 | { 5 | load(); 6 | } 7 | 8 | void Inventory::add(QString name, int qty) 9 | { 10 | if(m_items.contains(name)) 11 | { 12 | m_items[name] += qty; 13 | } 14 | else 15 | { 16 | m_items.insert(name,qty); 17 | } 18 | } 19 | 20 | void Inventory::remove(QString name, int qty) 21 | { 22 | if(m_items.contains(name)) 23 | { 24 | m_items[name] -= qty; 25 | if(m_items.value(name) < 0) m_items.remove(name); 26 | qInfo() << "Item removed"; 27 | } 28 | else 29 | { 30 | qWarning() << "No item of that name!";; 31 | } 32 | } 33 | 34 | void Inventory::list() 35 | { 36 | qInfo() << "Items:" << m_items.size(); 37 | foreach(QString key, m_items.keys()) 38 | { 39 | qInfo() << key << " = " << m_items.value(key); 40 | } 41 | } 42 | 43 | void Inventory::save() 44 | { 45 | QFile file("inventory.txt"); 46 | if(!file.open(QIODevice::WriteOnly)) 47 | { 48 | qCritical() << "Could not save file:" << file.errorString(); 49 | return; 50 | } 51 | 52 | QDataStream stream(&file); 53 | stream.setVersion(QDataStream::Qt_6_0); 54 | 55 | int len = m_items.size(); 56 | stream << len; 57 | 58 | qInfo() << "Number of items to save:" << len; 59 | 60 | foreach(QString key, m_items.keys()) 61 | { 62 | qInfo() << "Saving:" << key; 63 | stream << key; 64 | stream << m_items.value(key); 65 | } 66 | 67 | file.close(); 68 | 69 | qInfo() << "File saved"; 70 | 71 | } 72 | 73 | void Inventory::load() 74 | { 75 | QFile file("inventory.txt"); 76 | 77 | if(!file.exists()) 78 | { 79 | qWarning() << "File does not exist!"; 80 | return; 81 | } 82 | 83 | if(!file.open(QIODevice::ReadOnly)) 84 | { 85 | qCritical() << "Could not open file:" << file.errorString(); 86 | return; 87 | } 88 | 89 | QDataStream stream(&file); 90 | if(stream.version() != QDataStream::Qt_6_0) 91 | { 92 | qCritical() << "Wrong data stream version!"; 93 | file.close(); 94 | return; 95 | } 96 | 97 | m_items.clear(); 98 | int max; 99 | stream >> max; 100 | 101 | qInfo() << "Number of items to load:" << max; 102 | 103 | for(int i = 0; i < max; i ++) 104 | { 105 | QString key; 106 | int qty; 107 | stream >> key; 108 | stream >> qty; 109 | m_items.insert(key,qty); 110 | } 111 | 112 | file.close(); 113 | 114 | qInfo() << "File loaded"; 115 | } 116 | -------------------------------------------------------------------------------- /qt6e-11/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | QByteArray 4 | Best array ever 5 | 6 | */ 7 | 8 | #include 9 | 10 | void stats(QByteArray &data) 11 | { 12 | qInfo() << "Length" << data.length() << "Capacity" << data.capacity(); 13 | qInfo() << data; 14 | } 15 | 16 | int main(int argc, char *argv[]) 17 | { 18 | QCoreApplication a(argc, argv); 19 | 20 | //--- Creating an array --- 21 | QByteArray stuff; 22 | qInfo() << stuff; 23 | 24 | QByteArray data("Hello"); 25 | qInfo() << data; 26 | 27 | QByteArray buffer(10,'\t'); 28 | qInfo() << buffer; 29 | 30 | QByteArray person(QString("Bryan").toLocal8Bit()); 31 | qInfo() << person; 32 | 33 | //--- Sizing the array --- 34 | data.reserve(25); 35 | stats(data); 36 | 37 | data.resize(10); 38 | stats(data); 39 | 40 | data.truncate(8); 41 | stats(data); 42 | 43 | data.clear(); 44 | stats(data); 45 | 46 | //--- Modifying the data --- 47 | data.resize(5); 48 | data.fill('\x02'); 49 | stats(data); 50 | 51 | data.replace(0,99,QByteArray("Sweet")); 52 | stats(data); 53 | 54 | data.fill('*'); 55 | data.insert(3,QByteArray("Hello World")); 56 | stats(data); 57 | 58 | data.append('!'); 59 | stats(data); 60 | 61 | data.remove(0,3); 62 | stats(data); 63 | 64 | //--- Reading the data --- 65 | 66 | int first = data.indexOf('*'); 67 | int last = data.lastIndexOf('*'); 68 | qInfo() << "Start" << first << "End" << last; 69 | 70 | if(first > -1 && last > -1) qInfo() << data.mid(first,(last - first) + 1); 71 | 72 | data.clear(); 73 | data.append("Bryan Cairns"); 74 | 75 | for(int i = 0; i < data.length(); i++) 76 | { 77 | qInfo() << "At" << data.at(i) << "or" << data[i]; 78 | } 79 | 80 | foreach(char c, data) 81 | { 82 | qInfo() << "Char" << c; 83 | } 84 | 85 | foreach(auto item, data.split(' ')) 86 | { 87 | qInfo() << "Item" << item; 88 | } 89 | 90 | //--- Encoding the data --- 91 | qInfo() << "Normal" << data; 92 | qInfo() << "Repeat" << data.repeated(3); 93 | 94 | data.append(QByteArray("\t\r\n")); 95 | data.insert(0,QByteArray(" \t\t\t")); 96 | qInfo() << "Trimmed" << data.trimmed(); 97 | qInfo() << "Actual" << data; 98 | data = data.trimmed(); 99 | qInfo() << "Actual" << data; 100 | 101 | QByteArray hex = data.toHex(); 102 | qInfo() << "Hex" << hex; 103 | QByteArray from_hex = QByteArray::fromHex(hex); 104 | qInfo() << "From Hex" << from_hex; 105 | 106 | QByteArray base64 = data.toBase64(); 107 | qInfo() << "base64" << base64; 108 | QByteArray from_base64 = QByteArray::fromBase64(base64); 109 | qInfo() << "From base64" << from_base64; 110 | 111 | 112 | 113 | 114 | 115 | return a.exec(); 116 | } 117 | -------------------------------------------------------------------------------- /qt6e-15/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Intercepting QDebug 3 | 4 | https://doc-snapshots.qt.io/qt6-dev/qtglobal.html 5 | 6 | Message types 7 | https://doc-snapshots.qt.io/qt6-dev/qtglobal.html#QtMsgType-enum 8 | 9 | QtMessageHandler is a typedef 10 | https://doc-snapshots.qt.io/qt6-dev/qtglobal.html#QtMessageHandler-typedef 11 | 12 | qInstallMessageHandler installs the handler 13 | https://doc-snapshots.qt.io/qt6-dev/qtglobal.html#qInstallMessageHandler 14 | 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include "kitten.h" 22 | 23 | using namespace std; 24 | 25 | void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) 26 | { 27 | //Because we can intercept this from anywhere in the app, we can log to a file, or the system log 28 | 29 | QByteArray localMsg = msg.toLocal8Bit(); 30 | const char *file = context.file ? context.file : ""; 31 | const char *function = context.function ? context.function : ""; 32 | switch (type) { 33 | case QtDebugMsg: 34 | fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function); 35 | break; 36 | case QtInfoMsg: 37 | fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function); 38 | break; 39 | case QtWarningMsg: 40 | fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function); 41 | break; 42 | case QtCriticalMsg: 43 | fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function); 44 | break; 45 | case QtFatalMsg: 46 | fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function); 47 | break; 48 | } 49 | } 50 | 51 | void test() 52 | { 53 | qDebug() << "test"; 54 | qInfo() << "test"; 55 | qWarning() << "test"; 56 | qCritical() << "test"; 57 | qFatal("fatal message"); 58 | 59 | Kitten kitty; 60 | kitty.meow(); 61 | } 62 | 63 | 64 | int main(int argc, char *argv[]) 65 | { 66 | QCoreApplication a(argc, argv); 67 | 68 | //--- Setup (and copy the code) 69 | QTextStream qin(stdin); 70 | cout << "Starting" << endl; 71 | 72 | bool running = true; 73 | do 74 | { 75 | cout << "Enter a command (start, stop, test, or exit)" << endl; 76 | QString command = qin.readLine(); 77 | qInfo() << "You entered:" << command; 78 | 79 | //--- Install 80 | if(command.toUpper() == "START") qInstallMessageHandler(myMessageOutput); 81 | 82 | //--- Uninstall 83 | if(command.toUpper() == "STOP") qInstallMessageHandler(0); 84 | 85 | //--- Test 86 | if(command.toUpper() == "TEST") test(); 87 | 88 | //--- Exit the loop 89 | if(command.toUpper() == "EXIT") running = false; 90 | 91 | }while (running); 92 | 93 | 94 | cout << "Finished" << endl; 95 | 96 | return a.exec(); 97 | } 98 | -------------------------------------------------------------------------------- /qt6e-13/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | QMap 4 | key and value pairs 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "cat.h" 12 | 13 | //--- Creating a QMap 14 | typedef QMap> catMap; 15 | 16 | catMap getCats() 17 | { 18 | catMap cats; 19 | 20 | for(int i = 0; i < 5; i++) 21 | { 22 | QSharedPointer ptr(new Cat()); 23 | ptr->setAge(QRandomGenerator::global()->bounded(1,5)); 24 | ptr->setName("Unknown"); 25 | cats.insert("pet" + QString::number(i), ptr); 26 | 27 | } 28 | 29 | return cats; 30 | } 31 | 32 | //--- Displaying a QMap 33 | void display(catMap &cats) 34 | { 35 | foreach(QString key, cats.keys()) 36 | { 37 | QSharedPointer ptr = cats.value(key); 38 | qInfo() << key << ptr->name() << ptr->age() << ptr.data(); 39 | } 40 | } 41 | 42 | //--- Modifying an item 43 | void modifyCat(catMap &cats,QString key) 44 | { 45 | if(cats.contains(key)) 46 | { 47 | qInfo() << "Modifying:" << key; 48 | cats[key]->setAge(99); 49 | cats[key]->setName("Fluffy"); 50 | display(cats); 51 | } 52 | } 53 | 54 | //--- Adding and inserting 55 | void addCat(catMap &cats) 56 | { 57 | qInfo() << "Adding and inseting"; 58 | QSharedPointer ptr(new Cat()); 59 | ptr->setAge(1000); 60 | ptr->setName("Test Cat"); 61 | cats.insert("test",ptr); 62 | //cats["test"] = ptr; 63 | display(cats); 64 | } 65 | 66 | //--- Searching for an item 67 | void findCat(catMap &cats,QString key) 68 | { 69 | qInfo() << "Exists:" << cats.contains(key); 70 | 71 | auto iter = cats.find(key); 72 | if(iter != cats.end()) qInfo() << iter.key() << iter.value(); 73 | 74 | foreach(QSharedPointer value, cats.values()) 75 | { 76 | qInfo() << value; 77 | } 78 | 79 | foreach(QString key, cats.keys()) 80 | { 81 | qInfo() << "Key:" << key << "Value:" << cats.value(key); 82 | } 83 | } 84 | 85 | //--- Removing an item 86 | void removeCat(catMap &cats,QString key) 87 | { 88 | if(!cats.contains(key)) 89 | { 90 | qWarning() << "Cat not found"; 91 | return; 92 | } 93 | 94 | cats.remove(key); 95 | display(cats); 96 | } 97 | 98 | int main(int argc, char *argv[]) 99 | { 100 | QCoreApplication a(argc, argv); 101 | 102 | //--- Intro 103 | QMap lucky; 104 | lucky.insert(QString("Bryan"),22.5); 105 | qInfo() << lucky; 106 | qInfo() << "My Lucky Number:" << lucky["Bryan"]; 107 | 108 | //--- Creating a QMap 109 | catMap cats = getCats(); 110 | //qInfo() << cats; 111 | 112 | //--- Displaying a QMap 113 | display(cats); 114 | 115 | //--- Modifying an item 116 | modifyCat(cats,"pet2"); 117 | 118 | //--- Adding and inserting 119 | addCat(cats); 120 | //addCat(cats); 121 | 122 | //--- Searching for an item 123 | findCat(cats,"test"); 124 | 125 | //--- Removing an item 126 | removeCat(cats,"test"); 127 | 128 | //--- Cleanup 129 | qInfo() << "Clearing"; 130 | cats.clear(); 131 | 132 | return a.exec(); 133 | } 134 | -------------------------------------------------------------------------------- /qt6e-12/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | QList 3 | https://doc.qt.io/qt-6.0/qlistwidget.html 4 | https://www.qt.io/blog/qlist-changes-in-qt-6 5 | 6 | QList is a templated container 7 | QList is not a QObject 8 | QList and QVector are the same thing 9 | No more 2 gb limit 10 | QList will auto resize itself 11 | QObjects and pointers need to be cleaned up 12 | */ 13 | 14 | #include 15 | #include 16 | #include 17 | #include "test.h" 18 | 19 | //--- Creating and adding data 20 | QList getNumbers() 21 | { 22 | QList list; 23 | 24 | //use the operator 25 | list << 300 << 400 << 500; 26 | 27 | int max = QRandomGenerator::global()->bounded(1,10); 28 | for(int i = 0; i < max; i++) 29 | { 30 | list.append(QRandomGenerator::global()->bounded(1,100)); 31 | } 32 | 33 | return list; 34 | } 35 | void test_numbers() 36 | { 37 | QList list = getNumbers(); 38 | qInfo() << list; 39 | 40 | qInfo() << "Length" << list.length(); //Same 41 | qInfo() << "Count" << list.count(); //Same 42 | qInfo() << "Size" << list.size(); //Same 43 | 44 | for(int i = 0; i < list.length(); i++) 45 | { 46 | qInfo() << i << "=" << list.at(i) << "or" << list[i]; 47 | } 48 | 49 | qInfo() << "----------"; 50 | foreach(int i, list) 51 | { 52 | qInfo() << i; 53 | } 54 | } 55 | 56 | //--- Modifying exiting data 57 | void test_modifying() 58 | { 59 | QList list = getNumbers(); 60 | 61 | 62 | 63 | //direct mod 64 | list[0] = 1000; 65 | //list.at(0) = 3000; 66 | 67 | //appending 68 | list.append(99); 69 | list << 2000 << 3000; 70 | 71 | //inserting 72 | list.insert(0,55); 73 | list.insert(1,22); 74 | list.insert(1,255); 75 | list.insert(1,255); 76 | list.insert(1,255); 77 | list.insert(1,255); 78 | list.insert(1,255); 79 | list.insert(1,255); 80 | list.insert(1,255); 81 | 82 | 83 | 84 | //Removing 85 | qInfo() << "Length" << list.length(); 86 | list.removeAt(0); 87 | qInfo() << "RemoveAt" << list.length(); 88 | list.removeOne(255); 89 | qInfo() << "RemoveOne" << list.length(); 90 | list.removeAll(255); 91 | qInfo() << "RemoveAll" << list.length(); 92 | list.remove(5,7); 93 | qInfo() << "Remove" << list.length(); 94 | 95 | for(int i = 0; i < list.length(); i++) 96 | { 97 | qInfo() << i << "=" << list.at(i); 98 | } 99 | 100 | } 101 | 102 | //--- Searching --- 103 | void test_search() 104 | { 105 | QList list = getNumbers(); 106 | int item = 999; 107 | 108 | list.insert(0,item); 109 | list.insert(3,item); 110 | list.append(item); 111 | 112 | for(int i = 0; i < list.length(); i++) 113 | { 114 | qInfo() << i << "=" << list.at(i); 115 | } 116 | 117 | //First 118 | int fpos = list.indexOf(item); 119 | qInfo() << "First" << fpos; 120 | 121 | //Last 122 | int lpos = list.lastIndexOf(item); 123 | qInfo() << "Last" << lpos; 124 | 125 | 126 | //All 127 | int pos = list.indexOf(item); 128 | do 129 | { 130 | qInfo() << "At" << pos; 131 | pos = list.indexOf(item,pos + 1); 132 | } while(pos > -1); 133 | 134 | qInfo() << "Contains" << list.contains(item); 135 | 136 | //Get a slice 137 | QList items = list.sliced(1,4); 138 | qInfo() << list; 139 | qInfo() << items; 140 | } 141 | 142 | //--- Memory considerations --- 143 | void test_deleteall() 144 | { 145 | QList list; 146 | for(int i = 0; i < 5; i++) 147 | { 148 | list.append(new Test()); //Danger no parent 149 | } 150 | 151 | qInfo() << "Cleanup"; 152 | qDeleteAll(list); 153 | list.clear(); 154 | qInfo() << list.length(); 155 | } 156 | 157 | void test_smart() 158 | { 159 | QList> list; 160 | for(int i = 0; i < 5; i++) 161 | { 162 | QSharedPointer item(new Test); //Auto memory management 163 | list.append(item); 164 | } 165 | 166 | qInfo() << "Remove first"; 167 | list.removeFirst(); 168 | 169 | qInfo() << "Clear"; 170 | list.clear(); 171 | qInfo() << list.length(); 172 | 173 | 174 | } 175 | 176 | int main(int argc, char *argv[]) 177 | { 178 | QCoreApplication a(argc, argv); 179 | 180 | //--- Creating and adding data 181 | //test_numbers(); 182 | 183 | //--- Modifying exiting data 184 | //test_modifying(); 185 | 186 | //--- Searching --- 187 | //test_search(); 188 | 189 | //--- Memory considerations --- 190 | //test_deleteall(); 191 | test_smart(); 192 | 193 | return a.exec(); 194 | } 195 | --------------------------------------------------------------------------------