Exemplo n.º 1
0
from PyQt5 import QtCore, QtQml, QtWidgets
from mymodel import MyModel, MyItem

osname = os.name.lower()
sysplatform = sys.platform.lower()
windows = os.name.lower() == "nt" and sysplatform.startswith("win")

# PyQt class name, QML URI, major version, minor version, QML type name
QtQml.qmlRegisterType(MyModel, 'MyModel', 1, 0, 'MyModel')
QtQml.qmlRegisterType(MyItem, 'MyItem', 1, 0, 'MyItem')

app = QtWidgets.QApplication(sys.argv)

# Create the QML engine
engine = QtQml.QQmlEngine(app)
engine.quit.connect(app.quit)

# Load the main.qml file and create the toplevel component
component = QtQml.QQmlComponent(engine)
currentFilePath = os.path.dirname(os.path.abspath(__file__))
mainFilepath = os.path.join(currentFilePath, "main.qml")
if windows:
    mainFilepath = mainFilepath.replace('\\', '/')
qmlFile = QtCore.QUrl("file:///" + mainFilepath)
component.loadUrl(qmlFile)
if component.status() != QtQml.QQmlComponent.Ready:
    for error in component.errors():
        print(error.toString())
    sys.exit(-1)