Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML slot looses context
Forum Update on Monday, May 27th 2025

QML slot looses context

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shukurmukur
    wrote on last edited by
    #1

    Hello.

    I am developing an application with QML frontend and C++ backend - QML retrieves all data required by calling C++ functions on backend. From QML backend functions are accessible through special context property which is set like that:

    @
    Backend backend;
    qmlContext->setContextProperty("backend", &backend);
    @

    This is working nicely. However some backend functions are asynchrounous, so I would like to implement a promise-like solution - asynchronous function would return an intermediate promise object, which would emit signals when data is available. So my Promise class looks like this:

    @
    class Promise: public QObject
    {
    Q_OBJECT
    public:
    explicit Promise(QObject* parent=0);

    signals:
    void finished(QVariant value);

    private:
    Q_DISABLE_COPY(Promise)
    ...
    };

    ...
    qmlRegisterUncreatableType<Promise>("com.myapp", 1, 0, "Promise", "Promise can only be created from c++");

    @

    In Backend class asynchronous function is implemented like this:

    @

    Q_INVOKABLE Promise* getData();

    ..

    Promise* Backend::getData()
    {
    auto promise = new Promise();
    ...
    return promise;
    }

    @

    In QML I am calling backend function, get promise object and connect a javascript function to its signal like this:

    @

    Item {
    id: root

        Button {
            id: button
            ...
    
            onClicked: {
                promise = backend.getData();
                promise.finished.connect(gotData);
            }
        }
    }
    
    property var promise: null
    
    function gotData() {
        console.log("gotData", root, button); // prints: gotData undefined undefined
    }
    

    }

    @

    The problem is, that QML function is called as intended when promise object emits 'finished' signal, however in that function no context variables are accessible.

    Am I missing something? Or is my approach totally wrong?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Can you try just calling function gotData() directly and see if you get access to those components in it ? Also is the Item with id root inside any other Item ?

      157

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shukurmukur
        wrote on last edited by
        #3

        Yes, if called normally, gotData works as expected. Also tried to connect to a signal on backend object - also worked correctly. Only when connecting on promise signal it does not work as I want

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          Hmm.. Can you post the complete compilable code which shows the problem?

          157

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shukurmukur
            wrote on last edited by
            #5

            Ok, it seems the problem was not with my promise pattern - some other code was simply recreating views, hence lost context. Stupid me :/ Thank you, p3c0

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved