diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/.gitignore b/Blog-projects/Widget-window-in-Qt-Quick-app/.gitignore new file mode 100644 index 0000000..e25c4d9 --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/.gitignore @@ -0,0 +1,75 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* +CMakeLists.txt.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + +*build* diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/CMakeLists.txt b/Blog-projects/Widget-window-in-Qt-Quick-app/CMakeLists.txt new file mode 100644 index 0000000..49e07f0 --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/CMakeLists.txt @@ -0,0 +1,48 @@ +cmake_minimum_required(VERSION 3.16) + +project(WidgetWindowsInQtQuickApp VERSION 0.1 LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(Qt6 6.5 REQUIRED COMPONENTS Quick Widgets) + +qt_standard_project_setup(REQUIRES 6.5) + +qt_add_executable(appWidgetWindowsInQtQuickApp + main.cpp +) + +qt_add_qml_module(appWidgetWindowsInQtQuickApp + URI WidgetWindowsInQtQuickApp + VERSION 1.0 + QML_FILES + Main.qml + FontControlsQmlForm.qml + SOURCES + fontsbackend.h fontsbackend.cpp + fontcontrolswidgetsform.h fontcontrolswidgetsform.cpp + timer.h timer.cpp + widgetFormHandler.h widgetFormHandler.cpp + RESOURCES fontcontrolswidgetsform.ui +) + +# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. +# If you are developing for iOS or macOS you should consider setting an +# explicit, fixed bundle identifier manually though. +set_target_properties(appWidgetWindowsInQtQuickApp PROPERTIES + MACOSX_BUNDLE_GUI_IDENTIFIER com.kdab.appWidgetWindowsInQtQuickApp + MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} + MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} + MACOSX_BUNDLE TRUE + WIN32_EXECUTABLE TRUE +) + +target_link_libraries(appWidgetWindowsInQtQuickApp + PRIVATE Qt6::Quick Qt6::Widgets) + +include(GNUInstallDirs) +install(TARGETS appWidgetWindowsInQtQuickApp + BUNDLE DESTINATION . + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} +) diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/FontControlsQmlForm.qml b/Blog-projects/Widget-window-in-Qt-Quick-app/FontControlsQmlForm.qml new file mode 100644 index 0000000..66a8bac --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/FontControlsQmlForm.qml @@ -0,0 +1,51 @@ +import QtQuick +import QtQuick.Controls +import QtQuick.Layouts +import WidgetWindowsInQtQuickApp as Cpp + +Window { + id: window + readonly property alias text: textField.text + readonly property string font: fontSelector.model[fontSelector.currentIndex] + signal toggleWidgetsWindow + height: 110 + width: 300 + minimumHeight: 110 + minimumWidth: 260 + visible: true + title: qsTr("Font Controls - Qt Quick") + color: systemPalette.window + SystemPalette { + id: systemPalette + } + ColumnLayout { + anchors.fill: parent + anchors.margins: 10 + TextField { + id: textField + text: "KDAB" + focus: true + Layout.fillWidth: true + } + Button { + text: "Switch to Widgets Form" + Layout.fillWidth: true + onClicked: { + window.toggleWidgetsWindow(); + } + } + ComboBox { + id: fontSelector + model: fontsBackend.fontList() + editable: true + Layout.fillWidth: true + Component.onCompleted: { + if (!fontSelector.currentIndex) + currentIndex = indexOfValue("Noto Sans") + } + Cpp.FontsBackend { + id: fontsBackend + } + } + } +} diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/Main.qml b/Blog-projects/Widget-window-in-Qt-Quick-app/Main.qml new file mode 100644 index 0000000..5e08718 --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/Main.qml @@ -0,0 +1,74 @@ +import QtQuick +import WidgetWindowsInQtQuickApp as Cpp + +Window { + id: frame + property bool widgetsWindow: false + function updatePosition(miliseconds: int): void { + const time = miliseconds / 1000.0; + const aspectRatio = boundary.right / boundary.bottom * 11 + x = triangleWave(time, boundary.right, aspectRatio); + y = triangleWave(time, boundary.bottom, 1-aspectRatio); + } + function triangleWave(x: double, amplitude: int, period: double): int { + return Math.abs((2*amplitude)/Math.PI*Math.asin(Math.sin((2*Math.PI)/period*x))); + } + function toggleWidgetsWindow() { + frame.widgetsWindow = !frame.widgetsWindow + } + flags: Qt.FramelessWindowHint + color: "transparent" + visible: false + width: bounce.width + 1 + height: bounce.height + QtObject { + id: boundary + readonly property int right: Screen.desktopAvailableWidth - frame.width + readonly property int bottom: Screen.desktopAvailableHeight - frame.height + } + Text { + id: bounce + text: frame.widgetsWindow ? fontWidgetsForm.text : fontQmlForm.text + font.pixelSize: 120 + font.family: frame.widgetsWindow ? fontWidgetsForm.font : fontQmlForm.font + color: "#0077C8" + Rectangle { + color: "transparent" + anchors.fill: parent + border.color: bounce.color + border.width: 4 + } + } + Timer { + id: timer + readonly property int startOffset: Math.random() * 36000 + running: true + triggeredOnStart: true + repeat: true + interval: 10 + onTriggered: { + frame.updatePosition(startOffset + elapsedtimer.deltaTime()); + frame.visible = true; + } + } + Cpp.Timer { + id: elapsedtimer + } + FontControlsQmlForm { + id: fontQmlForm + visible: !frame.widgetsWindow + onToggleWidgetsWindow: () => { + frame.toggleWidgetsWindow(); + } + onClosing: { + Qt.quit(); + } + } + Cpp.WidgetFormHandler { + id: fontWidgetsForm + visible: frame.widgetsWindow + onToggleWidgetsWindow: () => { + frame.toggleWidgetsWindow(); + } + } +} diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.cpp b/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.cpp new file mode 100644 index 0000000..ed97eec --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.cpp @@ -0,0 +1,47 @@ +#include "fontcontrolswidgetsform.h" +#include "ui_fontcontrolswidgetsform.h" + +FontControlsWidgetsForm::FontControlsWidgetsForm(QWidget *parent) + : QWidget(parent) + , ui(new Ui::FontControlsWidgetsForm) +{ + ui->setupUi(this); + connect(ui->lineEdit, &QLineEdit::textEdited, this, &FontControlsWidgetsForm::lineEdit_textChanged); + connect(ui->fontComboBox, &QFontComboBox::currentFontChanged, this, &FontControlsWidgetsForm::fontComboBox_currentFontChanged); + connect(ui->pushButton, &QPushButton::clicked, this, &FontControlsWidgetsForm::pushButton_clicked); +} + +FontControlsWidgetsForm::~FontControlsWidgetsForm() +{ + delete ui; +} + +const QString FontControlsWidgetsForm::text() +{ + return ui->lineEdit->text(); +} + +void FontControlsWidgetsForm::lineEdit_textChanged(const QString &arg1) +{ + emit textChanged(); +} + +void FontControlsWidgetsForm::setText(const QString &text) +{ + ui->lineEdit->setText(text); + emit textChanged(); +} + +const QString FontControlsWidgetsForm::font() +{ + return ui->fontComboBox->currentFont().family(); +} + +void FontControlsWidgetsForm::closeEvent(QCloseEvent *event) { + QApplication::quit(); +} + +void FontControlsWidgetsForm::fontComboBox_currentFontChanged(const QFont &f) +{ + emit fontChanged(); +} diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.h b/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.h new file mode 100644 index 0000000..688b6c2 --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.h @@ -0,0 +1,40 @@ +#ifndef FONTCONTROLSWIDGETSFORM_H +#define FONTCONTROLSWIDGETSFORM_H + +#include + +namespace Ui { +class FontControlsWidgetsForm; +} + +class FontControlsWidgetsForm : public QWidget +{ + Q_OBJECT + Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) + Q_PROPERTY(QString font READ font NOTIFY fontChanged) + +public: + explicit FontControlsWidgetsForm(QWidget *parent = nullptr); + ~FontControlsWidgetsForm(); + + const QString text(); + void setText(const QString&); + const QString font(); + +signals: + void textChanged(); + void fontChanged(); + void pushButton_clicked(); + +protected: + void closeEvent(QCloseEvent *event) override; + +private slots: + void lineEdit_textChanged(const QString &arg1); + void fontComboBox_currentFontChanged(const QFont &f); + +private: + Ui::FontControlsWidgetsForm *ui; +}; + +#endif // FONTCONTROLSWIDGETSFORM_H diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.ui b/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.ui new file mode 100644 index 0000000..c3d9eb9 --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.ui @@ -0,0 +1,38 @@ + + + FontControlsWidgetsForm + + + + 0 + 0 + 300 + 110 + + + + Font Controls - Qt Widgets + + + + + + KDAB + + + + + + + Switch to QML Form + + + + + + + + + + + diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.cpp b/Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.cpp new file mode 100644 index 0000000..60fd437 --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.cpp @@ -0,0 +1,13 @@ +#include "fontsbackend.h" + +#include + +FontsBackend::FontsBackend(QObject *parent) + : QObject{parent} +{ +} + +const QStringList FontsBackend::fontList() +{ + return QFontDatabase::families(); +} diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.h b/Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.h new file mode 100644 index 0000000..37299ac --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.h @@ -0,0 +1,21 @@ +#ifndef FONTSBACKEND_H +#define FONTSBACKEND_H + +#include +#include + +class FontsBackend : public QObject +{ + Q_OBJECT + QML_ELEMENT +private: + +public: + explicit FontsBackend(QObject *parent = nullptr); + + Q_INVOKABLE static const QStringList fontList(); + +signals: +}; + +#endif // FONTSBACKEND_H diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/main.cpp b/Blog-projects/Widget-window-in-Qt-Quick-app/main.cpp new file mode 100644 index 0000000..97de368 --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/main.cpp @@ -0,0 +1,18 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + QQmlApplicationEngine engine; + QObject::connect( + &engine, + &QQmlApplicationEngine::objectCreationFailed, + &app, + []() { QCoreApplication::exit(-1); }, + Qt::QueuedConnection); + engine.loadFromModule("WidgetWindowsInQtQuickApp", "Main"); + + return app.exec(); +} diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/timer.cpp b/Blog-projects/Widget-window-in-Qt-Quick-app/timer.cpp new file mode 100644 index 0000000..b3d178d --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/timer.cpp @@ -0,0 +1,12 @@ +#include "timer.h" + +Timer::Timer(QObject *parent) + : QObject{parent} +{ + m_timer.start(); +} + +int Timer::deltaTime() +{ + return m_timer.elapsed(); +} diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/timer.h b/Blog-projects/Widget-window-in-Qt-Quick-app/timer.h new file mode 100644 index 0000000..7296334 --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/timer.h @@ -0,0 +1,23 @@ +#ifndef TIMER_H +#define TIMER_H + +#include +#include +#include + +class Timer : public QObject +{ + Q_OBJECT + QML_ELEMENT +private: + QElapsedTimer m_timer; + +public: + explicit Timer(QObject *parent = nullptr); + + Q_INVOKABLE int deltaTime(); + +signals: +}; + +#endif // TIMER_H diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.cpp b/Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.cpp new file mode 100644 index 0000000..afde5c1 --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.cpp @@ -0,0 +1,39 @@ +#include "widgetFormHandler.h" + +WidgetFormHandler::WidgetFormHandler(QObject *parent) + : QObject{parent} +{ + m_window = new FontControlsWidgetsForm(); + m_window->setVisible(false); + + QObject::connect(m_window, &FontControlsWidgetsForm::pushButton_clicked, this, &WidgetFormHandler::toggleWidgetsWindow); + QObject::connect(m_window, &FontControlsWidgetsForm::textChanged, this, &WidgetFormHandler::textChanged); + QObject::connect(m_window, &FontControlsWidgetsForm::fontChanged, this, &WidgetFormHandler::fontChanged); +} + +const bool WidgetFormHandler::isVisible() +{ + return m_window->isVisible(); +} + +void WidgetFormHandler::setVisible(bool visible) +{ + m_window->setVisible(visible); + emit visibleChanged(); +} + +const QString WidgetFormHandler::text() +{ + return m_window->text(); +} + +void WidgetFormHandler::setText(const QString& text) +{ + m_window->setText(text); + emit textChanged(); +} + +const QString WidgetFormHandler::font() +{ + return m_window->font(); +} diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.h b/Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.h new file mode 100644 index 0000000..88aea6c --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.h @@ -0,0 +1,37 @@ +#ifndef WIDGETFORMHANDLER_H +#define WIDGETFORMHANDLER_H + +#include "fontcontrolswidgetsform.h" + +#include +#include +#include + +class WidgetFormHandler : public QObject +{ + Q_OBJECT + QML_ELEMENT + Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged) + Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) + Q_PROPERTY(QString font READ font NOTIFY fontChanged) + +public: + explicit WidgetFormHandler(QObject *parent = nullptr); + + const bool isVisible(); + void setVisible(bool); + const QString text(); + void setText(const QString&); + const QString font(); + +signals: + void visibleChanged(); + void textChanged(); + void fontChanged(); + void toggleWidgetsWindow(); + +private: + FontControlsWidgetsForm *m_window; +}; + +#endif // WIDGETFORMHANDLER_H