Qt第五十五章:Qt Design Studio设计登录页并打包到python运行
创始人
2024-05-04 06:53:37
0

目录

一、Qt Design Studio

二、导出所有文件到QRC(不要改动默认的QRC文件名称)

三、QRC转换成py

1.删除Constants.qml中的

2.将App.qml和Screen01.qml中的

3.转换

4、将QRC文件和转换后的py文件,复制到python项目中使用。


一、Qt Design Studio

/*
This is a UI file (.ui.qml) that is intended to be edited in Qt Design Studio only.
It is supposed to be strictly declarative and only uses a subset of QML. If you edit
this file manually, you might introduce QML code that is not supported by Qt Design Studio.
Check out https://doc.qt.io/qtcreator/creator-quick-ui-forms.html for details on .ui.qml files.
*/
import QtQuick 6.2
import QtQuick.Controls 6.2
import UntitledProjectRectangle {width: Constants.widthheight: Constants.heightcolor: Constants.backgroundColorscale: 1AnimatedImage {id: v233ef8aa204a741d9472bfe93b618bbd5_bx: 0y: 0source: "images/v2-33ef8aa204a741d9472bfe93b618bbd5_b.gif"fillMode: Image.PreserveAspectFitButton {id: button_loginwidth: 200height: 30text: qsTr("登录")anchors.verticalCenter: textInput_password.verticalCenterfont.wordSpacing: 0font.pointSize: 12font.bold: trueanchors.verticalCenterOffset: 80//水平居中anchors.horizontalCenter: parent.horizontalCenter//内边距rightPadding: 0bottomPadding: 0leftPadding: button_login.pressed?2:0topPadding: button_login.pressed?2:0//点击事件onClicked: console.log("点击了")//重写Rectangle来设置背景background: Rectangle  {//透明度opacity: 0.3//圆角radius: 20//抗锯齿antialiasing: true//边框宽度border.width: button_login.pressed?3:1//边框颜色border.color: button_login.pressed?"blue":"#ffffff"//背景颜色color: {if (button_login.pressed|button_login.hovered) {//悬停或按下状态颜色Qt.rgba(0,0,0,100)} else {//静态颜色"#ffffff"}}}//重写Text来设置字体颜色contentItem:Text {text: button_login.textcolor: button_login.pressed|button_login.hovered?"white":"black"horizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}TextField {id: textInput_username//悬停或获取焦点 放大输入框width: textInput_username.activeFocus|textInput_username.hovered?205:200height: textInput_username.activeFocus|textInput_username.hovered?35:30anchors.verticalCenter: textInput_password.verticalCenterfont.pixelSize: 12//字体白色color: 'white'placeholderText: "账号"anchors.verticalCenterOffset: -50anchors.horizontalCenterOffset: 0//水平居中anchors.horizontalCenter: textInput_password.horizontalCenter//重写Rectangle设置背景background: Rectangle  {//透明度opacity: 0.3//圆角radius: 20//抗锯齿antialiasing: true//边框宽度border.width: textInput_username.activeFocus?2:0//边框颜色border.color: textInput_username.activeFocus?"blue":"#ffffff"}}TextField {id: textInput_password//悬停或获取焦点 放大输入框width: textInput_password.activeFocus|textInput_password.hovered?205:200height: textInput_password.activeFocus|textInput_password.hovered?35:30anchors.verticalCenter: parent.verticalCenterfont.pixelSize: 12//字体白色color: 'white'placeholderText: "密码"
//            placeholderTextColor: "black"echoMode: "Password"//水平居中anchors.horizontalCenter: parent.horizontalCenter//重写Rectangle 设置背景background: Rectangle  {//透明度opacity: 0.3//圆角radius: 20//抗锯齿antialiasing: true//边框宽度border.width: textInput_password.activeFocus?2:0//边框颜色border.color: textInput_password.activeFocus?"blue":"#ffffff"}}}
}

 

二、导出所有文件到QRC(不要改动默认的QRC文件名称)

三、QRC转换成py

将导包换成导入qrc资源:

1.删除Constants.qml中的

import QtQuick.Studio.Application
property StudioApplication application: StudioApplication {fontPath: Qt.resolvedUrl("../../content/" + relativeFontDirectory)
}

2.将App.qml和Screen01.qml中的

import UntitledProject换成下面(注意:冒号前的qrc不能少,用英文单引号包起来)
import 'qrc:/imports/UntitledProject'

3.转换

4、将QRC文件和转换后的py文件,复制到python项目中使用。

import sysfrom PySide6.QtCore import QObject, Slot
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtQuick import QQuickItem
from PySide6.QtWidgets import QApplication
import UntitledProject_rc# 槽函数类(一个承载槽函数的容器类)
class Slots(QObject):def __init__(self, objects):self.objects = objectssuper().__init__()@Slot(str, result=None)def set_text_msg(self, msg):# 获取qml中的Text对象child = self.objects[0].findChild(QQuickItem, "text1")# 获取对象属性p = child.property("text")# 设置对象属性child.setProperty("text", p + msg)@Slot(result=str)def get_text_msg(self):return "皎氯""""
这种方式是以Qml作为窗口来使用。所有的逻辑UI都由Qml来完成。python提供可以调用数据的API即可。
"""class LoginQuickWidget:def __init__(self):# 初始化UIself.engine = QQmlApplicationEngine()# 加载qml文件self.engine.load(u":/content/App.qml")if not self.engine.rootObjects():sys.exit(-1)# qml对象集合objects = self.engine.rootObjects()# 实例化槽函数self.slots = Slots(objects)# 注入槽函数self.engine.rootContext().setContextProperty('slots', self.slots)if __name__ == '__main__':app = QApplication([])quick_widget = LoginQuickWidget()app.exec()

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...