【问题标题】:MouseArea calling a FileDialog : know which element opened the FileDialog (QML)MouseArea 调用 FileDialog :知道哪个元素打开了 FileDialog (QML)
【发布时间】:2021-05-10 20:35:17
【问题描述】:

自从我上一个代码问题以来,我遇到了一个新问题。不幸的是,这并不是真正的实施问题,而更像是一个“概念”问题。

那么让我们来介绍一下这个案例。我有一个充满按钮的网格,然后处理他们的onClicked 事件我有一个ButtonGroup

GridLayout {
id: gl
anchors.fill: parent
...

   CustomButton{
      id: btnMILA1
      text: "PlayBook 1"
      ... //Layout stuff
   }
   CustomButton{
      id: btnMILA2
      text: "PlayBook 1"
      ... //Layout stuff
   }
   CustomButton{
      id: btnMILAN
      text: "PlayBook 1"
      ... //Layout stuff
   }
}

这些是在循环中生成的,所以不用担心,我没有写所有 40 个按钮 ^^ 所以这是我的ButtonGroup

ButtonGroup {
   id: btnGroup
   buttons: gl.children
   onClicked: {       
      ... //Do some stuff
   }
}

您可能已经看到,我有一个 CustomButton 元素,使用它有两个原因:

  • 美学(定制设计、圆角等...)
  • 为每个按钮添加一个 MouseArea 并在右键单击时显示一个菜单元素

所以这是我的 CustomButton 元素代码的简化版本:

import QtQuick 2.15

Button {
    id: button

    property string optionalConf //SEE LATER BELOW, THIS ITEM WILL BE USEFUL

    text: qsTr("Button")
    contentItem: Item{
        Text {
            id: name
            text: button.text
            font: button.font
            color: "#ffffff"
            anchors.verticalCenter: parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
        }
    }

    background: Rectangle{
        color: internal.dynamicColor //Used to deal with Hovered/Pressed/Default states
        radius: 10
    }

    MouseArea {
        id:mouseHovered
        anchors.fill: parent
        acceptedButtons: Qt.RightButton
        onClicked:{
            rightClickMenu.open()
        }
        hoverEnabled: true
    }

    Menu {
        id: rightClickMenu

        MenuItem {
            text: qsTr("Choix du fichier de configuration...")
            shortcut: StandardKey.Open
            onTriggered: confOpen.open()
        }

        MenuItem {
            text: qsTr("Choix du firmware...")
            shortcut: "Ctrl+Shift+O"
            onTriggered: firmwareOpen.open()
        }

        MenuSeparator{}

        MenuItem {
            text: qsTr("Console")
            shortcut: StandardKey.AddTab
            //onTriggered: zoomOut()
            enabled: false
        }
    }
}

我真的不知道为每个元素生成 mouseArea 的效率,所以如果您有更好的方法来为 20 或 30 个元素设置独立的 onRightclick 选项,请告诉我。

我的问题如下。在页面上,假设实现了 CustomButton 的 main.qml,我有两个 fileDialog 项目:一个名为 confOpen,另一个名为 firmwareOpen,正如您在上面的代码中所期望的那样。当用户使用右键单击时,MenuItem 显示在鼠标的确切位置,他可以选择他想要的任何选项。然后调用confOpenfirmwareOpen,用户可以选择一个文件。

FileDialog{
   id: confOpen
   title: "Please choose a conf file"
   folder: shortcuts.desktop
   selectMultiple: false
   nameFilters: ["Conf file (*.conf)"]
   onAccepted: {
      console.log(fileUrl)
      //I'd like to do something like this :
      //ButtonUsedToOpenFileDialog.optionalConf : fileUrl
   }
}

所以这是真正的问题,我想将文件路径存储到我的 CustomButton 的属性中。为了这样做,我有一个property string optionalConf。但是我无法管理哪个按钮调用了 FileDialog,所以我不知道哪个按钮应该更新他的optionalConf 属性。

我希望我已经很清楚了,并且不会花很长时间阅读,但我想清楚和准确。如果您有更好的方法来做我正在做的事情,请告诉我,我一直在听取建议:)

【问题讨论】:

    标签: qt qml qtquick2 fileopendialog mousearea


    【解决方案1】:

    向您的 FileDialog 添加一个名为 openDialog 的函数并将按钮传递给它,如下所示:

    [...]
            MenuItem {
                text: qsTr("Choix du fichier de configuration...")
                shortcut: StandardKey.Open
                onTriggered: confOpen.openDialog(button)
            }
    [...]
    FileDialog {
       id: confOpen
       
       property var button
    
       function openDialog(button_) {
           button = button_;
           open();
       }
    
       onAccepted: {
           button.optionalConf = "UPDATED";
       }
    }
    

    【讨论】:

    • 谢谢大卫,这就是我要找的!我仍然有一个问题,因为在您的示例中我无法将 fileUrl 放入 button.optionalConf 因为我还没有得到它。我必须等待 onAccepted 事件。但是在那里,按钮是未知的,因为它超出了范围。所以它确实已经解决了,但我们越来越近了;)
    • 它确实有效,非常感谢大卫!祝你有美好的一天
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    相关资源
    最近更新 更多