qml控件使用相关

Settings

Settings {
        id: systemSettings
        category: "system"

        //设置-休眠时间(范围:1-5,单位:分钟 )
        property int sleepTime: 4
        property bool wifiEnable: true

        //判断儿童锁(true表示锁定,false表示未锁定)
        property bool childLock:false

        property bool leftCookDialog:true
        property bool rightCookDialog:true
        property bool multistageRemind:true
        property bool multistageDialog:true
        property var wifiPasswdArray

方法定义

function deleteWifiInfo(wifiInfo)
{
	return 1
}

StackView

StackView {
        id: stackView
        initialItem: pageHome
        anchors.fill: parent
        enabled:true
}

定时器

Timer{
        id:timer_window
        repeat: false
        running: false
        interval: systemSettings.sleepTime*60000
        triggeredOnStart: false
        onTriggered: {

        }
}

Connections信号与槽函数连接

Connections { // 将目标对象信号与槽函数进行连接
        target: QmlDevState
        onLocalConnectedChanged:{
            console.log("page home onLocalConnectedChanged",value)
            if(value > 0)
            {
                closeLoaderError()
                permitSteamStartStatus(0)
            }
            else
            {
                showLoaderFault("通讯板故障!","请拨打售后电话<font color='#00E6B6'>400-888-8490</font><br/>咨询售后人员",false)
            }
        }
}

自定义CheckBox

import QtQuick 2.7
import QtQuick.Controls 2.2

CheckBox {
    id:control
    width:200
    height:40
    checked: false
    text: qsTr("下次不再提醒")
    leftPadding:10
    contentItem: Text {
        width:parent.width
        text: parent.text
        font.pixelSize: 30
        opacity: 1.0
        color: "#FFF"
        verticalAlignment: Text.AlignVCenter
        leftPadding: control.indicator.width + control.spacing
    }
    indicator: Rectangle {
        implicitWidth: 40
        implicitHeight: 40
        x: control.leftPadding
        y: parent.height / 2 - height / 2
        //                radius: 3
        color:"transparent"
        //                border.color: control.down ? "#17a81a" : "#21be2b"

        //                Rectangle {
        //                    width: 14
        //                    height: 14
        //                    x: 6
        //                    y: 6
        //                    radius: 2
        //                    color: control.down ? "#17a81a" : "#21be2b"
        //                    visible: control.checked
        //                }
        Image {

            anchors.centerIn: parent
            source: control.checked ?"qrc:/x50/main/iocn_kuang_x.png":"qrc:/x50/main/iocn_kuang_w.png"
            //                    visible: control.checked
        }
    }
}

自定义BusyIndicator

BusyIndicator {
        id: control
//        anchors.fill: parent
        implicitWidth: 40
        implicitHeight: 40
        background:Rectangle{
            color:"transparent"
        }
        contentItem: Item {
            anchors.centerIn: parent
            implicitWidth: parent.width
            implicitHeight: parent.width

            Item {
                id: item
                anchors.centerIn: parent
                x: parent.width / 2
                y: parent.height / 2
                width: parent.width
                height: parent.height
                opacity: control.running ? 1 : 0

                Behavior on opacity {
                    OpacityAnimator {
                        duration: 300
                    }
                }

                RotationAnimator {
                    target: item
                    running: control.visible && control.running
                    from: 0
                    to: 360
                    loops: Animation.Infinite
                    duration: 7000
                }

                Repeater {
                    id: repeater
                    model: 8

                    Rectangle {
                        x: item.width / 2 - width / 2
                        y: item.height / 2 - height / 2
                        implicitWidth: 6
                        implicitHeight: 6
                        radius: 5
                        color: index<3?"darkgray":"#fff"
                        transform: [
                            Translate {
                                y: -Math.min(item.width, item.height) * 0.5
                            },
                            Rotation {
                                angle: index / repeater.count * 360
                                origin.x: 3
                                origin.y: 3
                            }
                        ]
                    }
                }
            }
        }
}

RotationAnimator旋转动画

    RotationAnimator {
        target: busyImg
        running: busyImg.visible
        from: 0
        to: 360
        loops: Animation.Infinite
        duration: 5000
    }

波浪进度条

import QtQuick 2.0
import QtQuick.Controls 2.2

Item {
    Canvas{
        property real percent: slider.value
        property real lineWidth:5
        property real r: canvas.width/2-lineWidth
        id: canvas
        height: 300
        width: 300
        anchors.centerIn: parent
        onPaint: {
            var ctx = getContext("2d");
            //                ctx.reset()
            ctx.save()
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            ctx.translate(150,150)
            ctx.lineWidth = lineWidth
            //显示外圈
            ctx.beginPath();
            ctx.moveTo(0,0)
            ctx.strokeStyle = 'red';
            ctx.fillStyle = '#00ffff'
            ctx.arc(0, 0, r, 0, 2*Math.PI);
            ctx.stroke();
            ctx.fill();
            ctx.clip()

            //显示sin曲线
            var dy = r-2*r*percent/100
            ctx.beginPath();
            ctx.moveTo(-r,dy)
            for(var x = 0; x < 2*r; x += 1){
                var y = -Math.sin(x*0.021);
                ctx.lineTo(-r+x, dy + y*6);
            }
            //显示波浪
            ctx.lineTo(r, r);
            ctx.lineTo(-r, r);
            ctx.closePath()
            ctx.fillStyle = '#1c86d1';
            ctx.fill();

            //显示百分数
            ctx.font = "30px sans-serif";
            ctx.textAlign = 'center';
            ctx.fillStyle = "blue";
            ctx.fillText(percent + '%', 0, 0);
            ctx.restore();
        }
    }
}

页面间切换传递参数

使用stackView切换页面时,好像必须使用state作为key

stackView.push(pageCookDetails, {"state":args})

控件间存在覆盖时,点击事件穿透

必须mouse.accepted = false

    MouseArea{
        anchors.fill: parent
        hoverEnabled:true
        propagateComposedEvents: true

        onPressed: {
                mouse.accepted = false
        }
    }
### QML控件使用方法与示例 QML是一种声明式的语言,用于构建用户界面。通过CMake配置以及`QtQuickControls2`模块的支持,可以轻松实现Material风格的应用程序。 以下是基于提供的引用内容[^1],展示如何在项目中启用并自定义Material风格,并提供一个完整的QML控件使用示例: #### CMake 配置 为了支持Material风格,在项目的CMake文件中需要引入`QtQuickControls2`模块: ```cmake find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS QuickControls2) target_link_libraries(your_target_name PRIVATE Qt${QT_VERSION_MAJOR}::QuickControls2) ``` #### 设置 Material 风格 在`main.cpp`中初始化应用程序时,可以通过调用`QQuickStyle::setStyle("Material")`来设置全局样式: ```cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQuickStyle> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); // 设置Material风格 QQuickStyle::setStyle("Material"); QQmlApplicationEngine engine; const QUrl url(u"qrc:/main.qml"_qs); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); } ``` #### QML 控件示例 下面是一个简单的QML代码片段,展示了如何创建带有按钮和滑动条的窗口,并应用Material主题: ```qml import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Window 2.15 Window { visible: true width: 640 height: 480 title: qsTr("QML Material Style Example") Column { anchors.centerIn: parent spacing: 20 Button { text: "Click Me" onClicked: console.log("Button clicked!") } Slider { from: 0 to: 100 value: 50 onValueChanged: console.log("Slider value changed:", value) } } } ``` 此代码会生成一个具有Material设计样式的窗口,其中包含一个按钮和一个滑动条。当点击按钮或调整滑动条时,控制台将显示相应的事件消息。 --- ### 自定义 Material 主题颜色 如果希望进一步定制Material风格的颜色方案,可以在QML中导入`QtQuick.Controls.Material`模块,并修改其属性: ```qml import QtQuick.Controls.Material 2.15 // 修改整体主题颜色 Material.theme: Material.Dark Material.primaryColor: Material.BlueGray ``` 上述代码设置了深色模式的主题背景,并指定了蓝色灰作为主要配色。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小分享

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值