2017-07-12 28 views
1

我的所有對話框都顯示在屏幕的左上角,而不是中心。如何在QtQuick Controls 2屏幕中居中對話框?

讓對話框自動更正的最佳方法是什麼?

enter image description here

import QtQuick 2.7 
import QtQuick.Controls 2.2 

ApplicationWindow { 
    id: mainWindow 

    visible: true 
    width: 640 
    height: 480 
    title: qsTr("Hello World") 

    Component.onCompleted: { 
     showMessageBox('Hey this actually works!'); 
    } 

    function showMessageBox(message) { 
     var component = Qt.createComponent("MessageDialog.qml") 
     if(component.status == Component.Ready) { 
      var dialog = component.createObject(mainWindow) 

      dialog.title = qsTr("Information") 
      dialog.text = message 

      dialog.open() 
     } else 
      console.error(component.errorString()) 
    } 
} 

用非常簡單的MessageDialog.qml

import QtQuick 2.7 
import QtQuick.Controls 2.2 

Dialog { 
    standardButtons: DialogButtonBox.Ok 

    property alias text : textContainer.text 

    Text { 
     id: textContainer 

     anchors.fill: parent 

     horizontalAlignment: Qt.AlignLeft 
     verticalAlignment: Qt.AlignTop 
    } 
} 

回答

3

文檔提示,該DialogPopup具有x/y座標 - 的後代。

我認爲這將是一個很好的開始來定位它。

要將無濟於事:

  • parent.width - 這應該是你的窗口
  • width的寬度 - 這應該是你的Dialog小號寬度
  • parent.height
  • height

計算正確的職位,你應該沒問題。

有了這個,你可以創建一個新基類CenteredDialog.qml

Dialog { 
    x: (parent.width - width)/2 
    y: (parent.height - height)/2 
} 

然後用CenteredDialog代替Dialog所有的時間。

此外,動態實例你可以聲明文件中的Component,只有採用component.createObject(parentObject, { property1Name : property1Value, property2Name : property2Value ... })語法設定在實例屬性。

+0

好的,我改爲'QtQuick 2.7'。我正在尋找一種解決方案,我不必關心寬度和高度。使用舊的'QtQuick.Dialogs 1.2'會不好嗎? –

+0

這取決於你的用例。對我來說問題是,'Dialog'是通過'QtQuick 2.8'引入的,所以我只能評估底層的'Popup'。 – derM

+0

你確定嗎?我是來自這裏的'Dialog':https://doc.qt.io/qt-5/qml-qtquick-controls2-dialog.html所以不需要'QtQuick 2.8',對嗎? –

1

您可以設置x/y位置(如derM說的),但是您必須重新計算ApplicationWindow的每個大小更改!

下面是另一種解決方案:

ApplicationWindow { 
    id: mainWindow 

    visible: true 
    width: 640 
    height: 480 
    title: qsTr("Hello World") 

    Component.onCompleted: { 
     showMessageBox('Hey this actually works!'); 
    } 

    Item { 
     anchors.centerIn: parent 
     width: msgDialog.width 
     height: msgDialog.height 
     MessageDialog { 
      id: msgDialog 
      title: qsTr("Information") 
      visible: false 
     } 
    } 

    function showMessageBox(message) { 
     msgDialog.text = message 
     msgDialog.visible = true 
    } 

UPDATE:用動態實例:

Item { 
    id: dialogParent 
    anchors.centerIn: parent 
} 

function showMessageBox(message) { 
    var component = Qt.createComponent("MessageDialog.qml") 
    if(component.status === Component.Ready) { 
     var dialog = component.createObject(dialogParent) 

     dialog.title = qsTr("Information") 
     dialog.text = message 
     dialog.open() 

     dialogParent.width = dialog.width 
     dialogParent.height = dialog.height 
    } else { 
     console.error(component.errorString()) 
    } 
} 
+0

當我使用'component.createObject'時會有什麼缺點嗎?我只是想避免當我不使用它們(但)時實例化所有的對話框。 –

+0

不,但您必須在每個實例化時更改該項目的大小。 (我將這個解決方案添加到我的答案中) – Hubi

+0

而且您需要每次重新提取和重新定位「Item」......否則,您的「Dialog」將不在頂層。理想情況下,'Dialog'的父節點是['ApplicationWindow.overlay'](https://doc.qt.io/qt-5/qml-qtquick-controls2-applicationwindow.html#attached-properties)。你可以在ApplicationWindow.overlay層創建一個Item,並且一直重用它,但是你一次只能使用一個Dialog。當您打開另一個尺寸不同的其他尺寸時,請將其關閉,正確的尺寸將不會恢復。 – derM