qml显示Toast

在使用qml编程的过程中,有时候执行一个任务结束后需要有一个结果提示,而且这个提示等待几秒后自动消失,这个就是一个Toast提示,本文介绍的就是使用qml显示Toast效果,具体的示例代码如下:

//Toast.qml
import QtQuick 2.12

Rectangle{
    id: toast

    property real time: defaultTime
    readonly property real defaultTime: 3000
    readonly property real fadeTime: 300

    property real margin: 10
    property bool selfDestroying: false 

    width: 98
    height: 98
    radius: 4
    opacity: 0
    color: "#000000"

    anchors.horizontalCenter: parent.horizontalCenter

    function show(text, duration){
        theText.text = text;
        if(typeof duration !== "undefined"){
            if(duration >= 2*fadeTime)
                time = duration;
            else
                time = 2*fadeTime;
        }
        else
            time = defaultTime;
        anim.start();
    }

    Image {
        id: imgHeader
        anchors.top: parent.top
        anchors.topMargin: 13
        anchors.horizontalCenter: parent.horizontalCenter
        sourceSize.width: 50
        sourceSize.height: 50
        source: "qrc:/image/notice_50.svg"
    }


    Text{
        id: theText
        text: ""
        horizontalAlignment: Text.AlignHCenter
        x: 28
        y: 68
        font.pixelSize: 14
        color: "#ffffff"
    }

    SequentialAnimation on opacity {
        id: anim

        running: false

        NumberAnimation{
            to: 0.9
            duration: fadeTime
        }
        PauseAnimation{
            duration: time - 2*fadeTime
        }
        NumberAnimation{
            to: 0
            duration: fadeTime
        }

        onRunningChanged:{
            if(!running && selfDestroying)
                toast.destroy();
        }
    }
}
//ToastManager.qml
import QtQuick 2.12

Column{

    function show(text, duration) {
        var toast = toastComponent.createObject(toastManager);
        toast.selfDestroying = true;
        toast.show(text, duration);
    }

    id: toastManager

    z: Infinity
    spacing: 5
    anchors.centerIn: parent

    property var toastComponent

    Component.onCompleted: toastComponent = Qt.createComponent("Toast.qml")
}
//main.qml
import QtQuick 2.12
import QtQuick.Window 2.12


Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ToastManager{ id: toast }
    Rectangle {
        id: rect1
        width: 100;
        height: 50
        color: "red"
        anchors.top: parent.top
        anchors.left: parent.left
        MouseArea {
            anchors.fill: parent
            onClicked: {
                toast.show("已开启")
            }
        }
    }

    Rectangle {
        id: rect2
        width: 100;
        height: 50
        color: "blue"
        anchors.top: parent.top
        anchors.right: parent.right
        MouseArea {
            anchors.fill: parent
            onClicked: {
                toast.show("显示蓝色", 5000)
            }
        }
    }


}

运行结果:

 

参考:通过QML在Android中制作Toast-Java 学习之路

QML界面中显示外部程序,可以通过将外部程序加载到QWidget框架中,然后再将QWidget窗口加载到QML界面中实现。具体步骤如下: 1. 创建一个QWidget窗口,并将外部程序加载到该窗口中。 2. 将QWidget窗口加载到QML界面中。 下面是一个示例代码: ```cpp // main.cpp #include <QApplication> #include <QQmlApplicationEngine> #include <QQuickWidget> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建一个QWidget窗口 QQuickWidget widget; widget.setSource(QUrl("qrc:/qml/ExternalProgram.qml")); // 加载外部程序的QML文件 // 创建一个QQmlApplicationEngine QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/qml/Main.qml"))); // 加载主界面的QML文件 // 将QWidget窗口加载到QML界面中 QObject *rootObject = engine.rootObjects().first(); QQuickItem *qmlItem = qobject_cast<QQuickItem*>(rootObject); qmlItem->setProperty("externalProgram", QVariant::fromValue(widget.rootObject())); return app.exec(); } ``` ```qml // Main.qml import QtQuick 2.0 Item { width: 800 height: 600 // 外部程序的容器 Item { id: externalProgramContainer anchors.fill: parent } // 加载外部程序的QML文件 Component.onCompleted: { externalProgramContainer.children = [externalProgram]; } } ``` ```qml // ExternalProgram.qml import QtQuick 2.0 Rectangle { width: 400 height: 300 color: "lightblue" // 外部程序的内容 Text { text: "External Program" anchors.centerIn: parent font.pixelSize: 24 } } ``` 通过以上代码,我们创建了一个QWidget窗口,并将外部程序的QML文件加载到该窗口中。然后,我们将该QWidget窗口加载到主界面的QML文件中的一个容器中,从而在QML界面中显示外部程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值