PDA

View Full Version : Best approach to retrieve values from a QML Modal dialog



Mrdata
2nd May 2011, 20:35
In my QT C++ application i call a QML ModalDialog with 2 buttons (OK/CANCEL), which displays correctly on screen and so, no problem there.
However i'm struggling to find a way to retrieve in my QT C++ application which button was pressed.
I'm unable to somehow "freeze" when i call the QML ModalDialog, to wait there until the user press OK Button or Cancel Button
What i see is that application calls the QML ModalDialog, and immediately exit that part and continue.
QMetaObject::invokeMethod can call a QML function and have a return value, but it just doesn't wait for the user press one of the buttons, it just exits immediately, so no use.

I want to use this QML ModalDialog in several places of my application (the QML modal
dialog can have different text passed from my QT C++ application), so i was looking to a generic solution for this.

Basically and generic speaking i'm looking for something like this:

C/C++
return_value = QML_Modal_Dialog(....)


Can someone point me in the right direction? Thanks

high_flyer
3rd May 2011, 14:28
How is your QML dialog implemented?
Is it a widget which is/has a QDeclarativeView or something different?
Please post code.

Mrdata
3rd May 2011, 15:03
This is my "last version" of the code, i tried different things in both qml and c/c++, but the basic problem remains, how to "stop" my C/C++ application and
wait for the user press "OK" or "Cancel" button, and retrieve the value in my C/C++ code?


I've also tried the other way, instead of QDeclarativeEngine i used QDeclarativeView, i guess it doesn't make a difference in solving this issue?


C/C++


QDeclarativeEngine *engine = new QDeclarativeEngine();
QDeclarativeComponent component( engine, "modaldialog.qml" );
QObject *myObject = component.create();

QGraphicsObject* graphicsObject =qobject_cast<QGraphicsObject *>(myObject);
scene.addItem(graphicsObject);


QML modaldialog.qml ( i use the modaldialog component from http://projects.forum.nokia.com/QMLTemplates)


import QtQuick 1.0
import "component" as Comp

Item
{
width: 400; height: 400
// Visual is an item that defines some constants for the application
// look and feel, e.g. the font sizes & colors etc.
Comp.Visual
{
id: visual
}

// Example on using ModalDialog component.
Comp.ModalDialog_t2
{
id: dialog_t2

// Shown text can be set by modifying "text" property.
//text: "Click OK to Accept this dialog. To send it away, click Cancel."
// Always remember to define the size for the dialog.
anchors.fill: parent

// Demonstrating how one could keep application wide variables saved
// in one place and use them conveniently all around the application.
fontName: "Helvetica"
fontColor: "#9DE352"
fontColorButton: "#9DE352"
fontSize: 16

// Use these if you would like to change the Button look and feel.
// See Visual.qml how these are defined. Remember to modify also the
// ModalDialog.qml, since this functionality is disabled currently.
// buttonBackground: visual.buttonComponent
// buttonBackgroundPressed: visual.buttonPressedComponent

onAccepted:
{
console.log("Dialog accepted signal received!");

// I know could call a C/C++function here or send a signal, but, if this modal dialog is to be used/called in several places of C/C++ application
// how i could manage such thing?
}


onCancelled:
{
console.log("Dialog cancelled signal received.")
}
}



function dialog_type_02()
{
dialog_t2.show();
}


// The model:
ListModel
{
id: list_model

Component.onCompleted: dialog_type_02()
}
}

high_flyer
3rd May 2011, 15:29
how to "stop" my C/C++ application and
wait for the user press "OK" or "Cancel" button,
It was not clear to me that this was the question, rather "why it doesn't work" where 'it' is the modal implementation.
That is why I ask how did yo implement it.

I would implement it as a modal QDialog, in which I would have a QDeclarativeView, from which I would then connect a signal from the QML to an accepted()/rejected() slots of the QDialog, which would release the dialog as usual to the C++ code using it.
This way you don't need to worry about the "modalness" your self.

Just out of curiosity, why make a a popup modal dialog in QML??

Mrdata
3rd May 2011, 19:50
QML_view = new QDeclarativeView;
QML_view->setSource( QUrl::fromLocalFile( QML_file ) );

QDialog *dialog = new QDialog( QML_view );
dialog->exec();


tried this, the Qdialog appears, but always empty, no sign of qml stuff inside the qdialog, what am i missing here?

high_flyer
3rd May 2011, 21:07
You are creating a QDialog with a QDeclarativeView as parent - that is all.
You left the QDialog empty.
You need to subclass QDialog, and make the QDeclarativeView its member.

What about my question? :)

Mrdata
4th May 2011, 14:36
Thanks, i managed to subclass qdialog and put qdeclarativeview as a member and it worked, i'm now trying to sort out other new problems :confused:

Regarding your question, it's for a computer game, i already use other qml parts inside my c++ code, and to maintain some visual consistency and the flexibility that QML offers me i decided to use it also for my modal dialogs