Location>code7788 >text

Qml in those pits (seven) --- ComboBox embedded in the popup, scrolling content over its visible area will not close the ComboBox popup window

Popularity:395 ℃/2024-11-11 19:09:55

[Writing in front]

Recently, I found a strange bug while writing a message submission (form) window:

The code for it is as follows:

import QtQuick 2.15
import  2.15
import  2.15

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

    Button{
        text: "open"
        onClicked: ();
    }

    Popup {
        id: popup
        width: 400
        height: 200
        : parent
        clip: true
        closePolicy: 
        background: Rectangle { color: "#80800000" }
        contentItem: Flickable {
            id: flickable
            clip: true
            topMargin: 10
            contentWidth: implicitWidth
            contentHeight: 500
            : ScrollBar { width: 14 }
            /*onMovementStarted: {
                for (let key in ) {
                    let item = [key];
                    if ( === "__ComboBox__")
                        ();
                }
            }*/

            ComboBox {
                width: 160
                height: 40
                objectName: "__ComboBox__"
                model: ["aaaaaa", "bbbbbb", "cccccc", "dddddd"]
            }
        }
    }
}

It can be seen that whenComboBox embeddingPopup When tapping on theComboBoxIf the content is not visible, then scrolling the content beyond its visible area does not close it.ComboBox pop-up window and will exceed itsParent Popup Scope.


[Text Begins]

In fact, it is present in almost all versions of Qt.( Qt5 ~ Qt6 ) The main reason for this bug is that the popup window is not able to crop the internal nested popup window, because this popup window( Popup ) Not really a window( Window )

I have reported this bug to the official:/browse/QTBUG-130960?filter=-2

However, until the official fix comes out, the less altered fix I implemented is:

  • Qt5 in for:
Flickable {
    ...
    onMovementStarted: {
        for (let key in ) {
            let item = [key];
            if ( === "__ComboBox__")
                ();
        }
    }

    ComboBox {
        ...
        objectName: "__ComboBox__"
    }
}
  • Qt6 in for:
Flickable {
    ...
    onMovementStarted: {
        for (let item of ) {
            if ( === "__ComboBox__")
                ();
        }
    }

    ComboBox {
        ...
        objectName: "__ComboBox__"
    }
}

It only needs to be used when the view is changed due to user interactions or generatedflick() And when you start moving, turn off theComboBox The pop-up window is sufficient.

The effect of the fix is as follows:

image


[Conclusion]

Finally, it is important to note that the examples in this article are not the only ones with this bug; all code like the one below can have it.

Popup {
    Popup {
        ...
    }
}

And the restoration ideas are broadly similar.