LoginSignup
0
0

More than 3 years have passed since last update.

QMLでlegacyなwidgetを使いたい

Posted at

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"
            }
        }
    }
}

2019-12-04_21h28_54.png

どうにもこのスタイルが好みじゃないので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"
            }
        }
    }
}

2019-12-04_21h29_11.png

そうそう。これこれ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0