我编写了简单的脚本来检查python3上的QML功能(在我使用c ++之前):
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
import sys
import logging
def run():
logging.basicConfig(level = logging.DEBUG)
logging.info("Create application object")
app = QApplication(sys.argv)
logging.info("Create qml engine/view")
engine = QQmlApplicationEngine()
logging.info("Loading qml file")
engine.load(('./qml/CurrentValues.qml'))
if not engine.rootObjects():
logging.fatal("Engine root objects in empty")
return -1
engine.quit.connect(app.quit)
logging.info("Run application")
return app.exec_()
if __name__ == "__main__":
sys.exit(run())
在“return app.exec_()”行上,脚本冻结并且不显示窗口。脚本不会显示错误,也不会被Ctrl-C停止,只能被kill命令杀死。
我有ubuntu 17.10并安装了软件包:python3-pyqt5,python3-pyqt5.qtquick。我也尝试使用这个软件包的pip版本(PyQt5)并尝试使用PySide2。我使用了QCoreApplication,QGuiApplication,但它没有产生效果。它不适用于任何qml文件。
可能是什么问题?
CurrentValues.qml:
import QtQuick 2.0
import "."
ListView {
orientation: ListView.Horizontal
flickableDirection: Flickable.HorizontalFlick
delegate: CurrentValueViewer {}
model: ListModel {
id: valuesModel
ListElement {
valueName: "Temp"
curValue: "250°"
}
ListElement {
valueName: "CO2"
curValue: "125 PPM"
}
ListElement {
valueName: "O2"
curValue: "18 %"
}
ListElement {
valueName: "N2"
curValue: "15%"
}
ListElement {
valueName: "RH"
curValue: "80 %"
}
}
}
CurrentValueViewer.qml:
import QtQuick 2.0
import QtQuick.Controls 1.4
Rectangle {
id: valueViewer
width: 120
height: 80
color: "#d6d6d6"
radius: 10
Column {
Text {
id: name
text: valueName
}
Text {
id: value
text: "<html><body>" + curValue +"<sup>°C</sup></body></html>"
font.bold: true
font.pointSize: 15
}
Text {
id: moreInfo
text: "And more..."
}
}
}
c ++上的工作版:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "currentmeasurement.h"
#include "currentmeasurementsmodel.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/CurrentValues.qml")));
if (engine.rootObjects().isEmpty())
return -1;
CurrentMeasurementsModel measurementList;
measurementList.append(CurrentMeasurement("Temp", 25, "°C"));
measurementList.append(CurrentMeasurement("CO2", 125, "PPM"));
measurementList.append(CurrentMeasurement("O2", 18, "%"));
measurementList.append(CurrentMeasurement("N2", 75, "%"));
measurementList.append(CurrentMeasurement("RH", 85, "%"));
QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty("measurementModel", &measurementList);
return app.exec();
}
它使用基于QAbstractListModel的类
答案 0 :(得分:0)
QQmlApplicationEngine
只能显示QQuickWindow
,因此无法显示ListView
,因此可能的解决方案是以root身份使用ApplicationWindow
。
import QtQuick 2.0
import QtQuick.Controls 1.4
import "."
ApplicationWindow {
visible: true
width: 640
height: 480
ListView {
anchors.fill: parent
orientation: ListView.Horizontal
flickableDirection: Flickable.HorizontalFlick
delegate: CurrentValueViewer {}
model: ListModel {
id: valuesModel
ListElement {
valueName: "Temp"
curValue: "250°"
}
ListElement {
valueName: "CO2"
curValue: "125 PPM"
}
ListElement {
valueName: "O2"
curValue: "18 %"
}
ListElement {
valueName: "N2"
curValue: "15%"
}
ListElement {
valueName: "RH"
curValue: "80 %"
}
}
}
}