私のコードに関する私の最後の問題以来、私は新しい問題に出くわしました。残念ながら、これは実際には実装の問題ではなく、「概念」の問題です。
それでは事例をご紹介しましょう。ボタンでいっぱいのグリッドがあり、それらの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
の 2 つの理由で使用される要素があります。
- エステ(特注デザイン、角丸など)
- 各ボタンと onRightclick に MouseArea を追加し、Menu 要素を表示します
したがって、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
}
}
}
各要素を生成する際の効率がよくわからないので、 20 個または 30 個の要素のようなmouseArea
独立したオプションを使用するためのより良い方法があれば教えてください。onRightclick
私の問題は次のとおりです。このページでは、CustomButton が実装されている main.qml としましょう。2 つの fileDialog アイテムがconfOpen
ありfirmwareOpen
ます。ユーザーが右クリックを使用すると、MenuItem がマウスの正確な場所に表示され、必要なオプションを選択できます。次に、 または のいずれかが呼び出されconfOpen
、firmwareOpen
ユーザーは 1 つのファイルを選択できます。
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
そうするために持っています。optionalConf
しかし、どのボタンが FileDialog を呼び出したかを管理できないため、どのボタンのプロパティを更新する必要があるかわかりません。
私は明確であり、読むのにそれほど時間はかからないことを願っていますが、明確かつ正確になりたいと思っていました. 私がやっていることを行うためのより良い方法があれば教えてください。私は常にアドバイスを聞いています:)