Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] noob in qml / c++ unable to bind method return to onClicked event: TypeError Object [object Object] has no method XXXXXX
Qt 6.11 is out! See what's new in the release blog

[SOLVED] noob in qml / c++ unable to bind method return to onClicked event: TypeError Object [object Object] has no method XXXXXX

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.9k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    dhevans79
    wrote on last edited by
    #1

    Hi all,

    First, thanks for looking, and possibly responding to help me make a start with this development tool.

    I'm trying to do a test where I use a QML window with a button to change the text of the button with the results of the function QString returnComplete(). However, even though the autocomplete shows me the method name, I get the error: TypeError: Object [object Object] has no method 'returnComplete'

    Please see the code snippets below for more info. Please let me know if you need more...

    backupplugin.h
    @
    #include <QTime>
    #include <QString>

    #ifndef BACKUPPLUGIN_H
    #define BACKUPPLUGIN_H

    class Backup : public QObject{
    Q_OBJECT

    Q_INVOKABLE QString returnComplete();
    Q_INVOKABLE int returnOne();
    

    };

    #endif // BACKUPPLUGIN_H
    @

    backupplugin.cpp
    @
    #include "backupplugin.h"

    QString Backup::returnComplete()
    {
    return QString("test complete");
    }

    int Backup::returnOne() {
    return 1;
    }
    @

    main.cpp
    @qmlRegisterType<Backup>("testNamespace", 1, 0, "MyBackup");@

    main.qml
    @Page {
    title: i18n.tr("Simple Backup")

        Backup {
            id: testBackup
        }
        ....
        //Start Button
                Row {
                    width: parent.width
                    Button {
                        id: btnStart
                        objectName: "btnStart"
                        width: parent.width
    
                        text: i18n.tr("Start Backup!")
                        onClicked: btnStart.text = MyBackup.returnComplete();
    
                    }
                }
    

    @

    I have also tried (what I think to be correct, but I do not get any autocomplete options that match the function name...)

    @onClicked: btnStart.text = testBackup.returnComplete();@

    but then I get the error:
    TypeError: Object Backup_QMLTYPE_36(0xddf660) has no method 'returnComplete'

    Whatever I try, I am unable to get the return value from the c++ function.

    please help, several hours of walking through the many, many examples on google searches, qt forums, BB forums, c++ forums, etc and I am unable to run a simple "hello world" POC for this stuff.

    Thanks and regards,

    David

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      In your main.cpp:
      @
      QQuickView *myView; // or whatever you are using to display your QML code
      Backup *myBackup = new Backup();
      myView->rootContext->setContextProperty("MyBackup", myBackup);
      @

      Then your code should work with:
      @
      onClicked: btnStart.text = MyBackup.returnComplete();
      @

      You can delete those lines, they are not needed:
      @
      // C++
      qmlRegisterType<Backup>("testNamespace", 1, 0, "MyBackup");

      // QML
      Backup {
      id: testBackup
      }
      @

      (Z(:^

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dhevans79
        wrote on last edited by
        #3

        Sierdzio,

        Thanks for your reply. Unfortunately it doesn't work for me.

        I have made the updates as you indicate, and I still get the same message:
        TypeError: Object Backup(0x113c410) has no method 'returnComplete'

        Just to confirm some details, as there were some differences I needed to make...

        1. I'm using QT 5 on Ubuntu 12.10 with QtQuick 2
        2. The window viewer is of type QtQuick2ApplicationViewer, however by using the autocomplete, it modified the line to:
          @viewer.rootContext()->setContextProperty("MyBackup", myBackup);@

        Sorry that it didn't work out, thanks for any further suggestions. I can zip the project for you if that would help???

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Ah, huh, ok. I've missed an obvious part. Methods are private by default in C++... so your method is not visible to QML engine or MOC :D
          just add "public:" to your code, after the Q_OBJECT macro:
          @
          class Backup : public QObject{
          Q_OBJECT
          public:
          Q_INVOKABLE QString returnComplete();
          Q_INVOKABLE int returnOne();

          };
          @

          (Z(:^

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dhevans79
            wrote on last edited by
            #5

            Doh!

            Thank you for that, that was the trick. My apologies for such an obvious blunder...

            Many regards,

            David

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              No problem, very often it's the small and obvious detail like this :) Happens all the time to everybody ;)

              (Z(:^

              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