Python3.7/PySide2/Qt5.13でQMLをはじめました。
とりあえずQMLを読み込んで動かせるためのエントリスクリプトはこんな感じ。
main.py
import sys
import os
import pathlib
from PySide2.QtGui import QGuiApplication, QFont
from PySide2.QtQml import QQmlApplicationEngine
def main() -> int:
appDir = pathlib.Path(__file__).parent
app = QGuiApplication(sys.argv)
app.setFont(QFont('メイリオ', 10))
engine = QQmlApplicationEngine(os.fspath(appDir / 'view.qml'))
if not engine.rootObjects():
sys.exit(-1)
return app.exec_()
if '__main__' == __name__:
sys.exit(main())
最新 QtQuick.Controls の 2.13 で普通に動かすとこんな感じになる。
view.qml
import QtQuick 2.13
import QtQuick.Controls 2.13 // 新しいスタイルの 2.x
ApplicationWindow {
title: "六歌仙"
width: 400
height: 300
visible: true
Column {
anchors.fill: parent
Repeater {
model: ["僧正遍昭", "在原業平", "文屋康秀", "喜撰法師", "小野小町", "大友黒主"]
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: modelData
onClicked: {
this.anchors.horizontalCenter = undefined
this.parent = null
}
}
}
move: Transition {
NumberAnimation {
properties: "y"
}
}
}
}
どうにもこのスタイルが好みじゃないのでlegacyなwidgetを使いたい。
調べたところ QtQuick.Controls を 1.x を import すればいいっぽい。
view.qml
import QtQuick 2.13
// import QtQuick.Controls 2.13 // 新しいスタイルの 2.x
import QtQuick.Controls 1.4 // legacyスタイルな 1.x
ApplicationWindow {
title: "六歌仙"
width: 400
height: 300
visible: true
Column {
anchors.fill: parent
Repeater {
model: ["僧正遍昭", "在原業平", "文屋康秀", "喜撰法師", "小野小町", "大友黒主"]
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: modelData
onClicked: {
this.anchors.horizontalCenter = undefined
this.parent = null
}
}
}
move: Transition {
NumberAnimation {
properties: "y"
}
}
}
}
そうそう。これこれ。