Results 1 to 7 of 7

Thread: Best approach to retrieve values from a QML Modal dialog

  1. #1
    Join Date
    Feb 2007
    Posts
    39
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Best approach to retrieve values from a QML Modal dialog

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Best approach to retrieve values from a QML Modal dialog

    How is your QML dialog implemented?
    Is it a widget which is/has a QDeclarativeView or something different?
    Please post code.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2007
    Posts
    39
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Best approach to retrieve values from a QML Modal dialog

    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++
    Qt Code:
    1. QDeclarativeEngine *engine = new QDeclarativeEngine();
    2. QDeclarativeComponent component( engine, "modaldialog.qml" );
    3. QObject *myObject = component.create();
    4.  
    5. QGraphicsObject* graphicsObject =qobject_cast<QGraphicsObject *>(myObject);
    6. scene.addItem(graphicsObject);
    To copy to clipboard, switch view to plain text mode 

    QML modaldialog.qml ( i use the modaldialog component from http://projects.forum.nokia.com/QMLTemplates)
    Qt Code:
    1. import QtQuick 1.0
    2. import "component" as Comp
    3.  
    4. Item
    5. {
    6. width: 400; height: 400
    7. // Visual is an item that defines some constants for the application
    8. // look and feel, e.g. the font sizes & colors etc.
    9. Comp.Visual
    10. {
    11. id: visual
    12. }
    13.  
    14. // Example on using ModalDialog component.
    15. Comp.ModalDialog_t2
    16. {
    17. id: dialog_t2
    18.  
    19. // Shown text can be set by modifying "text" property.
    20. //text: "Click OK to Accept this dialog. To send it away, click Cancel."
    21. // Always remember to define the size for the dialog.
    22. anchors.fill: parent
    23.  
    24. // Demonstrating how one could keep application wide variables saved
    25. // in one place and use them conveniently all around the application.
    26. fontName: "Helvetica"
    27. fontColor: "#9DE352"
    28. fontColorButton: "#9DE352"
    29. fontSize: 16
    30.  
    31. // Use these if you would like to change the Button look and feel.
    32. // See Visual.qml how these are defined. Remember to modify also the
    33. // ModalDialog.qml, since this functionality is disabled currently.
    34. // buttonBackground: visual.buttonComponent
    35. // buttonBackgroundPressed: visual.buttonPressedComponent
    36.  
    37. onAccepted:
    38. {
    39. console.log("Dialog accepted signal received!");
    40.  
    41. // 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
    42. // how i could manage such thing?
    43. }
    44.  
    45.  
    46. onCancelled:
    47. {
    48. console.log("Dialog cancelled signal received.")
    49. }
    50. }
    51.  
    52.  
    53.  
    54. function dialog_type_02()
    55. {
    56. dialog_t2.show();
    57. }
    58.  
    59.  
    60. // The model:
    61. ListModel
    62. {
    63. id: list_model
    64.  
    65. Component.onCompleted: dialog_type_02()
    66. }
    67. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Best approach to retrieve values from a QML Modal dialog

    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??
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. The following user says thank you to high_flyer for this useful post:

    Mrdata (3rd May 2011)

  6. #5
    Join Date
    Feb 2007
    Posts
    39
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Best approach to retrieve values from a QML Modal dialog

    Qt Code:
    1. QML_view = new QDeclarativeView;
    2. QML_view->setSource( QUrl::fromLocalFile( QML_file ) );
    3.  
    4. QDialog *dialog = new QDialog( QML_view );
    5. dialog->exec();
    To copy to clipboard, switch view to plain text mode 

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

  7. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Best approach to retrieve values from a QML Modal dialog

    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?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. The following user says thank you to high_flyer for this useful post:

    Mrdata (4th May 2011)

  9. #7
    Join Date
    Feb 2007
    Posts
    39
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Best approach to retrieve values from a QML Modal dialog

    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

    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

Similar Threads

  1. Non-Modal about dialog?
    By N3wb in forum Qt Programming
    Replies: 1
    Last Post: 8th December 2010, 06:47
  2. QT Modal Dialog
    By jiapei100 in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2010, 18:15
  3. Replies: 0
    Last Post: 8th November 2009, 11:29
  4. Retrieve Curve values
    By nenukino in forum Qwt
    Replies: 2
    Last Post: 26th February 2008, 15:33
  5. Replies: 8
    Last Post: 25th February 2008, 18:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.