PyQt/PySide6快速入门 - 4 QML天气小程序(a)

上一篇:PyQt/PySide6快速入门 - 3 QML简介与Qt Creator开发环境 - 简书 (jianshu.com)

本期知识点:

QML项目目录框架,PathView动画,StackView页面切换,信号,transform动画

天气小应用介绍:

主界面:各个城市之间可划动选择,也可以直接点图标选择。点击某个城市开始查询天气。这类操作适合在可穿戴设备上,比如手表、手环。


image.png

子页面:显示天气详情。


image.png

上下导航条:“返回”到主界面,返回上一级

项目目录框架:

/images  === 图片
/js  === js脚本
main.py  === Python主程序 
main.qml === QML主入口
HomePage.qml  === Homepage主界面
NaviButton.qml === 组件:导航返回
WeatherPage.qml === 天气页面
weather_qml.qrc === 资源文件

步骤:

  • 使用Qt Creator,新建一个Qt for Python - Quick应用,选PySide6

  • App主程序 main.py

  • 由于使用了Qt.labs.settings,这里需要添加两行参数:QCoreApplication.setXXX

  • 创建qml engine,引用QML主文件 main.qml

if __name__ == "__main__":
    os.environ["QML_XHR_ALLOW_FILE_READ"] = "1"
    QCoreApplication.setApplicationName("Weather App");
    QCoreApplication.setOrganizationName("QtProject");

    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.load(os.fspath(Path(__file__).resolve().parent / "main.qml"))
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec())
  • QML主入口:main.qml

  • ApplicationWindow 自带header/footer/background属性,方便设计应用框架

  • Settings组件:可以存储用户的选择,也可以当作全局变量来访问,如:settings.cityId

  • header和footer: 放置NavButton,用于导航、返回:onClicked: stackView.pop()

  • StackView:堆叠页面组件,可以让用户进入子页面,以及返回前一页面:push,pop方法

  • 初始页面为:HomePage

  • HomePage有 signal: lauched,在这里可以监听到

ApplicationWindow {
    id: window
    visible: true
    width: 450
    height: 800
    title: qsTr("小天气 App")

    property string page

    Settings {
        id: settings
        property int cityId
    }

    property alias settings: settings // 全局变量

    background: Image {
        source: "images/background-light.png"
    }

    header: NaviButton {
        id: homeButton
        edge: Qt.TopEdge
        enabled: stackView.depth > 1
        imageSource: "images/home.png"
        onClicked: stackView.pop(null)
    }

    footer: NaviButton {
        id: backButton
        edge: Qt.BottomEdge
        enabled: stackView.depth > 1
        imageSource: "images/back.png"
        onClicked: stackView.pop()
    }

    StackView {
        id: stackView
        focus: true
        anchors.fill: parent
        initialItem: HomePage {  // Qt6必须以function方式响应signal
            onLaunched: (_) => stackView.push("WeatherPage.qml")
        }
    }
}

  • 创建主界面UI:HomePage.qml

  • 定义信号:signal launched(int cityId)

  • 使用PathView,动态放置所有可选的model item(城市图标),到一条自定义路径上

  • Path :自定义路径。PathLine是画一条直线,PathArc画圆弧。里面可以用PathAttribute定义每个节点的特殊属性。我画的路径是从中心先往左,然后向上半圆移动到最右边,最后回到中心。

  • Delegate:设置每个子元素的渲染方法。这里用RoundButton 渲染。监听onClicked事件,发送信号,并存储cityId到全局变量中

  • Text组件:显示当前选择城市的名字

  • 可以用鼠标左右滑动,也可以直接点击某个城市图标。然后再点中间,就会进入到子页面(天气详情)

PathView {
    id: circularView

    signal launched(int cityId)

    readonly property int cX: width / 2
    readonly property int cY: height / 2
    readonly property int itemSize: size / 3
    readonly property int size: Math.min(width-80, height)
    readonly property int radius: size / 2.4 //画Path圆的半径,越大图标离得越远

    snapMode: PathView.SnapToItem
    anchors.fill: parent
    model: ListModel {
        ListElement {
            name: qsTr("南京")
            icon: "images/nanjing.png"
            cityId: 1
        }
。。。
        ListElement {
            name: "香港"
            icon: "images/hongkong.png"
            cityId: 7
        }
    }
    delegate: RoundButton {
        width: itemSize
        height: itemSize

        property string name: model.name //必须要设置prop,不然其它Component不能访问currentItem.xxx
        property string cityId: model.cityId

        icon.width: width*.7
        icon.height: width*.7
        icon.source: model.icon
        opacity: PathView.itemOpacity

        background: Rectangle {
            radius: width / 2
            border.width: 3
            border.color: parent.PathView.isCurrentItem ? "#41cd52" : "#aaaaaa"
        }

        onClicked: {
            console.log(`currIndex=${circularView.currentIndex}, index=${index}, id: ${name}`)
            if (PathView.isCurrentItem){
                settings.cityId = cityId
                circularView.launched(cityId) }
            else
                circularView.currentIndex = index
        }
    }

    path: Path {
        startX: cX; startY: cY
        PathAttribute {name: "itemOpacity"; value: 1.0}
        PathLine { x: cX/5; y: cY}
        PathAttribute {name: "itemOpacity"; value: .2}
        PathArc {
            x: cX*1.8
            y: cY
            radiusX: cX/1.2
            radiusY: circularView.radius/1.5
            useLargeArc: true
            direction: PathArc.Clockwise
        }
        PathAttribute {name: "itemOpacity"; value: .2}
        PathLine { x: cX; y: cY }
        PathAttribute {name: "itemOpacity"; value: .1}
      }

    Text {
        id: nameText
        property Item currentItem: circularView.currentItem
        visible: currentItem ? currentItem.PathView.itemOpacity === 1.0 : 0
        text: currentItem ? currentItem.name : ""
    }
}

  • NavButton.qml 子组件

  • 你的主QML文件,会自动调用当前目录下的所有*.qml文件,作为子组件引入,然后可直接使用它,比如NavButton。当然,也可以用import命令来手动引入子模块、js代码等

  • 添加自定义属性imageSource:父组件可以通过它来对子组件的属性赋值,imageSource: "images/xxx.png",上下图标选择对应的.png文件

  • 添加了动画Behavior on y { NumberAnimation { } :当enabled(父组件设置)时,监听y值变化,然后动态自动改变值从初始值到目标值,达到动画显示和隐藏的效果,默认动画时长250ms

AbstractButton {
    id: button

    property int edge: Qt.TopEdge
    property alias imageSource: image.source

    contentItem: Image {
        id: image
        fillMode: Image.Pad
        sourceSize { width: 40; height: 40 } // ### TODO: resize the image
    }

    background: Rectangle {
        height: button.height * 4
        width: height
        radius: width / 2
        anchors.horizontalCenter: button.horizontalCenter
        anchors.top: edge === Qt.BottomEdge ? button.top : undefined
        anchors.bottom: edge === Qt.TopEdge ? button.bottom : undefined

    }

    transform: Translate {
        Behavior on y { NumberAnimation { } }
        y: enabled ? 0 : edge === Qt.TopEdge ? -button.height : button.height
    }
}

下一篇:会讲天气详情页面的设计,包含调用javascript,发送信号给Ptyhon后台,以及本地文件权限管理等。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,373评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,732评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,163评论 0 238
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,700评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,036评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,425评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,737评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,421评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,141评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,398评论 2 243
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,908评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,276评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,907评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,018评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,772评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,448评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,325评论 2 261

推荐阅读更多精彩内容