当前位置: 首页 > news >正文

PyQt加载UI文件


1.动态加载

import sys
from PySide6 import QtCore,QtWidgets
from PySide6.QtWidgets import *
from PySide6.QtUiTools import QUiLoader


class readfile(QWidget):
    def __init__(self):
        super().__init__()
        self.ui=QUiLoader().load("test.ui",self)       
        self.__create_connections()

    def __create_connections(self):              
        self.ui.pushButton.clicked.connect(self.open_file)        


    def open_file(self):        
        fname,_ = QFileDialog.getOpenFileName(self,"打开文件", '.')
        if fname:            
            self.ui.lineEdit.setText(fname)            
            with open(fname,'r') as f:
            	content = f.read()
            self.ui.textBrowser.setText(str(content))     
if __name__ == '__main__':
	#QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
	#QApplication.setAttribute(Qt.AA_ShareOpenGLContexts)
	app = QApplication(sys.argv)
	w = readfile()
	w.show()       
	sys.exit(app.exec())

2.使用QFile加载

import sys
from PySide6 import QtCore,QtWidgets
from PySide6.QtWidgets import *
from PySide6.QtCore import QFile
from PySide6.QtUiTools import QUiLoader


class readfile(QWidget):
    def __init__(self):
        super().__init__()
        qfile=QFile("test.ui")
        qfile.open(QFile.ReadOnly)
        qfile.close()
        self.ui=QUiLoader().load(qfile,self)      
        self.__create_connections()

    def __create_connections(self):              
        self.ui.pushButton.clicked.connect(self.open_file)        


    def open_file(self):        
        fname,_ = QFileDialog.getOpenFileName(self,"打开文件", '.')
        if fname:            
            self.ui.lineEdit.setText(fname)            
            with open(fname,'r') as f:
            	content = f.read()
            self.ui.textBrowser.setText(str(content))     
if __name__ == '__main__':
	#QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
	#QApplication.setAttribute(Qt.AA_ShareOpenGLContexts)
	app = QApplication(sys.argv)
	w = readfile()
	w.show()       
	sys.exit(app.exec())

动态加载出有错误信息,但不影响运行
错误信息如下:

qt.pysideplugin: Environment variable PYSIDE_DESIGNER_PLUGINS is not set, bailing out.
qt.pysideplugin: No instance of QPyDesignerCustomWidgetCollection was found.
Qt WebEngine seems to be initialized from a plugin. Please set Qt::AA_ShareOpenGLContexts using QCoreApplication::setAttribute and QSGRendererInterface::OpenGLRhi using QQuickWindow::setGraphicsApi before constructing QGuiApplication.

 

http://www.dtcms.com/a/23972.html

相关文章:

  • x86平台基于Qt+opengl优化ffmpeg软解码1080P视频渲染效率
  • 【GoLang】【算法模板】2、GoLang 算法模板整理
  • Java包装类泛型编程
  • Sa-Token 根据官方文档简单实现登录认证的示例
  • 认识 ADB(Android Debug Bridge,Android SDK 中的一个工具)
  • 排序与算法:插入排序
  • mapbox基础,使用geojson加载line线图层,实现纯色填充、图片填充、虚线和渐变效果
  • 【Qt】Q_OBJECT无法用在模版类中的原因和解决方法
  • GPT-2 大模型
  • 本地部署DeepSeek
  • 提升接口性能之缓存
  • 分治-归并排序
  • 反射和特性
  • Annie导航2.0 新增加5个模版 开源免授权
  • 当机器人遇见艺术:春晚机器人舞蹈,一场科技与艺术的完美邂逅
  • Redis实现登录优化
  • 阿里云前端自动化部署流程指南
  • LayUi点击查看图片组件layer.photos()用法(图片放大预览后滚动鼠标缩放、底部显示自定义标题)
  • 观察者模式
  • uniapp中@input输入事件在修改值只有第一次有效的问题解决
  • RocketMQ面试题:原理部分
  • 组学数据分析实操系列 |(四) 富集气泡图的绘制
  • 书籍翻页动画
  • 系统学习算法:专题十一 floodfill算法
  • 51c自动驾驶~合集51
  • ubuntu22.04离线安装K8S
  • 关于deep seek的本地化部署
  • PHP 网络编程介绍
  • 【信息系统项目管理师】第23章:组织通用管理 详解
  • 字符串操作总结(C# and Lua)