qml中的两个互斥按钮怎么实现?

FlyWM_ 2016-10-26 11:20:59
如图,默认状态运行是蓝色,停止是灰色,点击运行,运行按钮编程灰色,停止按钮变成蓝色,点击停止,停止按钮编程灰色,运行按钮变成蓝色
...全文
931 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yhgaven 2016-11-09
  • 打赏
  • 举报
回复
ExclusiveGroup
马小悦悦 2016-10-27
  • 打赏
  • 举报
回复
import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.0

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

    Rectangle
    {
        id: run
        width: 100; height: 50
        Text {
            text: "运行"
            anchors.centerIn: parent
            font.pixelSize: 32
        }
        anchors.centerIn: parent
        state: "stopState"
        states: [
            State{
                name: "runState"
                PropertyChanges{
                    target: run; color:"lightgray"
                }
                PropertyChanges{
                    target: mouseArea1; enabled: false;
                }
            },
            State{
                name: "stopState"
                PropertyChanges{
                    target: run; color: "lightblue"
                }
                PropertyChanges{
                    target: mouseArea1; enabled: true;
                }
            }
        ]
        MouseArea
        {
            id: mouseArea1
            anchors.fill: parent
            onClicked: {
                if(run.state == "runState")
                    {
                        run.state = "stopState"
                        stop.state = "stopState"
                    }
                else
                    {
                        run.state = "runState"
                        stop.state = "runState"
                    }
            }
        }
    }

    Rectangle
    {
        id: stop
        width: 100; height: 50
        Text
        {
            anchors.centerIn: parent
            text: "暂停"
            font.pixelSize: 32
        }
        anchors.top: run.bottom
        anchors.left: run.left
        anchors.topMargin: 10
        state: "stopState"
        states: [
            State{
                name: "runState"
                PropertyChanges{
                    target: stop; color:"lightblue"
                }
                PropertyChanges{
                    target: mouseArea2; enabled: true;
                }
            },
            State{
                name: "stopState"
                PropertyChanges{
                    target: stop; color: "lightgray"
                }
                PropertyChanges{
                    target: mouseArea2; enabled: false;
                }
            }
        ]
        MouseArea
        {
            id: mouseArea2
            anchors.fill: parent
            onClicked: {
                if(stop.state == "runState")
                {stop.state = "stopState"
                 run.state = "stopState"}
                else
                {stop.state = "runState"
                 run.state = "runState"}
            }
        }
    }
}
东莞某某某 2016-10-27
  • 打赏
  • 举报
回复
用楼上的,使用state数组

16,233

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧