开发环境:PyQt 5.5.1 Python 3.4.4 Qt 5.6.3
- Qt Creator新建项目
通过New File or Project->Qt Quick Controls UI新建一个项目,需勾选With ui.qml file。Ctrl+R运行程序,效果如下:
- PyQt程序编写
参考教程 PyQt5 - Lesson 007. Works with QML QtQuick,在第一步中创建的项目目录untitled下新建一个py文件,代码如下:
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
if __name__ == "__main__":
import sys
# Create an instance of the application
app = QGuiApplication(sys.argv)
# Create QML engine
engine = QQmlApplicationEngine()
# Load the qml file into the engine
engine.load("untitled.qml")
engine.quit.connect(app.quit)
sys.exit(app.exec_())
效果如下:
3. 跟随系统样式
第二步中其风格没有跟随系统样式,在 How to access native styles for qml apps? 找到了解决方法,即用QApplication替代QGuiApplication,代码如下:
# from PyQt5.QtGui import QGuiApplication
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
if __name__ == "__main__":
import sys
# Create an instance of the application
app = QApplication(sys.argv)
# Create QML engine
engine = QQmlApplicationEngine()
# Load the qml file into the engine
engine.load("untitled.qml")
engine.quit.connect(app.quit)
sys.exit(app.exec_())