Home > Net >  Qml menu popup latency
Qml menu popup latency

Time:10-13

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 Creator version is 5.15,My system is win10.

 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"
                                    onTriggered: {
                                    }
                                }
                                MenuItem { text: "save " }
                                MenuItem { text: "else..." }
                            }
                        }

this is my screenshot enter image description here

CodePudding user response:

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)
  • Related