Qt 对话框按钮框按钮显示为堆叠
我正在尝试自定义自定义对话框按钮上的文本 为此,我使用了一个DialogButtonBox作为对话框的页脚,以及一些加载程序,这些加载程序将加载用户选择的适当按钮 然而,我看到的是,对于一些弹出窗口,按钮堆叠在最左边,您无法单击它们。我用一个小例子再现了它: main.qmlQt 对话框按钮框按钮显示为堆叠,qt,popup,qml,Qt,Popup,Qml,我正在尝试自定义自定义对话框按钮上的文本 为此,我使用了一个DialogButtonBox作为对话框的页脚,以及一些加载程序,这些加载程序将加载用户选择的适当按钮 然而,我看到的是,对于一些弹出窗口,按钮堆叠在最左边,您无法单击它们。我用一个小例子再现了它: main.qml import QtQuick 2.12 import QtQuick.Window 2.2 import QtQuick.Controls 2.4 Window { visible: true width
import QtQuick 2.12
import QtQuick.Window 2.2
import QtQuick.Controls 2.4
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Row{
anchors.centerIn: parent
Button{
property var popup: SimDialog { }
onClicked:
{
popup.open()
}
text: "Open"
}
}
}
SimDialog.qml
import QtQuick 2.12
import QtQuick.Controls 2.12
Dialog {
// ID
id: popupRoot
width: 500
height: 200
topMargin: 20
bottomMargin: 10
leftMargin: 20
rightMargin: 10
// Centered position
x: parent ? parent.x + (parent.width / 2) - (width / 2) : 0// align horizontally centered
y: parent ? parent.y + (parent.height / 2) - (height / 2) : 0// align vertically centered
modal: true
focus: true
clip: true
property int buttons: Dialog.Close | Dialog.Ignore
title: "Test"
Button
{
anchors.centerIn: parent
text: "HELLO"
}
// personalized dialog box buttons so we can translate the button text.
footer: DialogButtonBox {
id: dialogBox
alignment: Qt.AlignRight
Loader
{
active: buttons & Dialog.Help
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Help")
DialogButtonBox.buttonRole: DialogButtonBox.HelpRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Discard
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Continue without saving")
DialogButtonBox.buttonRole: DialogButtonBox.DestructiveRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Save
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Save")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Ignore
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Ignore")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Apply
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Apply")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Ok
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Ok")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Close
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Close")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
flat: true
}
}
Loader
{
active: buttons & Dialog.Cancel
sourceComponent:
Button {
parent: dialogBox
text: qsTr("Cancel")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
flat: true
}
}
}
}
请注意,在我的实际应用程序中,我有几个这样的弹出窗口,其中只有一些具有这种行为。我也没能找出那些不起作用的东西之间的共同点,所以我真的迷路了
这是我这边的情况
你并不真的需要装载机。您可以只使用按钮中的visible属性。 这对我来说就像预期的那样:
// personalized dialog box buttons so we can translate the button text.
footer: DialogButtonBox {
id: dialogBox
Button {
text: qsTr("Help")
DialogButtonBox.buttonRole: DialogButtonBox.HelpRole
flat: true
visible: true //false
}
Button {
text: qsTr("Continue without saving")
DialogButtonBox.buttonRole: DialogButtonBox.DestructiveRole
flat: true
}
Button {
text: qsTr("Save")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
flat: true
}
Button {
text: qsTr("Ignore")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
flat: true
}
Button {
text: qsTr("Apply")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
flat: true
}
Button {
text: qsTr("Ok")
DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
flat: true
}
Button {
text: qsTr("Close")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
flat: true
}
Button {
text: qsTr("Cancel")
DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
flat: true
}
}
我至少看到了大量不必要的组件。您只能放置一个加载程序和一组组件,因此可以根据
按钮
指定合适的组件。您还可以将sourceComponent:Component{Button{}}
替换为sourceComponent:Button{}
,因为按钮
已经是一个组件。至于我,问题是装载机并没有尺寸。有关更多信息,请参阅。该链接显示如果未为加载程序指定显式大小,则加载程序将在加载组件后自动调整为加载项的大小。
这正是我想要的。按钮具有基于其文本的隐式大小。给按钮或加载器一个显式的大小没有任何作用。至于更改为sourceComponent:Button{}
,谢谢,我已经这样做了(并编辑了我的问题),我不一定理解分配合适的组件是什么意思,因为我事先不知道每个对话框将显示哪些按钮。这确实有效,但按钮没有按照我的要求向右对齐。根据显示的是哪一个,它们处于各自的位置,这看起来很尴尬。添加属性alignment:Qt.AlignRight
没有任何作用,我知道我不需要加载程序,但我的应用程序将有几个这样的弹出窗口,我不想实例化额外的可视项,因为启动速度已经相当慢。看起来Qt.AlignRight只有在使用标准按钮时才能正常工作:DialogButtonBox.Ok | DialogButtonBox.Cancel,但在定义自定义按钮时却不能。我猜这是一只虫子!