【问题标题】:How do I show a message box with Qt Quick Controls?如何使用 Qt Quick Controls 显示消息框?
【发布时间】:2013-09-18 05:50:07
【问题描述】:

当人们希望使用 Qt Quick Controls 编写 QML 应用程序时,QMessageBox::information() 的等价物是什么?

【问题讨论】:

标签: qt qml qt-quick qtquick2


【解决方案1】:

您可以在 QtQuick Controls 2 中使用Popup

import QtQuick.Window 2.2
import QtQuick.Controls 2.0 // or import Qt.labs.controls 1.0

Window {
    id: window
    width: 400
    height: 400
    visible: true
Button {
    text: "Open"
    onClicked: popup.open()
}

Popup {
    id: popup
    x: 100
    y: 100
    width: 200
    height: 300
    modal: true
    focus: true
    closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent
    }
}

【讨论】:

    【解决方案2】:
    // CenteredDialog.qml
    import QtQml 2.2
    
    import QtQuick 2.9
    import QtQuick.Controls 2.2
    
    Dialog {
        parent: ApplicationWindow.overlay
    
        x: (parent.width - width) / 2
        y: (parent.height - height) / 2
    
        focus: true
        modal: true
    
        property alias text: messageText.text
    
        Label {
            id: messageText
    
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
    
            anchors.fill: parent
        }
    
        standardButtons: Dialog.Ok
    }
    

    您可以在某处声明CenteredDialog { id: centeredDialog },然后在某些事件处理程序中您可以调用:

     centeredDialog.title = qsTr("Error!")
     centeredDialog.text = qsTr("Access violation")
     centeredDialog.visible = true
    

    【讨论】:

      【解决方案3】:

      不幸的是,没有,至少在 Qt 5.1.1 的发货 Qt Quick Controls 中没有:(

      您需要通过QObject wrapper 将其添加到您的项目中。

      【讨论】:

        【解决方案4】:

        在 Qt 5.2 中有 MessageDialog:

        http://doc.qt.io/qt-5/qml-qtquick-dialogs-messagedialog.html

        import QtQuick 2.2
        import QtQuick.Dialogs 1.1
        
        MessageDialog {
            id: messageDialog
            title: "May I have your attention please"
            text: "It's so cool that you are using Qt Quick."
            onAccepted: {
                console.log("And of course you could only agree.")
                Qt.quit()
            }
            Component.onCompleted: visible = true
        }
        

        【讨论】:

          【解决方案5】:

          好的,这可以完成工作(很糟糕)。导入Window 对象:

          import QtQuick.Window 2.1
          

          然后将其添加到您的主窗口(或者您可以将其放在另一个文件中,我猜):

          function showMessage(text, title)
          {
              messageBox.text = text;
              messageBox.title = title;
              messageBox.visible = true;
          }
          
          Window {
              id: messageBox
              modality: Qt.ApplicationModal
              title: ""
              visible: false
              property alias text: messageBoxLabel.text
              color: parent.color
              minimumHeight: 100
              minimumWidth: 300
              Label {
                  anchors.margins: 10
                  anchors.top: parent.top
                  anchors.left: parent.left
                  anchors.right: parent.right
                  anchors.bottom: messageBoxButton.top
                  horizontalAlignment: Text.AlignHCenter
                  wrapMode: Text.WordWrap
                  id: messageBoxLabel
                  text: ""
              }
          
              Button {
                  anchors.margins: 10
                  id: messageBoxButton
                  anchors.bottom: parent.bottom
                  anchors.horizontalCenter: parent.horizontalCenter
                  text: "Ok"
                  onClicked: messageBox.visible = false
              }
          }
          

          它的问题:

          1. 可以缩小窗口,使文本和按钮重叠。
          2. 最小窗口大小是硬编码的,而不是根据文本大小计算出来的。
          3. 您无法选择文本。
          4. 看起来有点烂。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-06-11
            • 2017-09-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多