Skip to main content
 首页 » 编程设计

python之QML + PyQt5 Material 风格

2025年02月15日9daizhj

我有一个简单的 PyQt5 应用程序,其中包含 QML 文件。我想为应用程序设置 Material 样式,但它返回错误。

import QtQuick 2.3 
import QtQuick.Controls 2.1 
import QtQuick.Window 2.2 
import QtQuick.Controls.Material 2.1     
 
ApplicationWindow{ 
  title: qsTr('Qml app') 
  id: mainWindow 
  width:  500 
  height: 400 
  color: '#f6f6f6' 
} 

我该如何解决?请帮帮我 我的 python 代码是:

import sys 
import PyQt5 
from PyQt5 import QtWidgets 
from PyQt5.QtQuick import QQuickView 
 
if __name__ == '__main__': 
     myApp = PyQt5.QtWidgets.QApplication(sys.argv) 
     engine = PyQt5.QtQml.QQmlApplicationEngine() 
     ctx = engine.rootContext() 
     ctx.setContextProperty('main',engine) 
     engine.load('styles/mat.qml') 
     win = engine.rootObjects()[0] 
 
     win.show() 
     sys.exit(myApp.exec_()) 

错误信息:

Traceback (most recent call last):<br> 
  File "C:\My\Python programms\app8.py", line 12, in <module><br> 
    win = engine.rootObjects()[0]<br> 
IndexError: list index out of range<br> 

我正在使用 Python 2.7

请您参考如下方法:

Material Style 从 Qt 5.7 版本开始可用。