弹出窗口自动重新打开

时间:2020-07-05 16:06:34

标签: qt popup qml toggle

要解决here中提到的z定位问题,我想用Calendar包装Popup。这是我在DatePicker.qml中所做的更改:

Item {
    id: root
    ...
    RowLayout{
        ...
        LineField{ ... }
        Toggle{
            id: toggle
            uncheckedPath: Con.uncheckedcalendarIcon
            checkedPath: Con.checkedCalendarIcon
            onClicked: {
                if(checked) popup.open()
                else popup.close()
            }
        }
    }
    Popup{
        id: popup
        ...
        contentItem: Calendar {
            ...
            style: CalendarStyle{
                //borderVisible: false
                gridVisible: false
                navigationBar: RowLayout{ ... }
                dayOfWeekDelegate: ColumnLayout{ ... }
                dayDelegate: Rectangle{ ... }
        }
        background: Card {}
        //onClosed: if(toggle.checked) toggle.checked = false
    }
}

在弹出窗口中没有onClosed时,其行为如下:

enter image description here

当我单击Toggle打开和关闭Popup时,它可以正常工作。如果我单击离开Toggle的位置,弹出窗口将关闭,但切换状态仍为checked。因此,要将其设置为false,我使用了onClosed中的Popup。这是onClosed中这段代码的作用:

enter image description here

现在,它可以按我期望的操作,当我单击Toggle时,弹出窗口关闭并将状态更改切换为false。现在,如果我第二次单击Toggle,它将首先关闭弹出窗口,然后再次重新打开。不确定我的Toggle.qml中是否有问题。这是:

import QtQuick 2.9
import QtQuick.Controls 2.5
import QtQuick.Shapes 1.12

Item {
    property string checkedPath
    property string uncheckedPath
    property bool checked: false
    signal clicked()

    width: 24
    height: width
    Shape{
        ShapePath{
            id: path
            fillColor: "black"
            PathSvg{
                id: svg
                path: uncheckedPath
            }
        }
    }
    MouseArea{
        anchors.fill: parent
        onClicked: {
            checked = !checked
            parent.clicked()
        }
        hoverEnabled: true
        onHoveredChanged: path.fillColor = containsMouse? "blue" : checked? "green" : "black"
    }
    onCheckedChanged: {
        if(checked){
            svg.path = checkedPath
            path.fillColor = "green"
        }
        else{
            svg.path = uncheckedPath
            path.fillColor = "black"
        }
    }
}

我在弹出式onClosed和切换式onClicked中为Combobox使用了相同的代码,但是这里没有这个问题。

编辑

Here是项目链接。

0 个答案:

没有答案