How can set pop-up menu position in QML

北城以北 提交于 2021-01-03 07:10:16

问题


I want to fix the position of pop-up menu in QML. When I click on a setting button,I want the pop-up menu will display at the fixed position. I did it by a day but can't. How can I do it in QML. Also, I want to change the size of menu item(width and Height).

Hope your help!


回答1:


That depends on QtQuick.Controls version.

In 2.0 you can define size and position(and even more - you must do)

import QtQuick 2.7
import QtQuick.Controls 2.0
//import QtQuick.Controls 1.4
import QtQuick.Window 2.0

Window
{
    id: window
    width: 500
    height: 500
    visible: true

    MouseArea {
        anchors.fill: parent
        onClicked: {
            menu.x = (window.width - menu.width) / 2
            menu.y = (window.height - menu.height) / 2
            //menu.__popup(Qt.rect(200,200,100,100),0,0);
            menu.open();
        }
    }

    Menu {
        id: menu
        MenuItem { text: "item1" }
        MenuItem { text: "item2"; }
        MenuItem { text: "item3"; height: 100 }
    }
}

In 1.4 (see commented lines) you can try Menu.__popup() but this function is private and the behavior is unpredictable.



来源:https://stackoverflow.com/questions/38580053/how-can-set-pop-up-menu-position-in-qml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!