我使用的是QT5.7.1,UI是使用QML定义的 . 我们的应用程序用于控制仪器并以自助服务终端模式运行 .

我正在尝试使用MessageDialog元素在出现某些情况时通知操作员,例如电池电量不足,磁盘空间不足,......

MessageDialog在我的main.qml中声明,并在c方法发出信号(notifyOperator)时打开 .

我在main.qml(onNotifyOperator)中定义了一个槽/函数来处理信号 . 这两个连接在我的应用程序的main()函数中(类似于示例inhttps://andrew-jones.com/blog/qml2-to-c-and-back-again-with-signals-and-slots/ ) . 我尝试使用QML Connections元素来连接信号和插槽,并出现以下编译错误:无效的附加对象分配 . 我仍然不明白这个,但我想在以后解决这个问题 .

我还发现了一条评论,指出在打开Dialog之前进行属性更改是绝对必要的,即处于关闭状态 . (MessageDialog - text change from c++ but no update on screen)如果我想更改已打开的MessageDialog中的内容,我必须关闭它,更改属性然后再次打开它 . 仅更改属性然后关闭并重新打开(如"redraw")是不够的 . 这就是onNotifyOperator(),在属性更改后打开MessageDialog() . 更令人不安的是,我没有更改MessageDialog的属性,仍然得到元素的不完整显示 .

main.qml文件包含以下声明:

QtObject {
    property real defaultSpacing: 10
    property SystemPalette palette: SystemPalette { }

    //
    // scale factors dynamically computed
    //
    property double horizScaleFactor : Screen.width / 1280.0;
    property double vertScaleFactor : Screen.height / 800.0;

    property var splashWindow: Splash {
        onTimeout:
        {
            console.log( "Splash timeout: ...)

            ...
            controlWindow.visible = true
       }
    }

    property var controlWindow: Window {
        id: mainWindow
        objectName: "mainWindow"

        // this function is our QML slot; used to open a MessageDialog to notify the operator
        function notifyOperator()
        {
            console.log( "notifyOperator slot")
            myMessageDialog.open()
        }
 ...
 }

MessageDialog声明如下(controlWindow的一部分):

MessageDialog {
    id: myMessageDialog

    modality: Qt.WindowModal
    icon : StandardIcon.Warning
    standardButtons: StandardButton.Ok

    title: qsTr( "Power Adapter Status")
    text: qsTr( "Please disconnect the dock power adapter")

    onAccepted: {
        console.log( "messageDialog accepted() ")
    }

    Component.onCompleted: {
        console.log( "messageDialog.Component.onCompleted(); visible: ", visible)
        console.log( "messageDialog.Component.onCompleted(); title: ", title)
        console.log( "messageDialog.Component.onCompleted(); text: ", text)
    }
    onVisibilityChanged: {
        console.log( "messageDialog.onVisibilityChanged(); visible: ", visible)
        console.log( "messageDialog.onVisibilityChanged(); title: ", title)
        console.log( "messageDialog.onVisibilityChanged(); text: ", text)
    }
}

“应用程序输出”选项卡中显示的消息包括:

Starting U:\gitMaster\build-cad-detx-Desktop_Qt_5_7_1_MinGW_32bit-Debug\debug\cadx-gui.exe...

qml: messageDialog.Component.onCompleted(); visible:  false
qml: messageDialog.Component.onCompleted(); title:  Power Adapter Status
qml: messageDialog.Component.onCompleted(); text:  Please disconnect the dock power adapter

...
qml: notifyOperator slot
qml: messageDialog.onVisibilityChanged(); visible:  true
qml: messageDialog.onVisibilityChanged(); title:  Power Adapter Status
qml: messageDialog.onVisibilityChanged(); text:  Please disconnect the dock power adapter
The program has unexpectedly finished.

最后,我附上了MessageDialog的屏幕截图[MessageDialog截屏1

正如我所指出的,即使 Headers 和文本元素是固定的,MessageDialog也是不正确的 . 有什么建议?另外,如何摆脱 Headers 栏中的问号?

问候,丹尼尔