[SOLVED] noob in qml / c++ unable to bind method return to onClicked event: TypeError Object [object Object] has no method XXXXXX
-
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_Hclass Backup : public QObject{
Q_OBJECTQ_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
-
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
}
@ -
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...
- I'm using QT 5 on Ubuntu 12.10 with QtQuick 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???
-
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();};
@ -
No problem, very often it's the small and obvious detail like this :) Happens all the time to everybody ;)