如何使用Qt Quick Controls显示消息框?

时间:2013-09-13 13:49:18

标签: qt qml qt-quick qtquick2

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

5 个答案:

答案 0 :(得分:11)

在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
}

答案 1 :(得分:4)

您可以在QtQuick Controls 2中使用Popup

                pipe(fd);
                if ((pid = fork()) < 0)
                    perror("fork error");
                else if (pid == 0) {
                    dup2(fd[1], STDOUT_FILENO);
                    close(fd[0]);   // close both fd in child
                    close(fd[1]);
                    execvp(*command.argv1, command.argv1);
                } else {
                    dup2(fd[0], STDIN_FILENO);
                    close(fd[0]);
                    close(fd[1]);   // close both fd in parent
                    if (command.redirect_out)
                    {
                        fdout =
                          open(command.outfile, O_CREAT|O_WRONLY|O_TRUNC, 0666);
                        dup2(fdout, STDOUT_FILENO);
                    }
                    execvp(*command.argv2, command.argv2);
                }

答案 2 :(得分: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. 看起来有点狗屎。

答案 3 :(得分:1)

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

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

答案 4 :(得分:0)

// 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