Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add example for Widget window in Qt Quick #4

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions Blog-projects/Widget-window-in-Qt-Quick-app/.gitignore
Original file line number Diff line number Diff line change
@@ -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*
48 changes: 48 additions & 0 deletions Blog-projects/Widget-window-in-Qt-Quick-app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}
)
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
74 changes: 74 additions & 0 deletions Blog-projects/Widget-window-in-Qt-Quick-app/Main.qml
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misunderstanding.
Returning a const QString serves no purpose (on the contrary - it prevents move semantics for the caller)

When I said "make those methods const", I meant marking the actual methods themselves as const:

QString FontControlsWidgetsForm::font() const

(both in .h and .cpp)

(applies to all getters)

The purpose is to indicate (and ensure) that the member variables are not (and cannot be) modified in such methods, because in a const method, all member variables are const.

{
return ui->fontComboBox->currentFont().family();
}

void FontControlsWidgetsForm::closeEvent(QCloseEvent *event) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent formatting: you inserted a newline before '{' in most other methods (which is indeed more common).

You could grab the _clang-format from e.g. KDABViewer and run clang-format over the whole code, for consistent formatting.

QApplication::quit();
}

void FontControlsWidgetsForm::fontComboBox_currentFontChanged(const QFont &f)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I see why you asked about casing... This snake/camel mix is weird ;)

Suggestions:
slotFontChanged
onFontChanged

or in you really need to mention the originating widget, say if you have two lineEdits and you were connecting to textChanged() from each, you'd say something like slotNameChanged and slotCountryChanged or possibly slotNameTextChanged/slotCountryTextChanged. But it's not like the exact name of the signal has to be in the slot name. Only if you were connecting to multiple similar signals.

{
emit fontChanged();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef FONTCONTROLSWIDGETSFORM_H
#define FONTCONTROLSWIDGETSFORM_H

#include <QWidget>

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buttonClicked()


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
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>FontControlsWidgetsForm</class>
<widget class="QWidget" name="FontControlsWidgetsForm">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>300</width>
<height>110</height>
</rect>
</property>
<property name="windowTitle">
<string>Font Controls - Qt Widgets</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLineEdit" name="lineEdit">
<property name="text">
<string>KDAB</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Switch to QML Form</string>
</property>
</widget>
</item>
<item>
<widget class="QFontComboBox" name="fontComboBox"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
13 changes: 13 additions & 0 deletions Blog-projects/Widget-window-in-Qt-Quick-app/fontsbackend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "fontsbackend.h"

#include <QFontDatabase>

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

const QStringList FontsBackend::fontList()
{
return QFontDatabase::families();
}
Loading