本文介绍了如何在 QML 中设置弹出菜单位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想修复 QML 中弹出菜单的位置.当我点击设置按钮时,我希望弹出菜单显示在固定位置.我做了一天,但不能.我怎样才能在 QML 中做到这一点.另外,我想更改菜单项的大小(宽度和高度).
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).
希望得到您的帮助!
推荐答案
这取决于 QtQuick.Controls
版本.
在 2.0
中,您可以定义大小和位置(甚至更多 - 您必须这样做)
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 }
}
}
在 1.4
(见注释行)你可以尝试 Menu.__popup()
但这个函数是私有的,行为是不可预测的.
In 1.4
(see commented lines) you can try Menu.__popup()
but this function is private and the behavior is unpredictable.
这篇关于如何在 QML 中设置弹出菜单位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!