QtQuick - button onClick event

14,199

Solution 1

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.

Solution 2

You have to use signals and slots in order to trigger an event. You could use a QMessageBox that pops up to display Hello world.

Share:
14,199
lyubolp
Author by

lyubolp

Updated on June 13, 2022

Comments

  • lyubolp
    lyubolp about 2 years

    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

  • dtech
    dtech almost 10 years
    Bad idea to mix QtQuick with QWidgets. And unnecessarily tedious. There are already QML message box components.