Bladeren bron

简单的 button、slipper、radio、font、style

mrh 2 jaren geleden
bovenliggende
commit
16b4384f06

+ 1 - 0
.gitignore

@@ -1 +1,2 @@
+__pycache__
 env

+ 0 - 0
main.py → 1-YourFirstQtQuickQMLApplication/main.py


+ 1 - 1
ui/view.qml → 1-YourFirstQtQuickQMLApplication/ui/view.qml

@@ -8,7 +8,7 @@ Rectangle {
 
     Text {
         text: "Hello World"
-        // 浣挎枃鏈�樉绀哄湪id涓猴細main鐨勫�璞$殑涓�績锛屽湪鏈�緥涓�负鐭╁舰
+        // 使文本显示在id为:main的对象的中心,在本例中为矩形
         anchors.centerIn: main
     }
 }

+ 77 - 0
2-Python-QML integration/main.py

@@ -0,0 +1,77 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial
+
+import sys
+from pathlib import Path
+
+from PySide6.QtCore import QObject, Slot
+from PySide6.QtGui import QGuiApplication
+from PySide6.QtQml import QQmlApplicationEngine, QmlElement
+from PySide6.QtQuickControls2 import QQuickStyle
+
+import style_rc
+
+# To be used on the @QmlElement decorator
+# (QML_IMPORT_MINOR_VERSION is optional)
+QML_IMPORT_NAME = "io.qt.textproperties"
+QML_IMPORT_MAJOR_VERSION = 1
+
+
+@QmlElement
+class Bridge(QObject):
+
+    @Slot(str, result=str)
+    def getColor(self, s):
+        if s.lower() == "red":
+            return "#ef9a9a"
+        elif s.lower() == "green":
+            return "#a5d6a7"
+        elif s.lower() == "blue":
+            return "#90caf9"
+        else:
+            return "white"
+
+    @Slot(float, result=int)
+    def getSize(self, s):
+        size = int(s * 34)
+        if size <= 0:
+            return 1
+        else:
+            return size
+
+    @Slot(str, result=bool)
+    def getItalic(self, s):
+        if s.lower() == "italic":
+            return True
+        else:
+            return False
+
+    @Slot(str, result=bool)
+    def getBold(self, s):
+        if s.lower() == "bold":
+            return True
+        else:
+            return False
+
+    @Slot(str, result=bool)
+    def getUnderline(self, s):
+        if s.lower() == "underline":
+            return True
+        else:
+            return False
+
+
+if __name__ == '__main__':
+    app = QGuiApplication(sys.argv)
+    QQuickStyle.setStyle("Material")
+    engine = QQmlApplicationEngine()
+
+    # Get the path of the current directory, and then add the name
+    # of the QML file, to load it.
+    qml_file = 'view.qml'
+    engine.load(qml_file)
+
+    if not engine.rootObjects():
+        sys.exit(-1)
+
+    sys.exit(app.exec())

+ 10 - 0
2-Python-QML integration/qtquickcontrols2.conf

@@ -0,0 +1,10 @@
+[Controls]
+Style=Material
+
+[Universal]
+Theme=System
+Accent=Red
+
+[Material]
+Theme=Dark
+Accent=Red

+ 3 - 0
2-Python-QML integration/readme.md

@@ -0,0 +1,3 @@
+https://doc.qt.io/qtforpython-6/tutorials/qmlintegration/qmlintegration.html
+
+修改 style 颜色,没看出有什么区别

+ 5 - 0
2-Python-QML integration/style.qrc

@@ -0,0 +1,5 @@
+<!DOCTYPE RCC><RCC version="1.0">
+<qresource prefix="/">
+    <file>qtquickcontrols2.conf</file>
+</qresource>
+</RCC>

+ 41 - 0
2-Python-QML integration/style_rc.py

@@ -0,0 +1,41 @@
+# Resource object code (Python 3)
+# Created by: object code
+# Created by: The Resource Compiler for Qt version 6.6.0
+# WARNING! All changes made in this file will be lost!
+
+from PySide6 import QtCore
+
+qt_resource_data = b"\
+\x00\x00\x00i\
+[\
+Controls]\x0d\x0aStyle\
+=Material\x0d\x0a\x0d\x0a[Un\
+iversal]\x0d\x0aTheme=\
+System\x0d\x0aAccent=R\
+ed\x0d\x0a\x0d\x0a[Material]\
+\x0d\x0aTheme=Dark\x0d\x0aAc\
+cent=Red\
+"
+
+qt_resource_name = b"\
+\x00\x15\
+\x08\x1e\x16f\
+\x00q\
+\x00t\x00q\x00u\x00i\x00c\x00k\x00c\x00o\x00n\x00t\x00r\x00o\x00l\x00s\x002\x00.\
+\x00c\x00o\x00n\x00f\
+"
+
+qt_resource_struct = b"\
+\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\
+\x00\x00\x00\x00\x00\x00\x00\x00\
+\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
+\x00\x00\x01\x8b\x88\xad\x7fE\
+"
+
+def qInitResources():
+    QtCore.qRegisterResourceData(0x03, qt_resource_struct, qt_resource_name, qt_resource_data)
+
+def qCleanupResources():
+    QtCore.qUnregisterResourceData(0x03, qt_resource_struct, qt_resource_name, qt_resource_data)
+
+qInitResources()

+ 160 - 0
2-Python-QML integration/view.qml

@@ -0,0 +1,160 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial
+
+
+import QtQuick 2.0
+import QtQuick.Layouts 1.11
+import QtQuick.Controls 2.1
+import QtQuick.Window 2.1
+import QtQuick.Controls.Material 2.1
+
+import io.qt.textproperties 1.0
+
+ApplicationWindow {
+    id: page
+    width: 800
+    height: 400
+    visible: true
+    Material.theme: Material.Dark
+    Material.accent: Material.Red
+
+    Bridge {
+        id: bridge
+    }
+
+    GridLayout {
+        id: grid
+        columns: 2
+        rows: 3
+
+        ColumnLayout {
+            spacing: 2
+            Layout.columnSpan: 1
+            Layout.preferredWidth: 400
+
+            Text {
+                id: leftlabel
+                Layout.alignment: Qt.AlignHCenter
+                color: "white"
+                font.pointSize: 16
+                text: "Qt for Python"
+                Layout.preferredHeight: 100
+                Material.accent: Material.Green
+            }
+            // 在
+            RadioButton {
+                id: italic
+                Layout.alignment: Qt.AlignLeft
+                text: "Italic"
+                onToggled: {
+                    leftlabel.font.italic = bridge.getItalic(italic.text)
+                    leftlabel.font.bold = bridge.getBold(italic.text)
+                    leftlabel.font.underline = bridge.getUnderline(italic.text)
+
+                }
+            }
+            RadioButton {
+                id: bold
+                Layout.alignment: Qt.AlignLeft
+                text: "Bold"
+                onToggled: {
+                    leftlabel.font.italic = bridge.getItalic(bold.text)
+                    leftlabel.font.bold = bridge.getBold(bold.text)
+                    leftlabel.font.underline = bridge.getUnderline(bold.text)
+                }
+            }
+            RadioButton {
+                id: underline
+                Layout.alignment: Qt.AlignLeft
+                text: "Underline"
+                onToggled: {
+                    leftlabel.font.italic = bridge.getItalic(underline.text)
+                    leftlabel.font.bold = bridge.getBold(underline.text)
+                    leftlabel.font.underline = bridge.getUnderline(underline.text)
+                }
+            }
+            RadioButton {
+                id: noneradio
+                Layout.alignment: Qt.AlignLeft
+                text: "None"
+                checked: true
+                onToggled: {
+                    leftlabel.font.italic = bridge.getItalic(noneradio.text)
+                    leftlabel.font.bold = bridge.getBold(noneradio.text)
+                    leftlabel.font.underline = bridge.getUnderline(noneradio.text)
+                }
+            }
+        }
+
+        ColumnLayout {
+            id: rightcolumn
+            spacing: 2
+            Layout.columnSpan: 1
+            Layout.preferredWidth: 400
+            Layout.preferredHeight: 400
+            Layout.fillWidth: true
+
+            RowLayout {
+                Layout.alignment: Qt.AlignVCenter | Qt.AlignHCenter
+
+
+                Button {
+                    id: red
+                    text: "Red"
+                    highlighted: true
+                    Material.accent: Material.Red
+                    onClicked: {
+                        leftlabel.color = bridge.getColor(red.text)
+                    }
+                }
+                Button {
+                    id: green
+                    text: "Green"
+                    highlighted: true
+                    Material.accent: Material.Green
+                    onClicked: {
+                        leftlabel.color = bridge.getColor(green.text)
+                    }
+                }
+                Button {
+                    id: blue
+                    text: "Blue"
+                    highlighted: true
+                    Material.accent: Material.Blue
+                    onClicked: {
+                        leftlabel.color = bridge.getColor(blue.text)
+                    }
+                }
+                Button {
+                    id: nonebutton
+                    text: "None"
+                    highlighted: true
+                    Material.accent: Material.BlueGrey
+                    onClicked: {
+                        leftlabel.color = bridge.getColor(nonebutton.text)
+                    }
+                }
+            }
+            RowLayout {
+                Layout.fillWidth: true
+                Layout.alignment: Qt.AlignVCenter | Qt.AlignHCenter
+                Text {
+                    id: rightlabel
+                    color: "white"
+                    Layout.alignment: Qt.AlignLeft
+                    text: "Font size"
+                    Material.accent: Material.White
+                }
+                Slider {
+                    width: rightcolumn.width*0.6
+                    Layout.alignment: Qt.AlignRight
+                    id: slider
+                    value: 0.5
+                    onValueChanged: {
+                        leftlabel.font.pointSize = bridge.getSize(value)
+                    }
+                }
+            }
+        }
+    }
+}

+ 8 - 0
readme.md

@@ -4,11 +4,19 @@ QML 是一种声明性语言,它能比传统语言更快更好地开发项目
 ```shell
 conda create -p .\env  python=3.10
 pip install PySide6
+conda activate f:\Engineer\python\pySimpleGUI\env
 ```
 安装 vscode QML 预览插件 QML Syntax/Tools 
 
+QML 插件预览出错: gbk cant'decode 在QMl中如果使用中文,记得将编码格式改为 utf8
+
+python -c "import sys; print(sys.getdefaultencoding())"  看到系统编码明明是 utf8 不知道为什么运行插件会出错
+
+安装 vscode 插件 Qt For Python
+
 创建第一个应用: https://doc.qt.io/qtforpython-6/tutorials/basictutorial/qml.html
 
+详见: 1-YourFirstQtQuickQMLApplication
 
 
 # 为什么要使用 Pyside6