Search code examples
qtmenuqmlqt-creator

Qml menu popup latency


When my mouse event happened,the menu can popup,but not immediately,it seems a little latency. this is my code, is anything wrong?

My Qt version is 5.15, my system is Windows 10.

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.12

Window {
    visible: true
    width: 450
    height: 350
    title: qsTr("ListView")
    property bool refreshFlag: false

    Rectangle {
        id: rightview
        width: 60
        height: 300
        x: 100
        color: "#EEEEEE"

        MouseArea {
            anchors.fill: parent
            acceptedButtons: Qt.RightButton | Qt.LeftButton
            onClicked: {
                console.log(width)
                if (mouse.button === Qt.RightButton)
                    contextMenus.popup()
            }

            Menu {
                id: contextMenus
                MenuItem { text: "open" }
                MenuItem { text: "save " }
                MenuItem { text: "else..." }
            }
        }

        Text {
            font.pointSize: 12
            text: "content"
        }
    }
}

this is my screenshot

enter image description here


Solution

  • A couple of issues with your program snippet:

    1. Don't mix QtQuick.Controls 1.x with QtQuick.Controls 2.x
    2. Recommend you update all your references to versions to 2.15
    3. Do not declare Menu inside MouseArea, it doesn't make sense
    4. The MouseArea can be optimized to only accept the RightButton
    5. Declare the Menu at the "top level"

    Here's a cleanup of your code:

    import QtQuick 2.15
    import QtQuick.Controls 2.15
    Page {
        anchors.fill: parent
        Rectangle {
            x: 100
            width: 60
            height: 300
            color: "#EEEEEE"
            Text {
                font.pointSize: 12
                text: "content"
            }
            MouseArea {
                anchors.fill: parent
                acceptedButtons: Qt.RightButton
                onClicked: contextMenus.popup()
            }
        }
        Menu {
            id: contextMenus
            MenuItem { text: "open" }
            MenuItem { text: "save " }
            MenuItem { text: "else..." }
        }
    }
    

    You can Try it Online!

    There appears to be no performance issue when I run the above snippet using qmlonline. I don't think the code is an issue. I think we need to get an understanding of:

    • Your version of Qt
    • Your platform (i.e. OS, hardware, etc)