当前位置: 首页 > 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.

 

相关文章:

  • 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输入事件在修改值只有第一次有效的问题解决
  • 工商银行杭州金融研修院原院长蒋伟被“双开”
  • 1至4月全国铁路发送旅客14.6亿人次,创同期历史新高
  • 著名词作家陈哲逝世,代表作《让世界充满爱》《同一首歌》等
  • 思想史家陈谷嘉逝世,曾为岳麓书院当代复兴奠定重要基础
  • 西安市未央区委书记刘国荣已任西咸新区党工委书记
  • 上海市重大工程一季度开局良好,崇明线等按既定计划加快建设