Search code examples
qtqmlqtquick2qt-quickqtquickcontrols

QtQuick - button onClick event


Background story

So I recently decided that I should try out Qt. I started making a QtQuick Apllication. In my designer view I have one button and a mouse area.


What I want to do:

When I click the button, I want to display a message box with some text (like "Hello World").


My Question

How can I do that ?


Additional info

I tried googling it, i tried following this answer. But still nothing. I know how to program in .Net (C# and VB), i have some knowage in C/C++, but Qt seems to hard for me


Solution

  • How about this:

    import QtQuick 2.0
    import QtQuick.Controls 1.0
    import QtQuick.Dialogs 1.1    
    
    Rectangle {
        width: 360
        height: 360
    
        MessageDialog {
            id: msg
            title: "Title"
            text: "Button pressed"
            onAccepted: visible = false
        }
    
        Button {
            text: "press me"
            onClicked: msg.visible = true
        }
    }
    

    And if you prefer to have the dialog dynamically instantiated with arbitrary properties rather than have it "hardcoded", follow the first snippet from this answer. You can also set properties in createQmlObject() and instead of hiding the dialog just use destroy() to delete it.