【问题标题】:QML reset dialog with tabview带有标签视图的 QML 重置对话框
【发布时间】:2017-05-07 21:16:29
【问题描述】:

我试图在 QML 中实现一个选项卡式 Dialog 以将其重置为初始值。

由于选项卡是动态实例化的,因此任何直接的方法似乎都不起作用。父Dialog 不能引用内部ComboboxCombobox 不能引用外部Dialog。如何实现?

import QtQuick 2.3
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.1

Dialog {
    id: dlg
    title: "Settings"
    visible: true
    standardButtons: StandardButton.Apply | StandardButton.Reset
    property string val: ""
    onApply: console.log(val)
    onReset: {
        // RESET COMBOBOX TO DEFAULT
    }
    TabView {
        id: tabView
        anchors.fill: parent
        Tab {
            title: "ValueTab"
            id: tabVal
            GridLayout {
                id: gridVal
                anchors.fill: parent
                GroupBox {
                    title: qsTr("Choose value")
                    id: gb
                    Layout.fillWidth: true
                    ColumnLayout {
                        anchors.fill: parent
                        id: cl
                        ComboBox {
                            id: valueChooser
                            editable: false
                            model: ListModel {
                                id: listModel
                                ListElement { text: "One" }
                                ListElement { text: "Two" }
                                ListElement { text: "Three" }
                            }
                            Layout.fillWidth: true
                            onCurrentTextChanged : val = currentText
                        }
                    }
                }
            }
        }
    }
}

【问题讨论】:

  • 为什么Combobox 不能引用外部对话框?

标签: qt dialog qml qtquick2 qtquickcontrols


【解决方案1】:

我很不确定,如果我的问题如你所说,你不能在Combobox 中引用Dialog。我看不出原因。

假设您的示例确实包含您的问题,并且您要做的就是在按下重置按钮后重置值(并且您知道原始值),这就是我将如何解决它。
使用Connections-type 从Combobox 内连接到Dialogreset()

import QtQuick 2.3
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.1

Dialog {
    id: dlg
    title: "Settings"
    visible: true
    standardButtons: StandardButton.Apply | StandardButton.Reset
    property string val: ""
    onApply: console.log(val)
    onReset: {
        // **DONT** RESET COMBOBOX TO DEFAULT **HERE**
    }
    TabView {
        id: tabView
        anchors.fill: parent
        Tab {
            title: "ValueTab"
            id: tabVal
            GridLayout {
                id: gridVal
                anchors.fill: parent
                GroupBox {
                    title: qsTr("Choose value")
                    id: gb
                    Layout.fillWidth: true
                    ColumnLayout {
                        anchors.fill: parent
                        id: cl
                        ComboBox {
                            id: valueChooser
                            editable: false
                            model: ListModel {
                                id: listModel
                                ListElement { text: "One" }
                                ListElement { text: "Two" }
                                ListElement { text: "Three" }
                            }
                            Layout.fillWidth: true
                            onCurrentTextChanged : val = currentText

                            /// *** INTERESTING PART HERE! ***
                            Connections {
                                target: dlg
                                onReset: {
                                    // RESET COMBOBOX TO DEFAULT **HERE** INSTEAD
                                    valueChooser.currentIndex = 0
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2020-03-14
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2022-06-28
    • 2012-03-11
    相关资源
    最近更新 更多