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 1 commit
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,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) {
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::on_fontComboBox_currentFontChanged(const QFont &f)
{
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 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);
Cuperino marked this conversation as resolved.
Show resolved Hide resolved

private slots:
void on_lineEdit_textChanged(const QString &arg1);
Copy link
Contributor

Choose a reason for hiding this comment

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

on_* is definitely not best practice (no error at compile time in case of a mismatch). Please use a real connect to PMF.

Copy link
Author

Choose a reason for hiding this comment

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

Switched to explicit, modern, connects. Removed confusing on_ prefix.
Are my declarations and casing correct or can these be improved?

void on_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}
{
}

QStringList FontsBackend::fontList()
Cuperino marked this conversation as resolved.
Show resolved Hide resolved
{
return QFontDatabase::families();
}
Loading