Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. MessageDialog in QML
Qt 6.11 is out! See what's new in the release blog

MessageDialog in QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adrian.Aioanei
    wrote on last edited by Adrian.Aioanei
    #1

    Hey again to all :D

    I am back with a little problem.:D
    I want to create a MessageDialog in QML.
    I have found this in Qt documentation (Qt doc).

    And if i try to run this example:

    import QtQuick 2.2
    import QtQuick.Dialogs 1.1
    
    MessageDialog {
        title: "Overwrite?"
        icon: StandardIcon.Question
        text: "file.txt already exists.  Replace?"
        detailedText: "To replace a file means that its existing contents will be lost. " +
            "The file that you are copying now will be copied over it instead."
        standardButtons: StandardButton.Yes | StandardButton.YesToAll |
            StandardButton.No | StandardButton.NoToAll | StandardButton.Abort
        Component.onCompleted: visible = true
        onYes: console.log("copied")
        onNo: console.log("didn't copy")
        onRejected: console.log("aborted")
    }
    

    Doesn't give me any error but when i use this in main :

        QQuickView view;
        view.setSource(QUrl("qrc:/window.qml"));
        view.show();
    

    Popup a empty window.
    And what is more strange is when i run, popup tow window and i don't understand why.
    Is there another way or maybe the correct way to use MessageDialog ?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Prochy
      wrote on last edited by Prochy
      #2

      I think your code give you an error like this:
      "QQuickView only supports loading of root objects that derive from QQuickItem.
      Ensure your QML code is written for QtQuick 2, and uses a root that is or
      inherits from QtQuick's Item (not a Timer, QtObject, etc). "
      So you need put your MessageDialog to e.g. Item wrap like this:

      import QtQuick 2.2
      import QtQuick.Dialogs 1.1
      Item{
      MessageDialog {
          title: "Overwrite?"
          icon: StandardIcon.Question
          text: "file.txt already exists.  Replace?"
          detailedText: "To replace a file means that its existing contents will be lost. " +
              "The file that you are copying now will be copied over it instead."
          standardButtons: StandardButton.Yes | StandardButton.YesToAll |
              StandardButton.No | StandardButton.NoToAll | StandardButton.Abort
          Component.onCompleted: visible = true
          onYes: console.log("copied")
          onNo: console.log("didn't copy")
          onRejected: console.log("aborted")
      }
      }
      

      And It should work now.
      And in main use only:

      QQuickView view;
      view.setSource(QUrl("qrc:/window.qml"));
      
      1 Reply Last reply
      0
      • A Offline
        A Offline
        Adrian.Aioanei
        wrote on last edited by Adrian.Aioanei
        #3

        Yes, now works fine.
        Thank you @Prochy :)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved