本示例展示了如何使用pyside加载 QML 文件,并与python代码交互。
main.py文件代码及注释如下:
import sysfrom pathlib import Pathfrom PySide6.QtCore import QObject, Slotfrom PySide6.QtGui import QGuiApplicationfrom PySide6.QtQml import QQmlApplicationEngine, QmlElementfrom PySide6.QtQuickControls2 import QQuickStyleQML_IMPORT_NAME = "io.qt.textproperties"QML_IMPORT_MAJOR_VERSION = 1@QmlElement#使用@QmlElement装饰器class Bridge(QObject): #定义Bridge类 @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 Falseif __name__ == '__main__': app = QGuiApplication(sys.argv) QQuickStyle.setStyle("Material") engine = QQmlApplicationEngine() qml_file = Path(__file__).parent / 'view.qml'#获取当前目录的路径,然后添加qml文件 engine.load(qml_file)#加载qml文件 if not engine.rootObjects(): sys.exit(-1) sys.exit(app.exec())
view.qml文件代码及注释如下:
import QtQuick 2.0import QtQuick.Layouts 1.11import QtQuick.Controls 2.1import QtQuick.Window 2.1import QtQuick.Controls.Material 2.1import io.qt.textproperties 1.0ApplicationWindow { id: page width: 800 height: 400 visible: true Material.theme: Material.Dark Material.accent: Material.Red Bridge {//将信号连接到python中的Bridge类 id: bridge } GridLayout {//网格布局 id: grid columns: 2//2列 rows: 3//3行 ColumnLayout {//列布局 spacing: 2//元素间隔 Layout.columnSpan: 1//此属性允许您指定GridLayout中项目的列跨度。默认值为1。 Layout.preferredWidth: 400//此属性保存布局中项目的首选宽度 Text { id: leftlabel Layout.alignment: Qt.AlignHCenter//水平居中对齐 color: "white" font.pointSize: 16 text: "你好Pyside6" Layout.preferredHeight: 100 Material.accent: Material.Green } RadioButton { id: italic Layout.alignment: Qt.AlignLeft//左居中对齐 text: "Italic" onToggled: {//选择触发事件 //函数getItalic()返回bool值给leftlabel.font.italic设置字体是否为斜体 leftlabel.font.italic = bridge.getItalic(italic.text) //函数getBold()返回bool值给leftlabel.font.bold设置字体是否是粗体 leftlabel.font.bold = bridge.getBold(italic.text) //函数getUnderline()返回bool值给leftlabel.font.underline设置字体是否右下划线 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: { //函数getColor()返回颜色赋值给leftlabel.color 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: { //函数getSize()返回int值,赋值给leftlabel.font.pointSize改变字体大小 leftlabel.font.pointSize = bridge.getSize(value) } } } } }}
将main.py与view.qml放到同一文件夹,运行main.py,显示如下:
版权声明:内容来源于互联网和用户投稿 如有侵权请联系删除