Results 1 to 6 of 6

Thread: Access caller qml-Object from C++ Object

  1. #1
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1

    Default Access caller qml-Object from C++ Object

    Hi everyone,

    I have a qml file that is loaded using a QDeclarativeView in a small main application.
    The qml file uses a C++ class (call it MyClass) and in case of a click event calls a method from that class (say Q_INVOKABLE void MyClass::handleClick()).
    From that method I would now like to access elements of the qml Gui. How is this possible? Is there something equivalent to what QObject::sender() does in Qt slots?

    I know that I could emit a signal at the end of MyClass::handleClick() and connect that with a slot in the qml file to solve this but I'd rather have access to the qml file directly (at least to know how it's done).

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Access caller qml-Object from C++ Object

    You can get access to QML elements with QDeclarativeContext and its contextProperty() method.

    However I'd advise against accessing QML elements from C++. Normally you'd rather do the opposite.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1

    Default Re: Access caller qml-Object from C++ Object

    So you would recommend that emitting a signal and then handling the rest in a qml slot stuff I wrote above?!
    Or is there another (cleaner/more recommended) way?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Access caller qml-Object from C++ Object

    Quote Originally Posted by xdn View Post
    So you would recommend that emitting a signal and then handling the rest in a qml slot stuff I wrote above?!
    Or is there another (cleaner/more recommended) way?
    I have no idea what your code is meant to do so it is hard to suggest a good solution. I can only say that going from UI stuff to logic and then back to UI is likely a bad design.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access caller qml-Object from C++ Object

    Quote Originally Posted by wysota View Post
    I can only say that going from UI stuff to logic and then back to UI is likely a bad design.
    Not necessarily.
    The logic could reach a state where user input is required so it has to delegate back to the UI.

    Consider a case where the UI provides a way for the user to trigger saving of data. This would very likely call into the C++ core to do the actual saving.
    If that code then discovers that there is no know filename yet, it somehow has to ask the UI to ask the user for one.

    I do agree of course that accessing UI elements from logic is a no-go, but signalling the UI to do something sounds very reasonable.

    Cheers,
    _

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Access caller qml-Object from C++ Object

    Quote Originally Posted by anda_skoa View Post
    If that code then discovers that there is no know filename yet, it somehow has to ask the UI to ask the user for one.
    I think it should return information to the UI that it can't save the file because of a missing filename, the UI should ask the user for the filename and then call the save routine again. Or the logic should explicitly offer the filename to the UI and the UI should first check if it is valid or not and then call the saving routine. Doing what you suggest if of course possible and acceptable and I would probably do that myself in many cases however in theory I would still consider it bad design because of two direction coupling of components (UI had to know about logic and logic has to know about UI).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 17
    Last Post: 11th April 2013, 10:35
  2. Access Object Created by javascript in qml
    By alizadeh91 in forum Qt Quick
    Replies: 6
    Last Post: 23rd February 2012, 16:06
  3. Two QProcess object access to same function
    By pronetin in forum Newbie
    Replies: 3
    Last Post: 20th February 2011, 12:44
  4. Access an object stored in the list.
    By cbarmpar in forum General Programming
    Replies: 2
    Last Post: 21st September 2008, 21:19
  5. Access an object from another file.
    By cbarmpar in forum General Programming
    Replies: 1
    Last Post: 6th September 2008, 23:17

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.