From f3996f4caca6971bb2c244bb47c7b82336476017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20O=2E=20Cordero=20P=C3=A9rez?= Date: Tue, 19 Nov 2024 10:03:16 -0400 Subject: [PATCH 1/2] Add example for Widget window in Qt Quick Example app wher a window bounces across the screen, like an old DVD player's logo would. You control the text through one of two identical forms, one implemented in QML and the other in Widgets. The example code follows best practices. Due to limitations with Wayland, it only works on X11 on Linux. --- .../Widget-window-in-Qt-Quick-app/.gitignore | 75 +++++++++++++++++++ .../CMakeLists.txt | 48 ++++++++++++ .../FontControlsQmlForm.qml | 51 +++++++++++++ .../Widget-window-in-Qt-Quick-app/Main.qml | 74 ++++++++++++++++++ .../fontcontrolswidgetsform.cpp | 45 +++++++++++ .../fontcontrolswidgetsform.h | 40 ++++++++++ .../fontcontrolswidgetsform.ui | 38 ++++++++++ .../fontsbackend.cpp | 13 ++++ .../fontsbackend.h | 21 ++++++ .../Widget-window-in-Qt-Quick-app/main.cpp | 18 +++++ .../Widget-window-in-Qt-Quick-app/timer.cpp | 12 +++ .../Widget-window-in-Qt-Quick-app/timer.h | 23 ++++++ .../widgetFormHandler.cpp | 42 +++++++++++ .../widgetFormHandler.h | 37 +++++++++ 14 files changed, 537 insertions(+) create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/.gitignore create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/CMakeLists.txt create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/FontControlsQmlForm.qml create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/Main.qml create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.cpp create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.h create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.ui create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.cpp create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.h create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/main.cpp create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/timer.cpp create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/timer.h create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.cpp create mode 100644 Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.h 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..297d712 --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.cpp @@ -0,0 +1,45 @@ +#include "fontcontrolswidgetsform.h" +#include "ui_fontcontrolswidgetsform.h" + +FontControlsWidgetsForm::FontControlsWidgetsForm(QWidget *parent) + : QWidget(parent) + , ui(new Ui::FontControlsWidgetsForm) +{ + ui->setupUi(this); +} + +FontControlsWidgetsForm::~FontControlsWidgetsForm() +{ + delete ui; +} + +QString FontControlsWidgetsForm::getText() +{ + return ui->lineEdit->text(); +} + +void FontControlsWidgetsForm::on_lineEdit_textChanged(const QString &arg1) +{ + emit textChanged(); +} + +void FontControlsWidgetsForm::setText(const QString &text) +{ + ui->lineEdit->setText(text); + emit textChanged(); +} + +QString FontControlsWidgetsForm::getFont() +{ + return ui->fontComboBox->currentFont().family(); +} + +void FontControlsWidgetsForm::closeEvent(QCloseEvent *event) { + QApplication::quit(); +} + +void FontControlsWidgetsForm::on_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..47e45b6 --- /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 getText WRITE setText NOTIFY textChanged) + Q_PROPERTY(QString font READ getFont NOTIFY fontChanged) + +public: + explicit FontControlsWidgetsForm(QWidget *parent = nullptr); + ~FontControlsWidgetsForm(); + + QString getText(); + void setText(const QString&); + QString getFont(); + +signals: + void textChanged(); + void fontChanged(); + void on_pushButton_clicked(); + +protected: + void closeEvent(QCloseEvent *event); + +private slots: + void on_lineEdit_textChanged(const QString &arg1); + void on_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..49bd254 --- /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} +{ +} + +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..a6b4226 --- /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 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..c4fda93 --- /dev/null +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.cpp @@ -0,0 +1,42 @@ +#include "widgetFormHandler.h" + +WidgetFormHandler::WidgetFormHandler(QObject *parent) + : QObject{parent} +{ + m_window = new FontControlsWidgetsForm(); + m_window->setVisible(false); + + QObject::connect(m_window, &FontControlsWidgetsForm::on_pushButton_clicked, this, &WidgetFormHandler::toggleWidgetsWindow); + QObject::connect(m_window, &FontControlsWidgetsForm::textChanged, this, &WidgetFormHandler::textChanged); + QObject::connect(m_window, &FontControlsWidgetsForm::fontChanged, this, &WidgetFormHandler::fontChanged); +} + +bool WidgetFormHandler::isVisible() +{ + return m_window->isVisible(); +} + +void WidgetFormHandler::setVisible(bool visible) +{ + if (visible) + m_window->show(); + else + m_window->hide(); + emit visibleChanged(); +} + +QString WidgetFormHandler::getText() +{ + return m_window->getText(); +} + +void WidgetFormHandler::setText(const QString& text) +{ + m_window->setText(text); + emit textChanged(); +} + +QString WidgetFormHandler::getFont() +{ + return m_window->getFont(); +} 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..0817d11 --- /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 getText WRITE setText NOTIFY textChanged) + Q_PROPERTY(QString font READ getFont NOTIFY fontChanged) + +public: + explicit WidgetFormHandler(QObject *parent = nullptr); + + bool isVisible(); + void setVisible(bool); + QString getText(); + void setText(const QString&); + QString getFont(); + +signals: + void visibleChanged(); + void textChanged(); + void fontChanged(); + void toggleWidgetsWindow(); + +private: + FontControlsWidgetsForm *m_window; +}; + +#endif // WIDGETFORMHANDLER_H From e3b41948ac3ace44281ce3bea0f44c2ff8b4009c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20O=2E=20Cordero=20P=C3=A9rez?= Date: Sun, 24 Nov 2024 23:37:36 -0400 Subject: [PATCH 2/2] Implement best practice recommendations - Dropped on_ from UI form actions naming pattern - Manually connected to UI form actions - Made getters const - Renamed getters to follow - Turn fontList into a static const member function - Indicate function override --- .../fontcontrolswidgetsform.cpp | 12 +++++++----- .../fontcontrolswidgetsform.h | 16 ++++++++-------- .../fontsbackend.cpp | 2 +- .../fontsbackend.h | 2 +- .../widgetFormHandler.cpp | 17 +++++++---------- .../widgetFormHandler.h | 10 +++++----- 6 files changed, 29 insertions(+), 30 deletions(-) diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.cpp b/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.cpp index 297d712..ed97eec 100644 --- a/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.cpp +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.cpp @@ -6,6 +6,9 @@ FontControlsWidgetsForm::FontControlsWidgetsForm(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() @@ -13,12 +16,12 @@ FontControlsWidgetsForm::~FontControlsWidgetsForm() delete ui; } -QString FontControlsWidgetsForm::getText() +const QString FontControlsWidgetsForm::text() { return ui->lineEdit->text(); } -void FontControlsWidgetsForm::on_lineEdit_textChanged(const QString &arg1) +void FontControlsWidgetsForm::lineEdit_textChanged(const QString &arg1) { emit textChanged(); } @@ -29,7 +32,7 @@ void FontControlsWidgetsForm::setText(const QString &text) emit textChanged(); } -QString FontControlsWidgetsForm::getFont() +const QString FontControlsWidgetsForm::font() { return ui->fontComboBox->currentFont().family(); } @@ -38,8 +41,7 @@ void FontControlsWidgetsForm::closeEvent(QCloseEvent *event) { QApplication::quit(); } -void FontControlsWidgetsForm::on_fontComboBox_currentFontChanged(const QFont &f) +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 index 47e45b6..688b6c2 100644 --- a/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.h +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.h @@ -10,28 +10,28 @@ class FontControlsWidgetsForm; class FontControlsWidgetsForm : public QWidget { Q_OBJECT - Q_PROPERTY(QString text READ getText WRITE setText NOTIFY textChanged) - Q_PROPERTY(QString font READ getFont NOTIFY fontChanged) + 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(); - QString getText(); + const QString text(); void setText(const QString&); - QString getFont(); + const QString font(); signals: void textChanged(); void fontChanged(); - void on_pushButton_clicked(); + void pushButton_clicked(); protected: - void closeEvent(QCloseEvent *event); + void closeEvent(QCloseEvent *event) override; private slots: - void on_lineEdit_textChanged(const QString &arg1); - void on_fontComboBox_currentFontChanged(const QFont &f); + void lineEdit_textChanged(const QString &arg1); + void fontComboBox_currentFontChanged(const QFont &f); private: Ui::FontControlsWidgetsForm *ui; diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.cpp b/Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.cpp index 49bd254..60fd437 100644 --- a/Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.cpp +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.cpp @@ -7,7 +7,7 @@ FontsBackend::FontsBackend(QObject *parent) { } -QStringList FontsBackend::fontList() +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 index a6b4226..37299ac 100644 --- a/Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.h +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.h @@ -13,7 +13,7 @@ class FontsBackend : public QObject public: explicit FontsBackend(QObject *parent = nullptr); - Q_INVOKABLE QStringList fontList(); + Q_INVOKABLE static const QStringList fontList(); signals: }; diff --git a/Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.cpp b/Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.cpp index c4fda93..afde5c1 100644 --- a/Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.cpp +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.cpp @@ -6,28 +6,25 @@ WidgetFormHandler::WidgetFormHandler(QObject *parent) m_window = new FontControlsWidgetsForm(); m_window->setVisible(false); - QObject::connect(m_window, &FontControlsWidgetsForm::on_pushButton_clicked, this, &WidgetFormHandler::toggleWidgetsWindow); + 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); } -bool WidgetFormHandler::isVisible() +const bool WidgetFormHandler::isVisible() { return m_window->isVisible(); } void WidgetFormHandler::setVisible(bool visible) { - if (visible) - m_window->show(); - else - m_window->hide(); + m_window->setVisible(visible); emit visibleChanged(); } -QString WidgetFormHandler::getText() +const QString WidgetFormHandler::text() { - return m_window->getText(); + return m_window->text(); } void WidgetFormHandler::setText(const QString& text) @@ -36,7 +33,7 @@ void WidgetFormHandler::setText(const QString& text) emit textChanged(); } -QString WidgetFormHandler::getFont() +const QString WidgetFormHandler::font() { - return m_window->getFont(); + 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 index 0817d11..88aea6c 100644 --- a/Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.h +++ b/Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.h @@ -12,17 +12,17 @@ class WidgetFormHandler : public QObject Q_OBJECT QML_ELEMENT Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged) - Q_PROPERTY(QString text READ getText WRITE setText NOTIFY textChanged) - Q_PROPERTY(QString font READ getFont NOTIFY fontChanged) + Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) + Q_PROPERTY(QString font READ font NOTIFY fontChanged) public: explicit WidgetFormHandler(QObject *parent = nullptr); - bool isVisible(); + const bool isVisible(); void setVisible(bool); - QString getText(); + const QString text(); void setText(const QString&); - QString getFont(); + const QString font(); signals: void visibleChanged();