同时点击两个按钮,使用qml和Qt6打开第二个按钮的动作

问题描述 投票:0回答:1

我有这两个按钮,我想单击这两个按钮来打开第二个按钮的 onClick 操作。我怎样才能得到我想要的结果?也许要使用信号?

Button {
        id: btnLevel1
        x: 1016
        y: 492
        width: 554
        height: 67
        onClicked: classA.newGameWindow()
        text: "May God\nHave Mercy"
        background: Rectangle {
               color: "transparent"
            }

        font.family: "Inria Serif"
        font.pixelSize: 20
    }

    Text {
        id: very_hard_level
        color: "white"
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignTop
        wrapMode: Text.Wrap
        font.weight: Font.Normal
    }



Button {
                    id: btn2
                    x: 58
                    y: 246
                    width: 188
                    height: 34
                    onClicked: classA.newGameWindow()
                    text: qsTr("New Game")

                    background: Rectangle {
                       color: "transparent"
                    }

                    font.family: "Inria Serif"
                    font.pixelSize: 26

                    Text {
                           id: new_Game
                           color: "white"
                           horizontalAlignment: Text.AlignLeft
                           verticalAlignment: Text.AlignTop
                           wrapMode: Text.Wrap
                           font.weight: Font.Normal
                    }

             }

我尝试使用 MouseArea 但没有成功,我不知道该怎么办

qt button qml click qtquick2
1个回答
0
投票

我仍然不完全理解您的用例,因此解决方案可能是错误的。

我使用了按钮组来跟踪正在使用的按钮,并统一

onClicked
信号调用。这里的一个陷阱可能是添加一个不具有可检查的自定义属性
AbstractButton
activated
。当按下按钮时,该按钮的
activated
属性设置为
true
,如果不是该组的所有按钮都是
activated
,则该功能退出,如果所有按钮都是
activated
根的背景
Window
设置为该按钮的黄色
. Also the custom button 
MyButton
includes an indicator
矩形
to show the
激活`状态。

也就是说,根据您的用例,可能会有更好的解决方案来解决您的问题。

import QtQuick
import QtQuick.Controls

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

    ButtonGroup {
        id: buttonGroup
        onClicked: function(button) {
            button.activated = true

            for (let i = 0; i < buttonGroup.buttons.length; ++i) {
                if (!buttonGroup.buttons[i].activated)
                    return
            }

            console.log("All buttons pressed")
            root.color = "yellow"
        }
    }

    component MyButton: Row {
        property alias text: button.text

        spacing: 10

        Button {
            id: button
            property bool activated: false
            ButtonGroup.group: buttonGroup
        }

        Rectangle {
            width: 20
            height: 20
            color: button.activated ? "green" : "red"
            anchors.verticalCenter: parent.verticalCenter
        }
    }

    Column {
        spacing: 10
        anchors.centerIn: parent

        MyButton { text: "Button #1" }
        MyButton { text: "Button #2" }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.