15

QMessageBox::information()当一个人希望使用 Qt Quick Controls 编写 QML 应用程序时,这相当于什么?

4

5 回答 5

15

在 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
}
于 2013-12-17T23:11:40.737 回答
6

您可以在 QtQuick Controls 2 中使用弹出窗口:

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
    }
}
于 2016-09-06T11:39:38.353 回答
2

好的,这可以完成工作(很糟糕)。导入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. 看起来有点鸡肋
于 2013-09-16T09:52:15.333 回答
1

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

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

于 2013-09-13T15:39:43.003 回答
1
// 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
于 2017-08-29T11:30:16.080 回答