在qml中显示MessageDialog

时间:2014-07-07 11:53:24

标签: c++ qml qt5

我知道这可能是一个简单的但我刚刚开始使用qml和qt quick。我想显示一个MessageDialog,它包含我从C ++收到的文本,例如:

QML文件

import QtQuick 2.2
import QtQuick.Dialogs 1.1
import QtQuick.Window 2.1

Item  {

  id: container

  property string text
  property string alertBoxMsg: "Test"
  property bool disabled: false
  signal clicked

  // Suitable default size
  width: parent.width
  height: parent.height

  anchors.centerIn: parent

  signal loginButtonClicked(string username, string userpwd);


  Rectangle {

    id: screenFrame
    anchors.centerIn: parent
    //border.color: "black"
    //border.width: 5
    //width: 200
    //height: 200

    Column {
     id: simpleColumn
     anchors.centerIn: parent

     TextInput {

        id: username
        text: "username"
        color: "green"

        MouseArea {

            anchors.fill: parent

            onClicked: { 

                container.clearTextInput(username, "username");
            }
        }
      }


      TextInput {

          id: userpassword
          text: "password"
          echoMode: TextInput.Password

          MouseArea {

              anchors.fill: parent

              onClicked: { 

                  container.clearTextInput(userpassword, "password");
              }
          }
      }


      Button {

          id: loginButton
          text: "Login"
          property string alertBoxMsg: "None"


          MouseArea {

              anchors.fill: parent

              onClicked: { 

                  container.loginButtonClicked(username.text, userpassword.text);
              }
          }
      }


      MessageDialog {

          id: messageBox;
          objectName: "msgBox";

          title: "Information";
          //text: dialogText;

          onAccepted: {
              console.log("And of course you could only agree.")
              Qt.quit()
          }


          function displayMessageBox(msg) {

              messageBox.text = msg;
              messageBox.open();

              console.log("Got message:", messageBox.text);
          }
       }

    }

  }



  function clearTextInput(textInputField, matchingText) {


      if(textInputField.text == matchingText) {

          textInputField.text = "";
      }

      textInputField.forceActiveFocus();
  }
}

C ++文件

QMetaObject::invokeMethod(
                          msgBox, 
                          "displayMessageBox", 
                          Q_RETURN_ARG(QVariant,   returnedValue), 
                          Q_ARG(QVariant, toQmlMessage)
                          );

但MessageDialog拒绝显示,但控制台显示该消息。请帮帮我,我不知道我哪里出错了。我一直在努力解决这个问题。

0 个答案:

没有答案
相关问题