0

HERE IS Qt PROJECT with minimal skeleton to show what is the problem (check console after you run that project) http://uloz.to/xqxrXpdL/qtproject-zip

I try to call public slot from qml

Component.onCompleted: print(model.activate())

Still getting error:

TypeError: Property 'activate' of object QQmlDMObjectData(0x7fa35dd89eb0) is not a function

If i tried to call the method dynamically from C++, it works:

auto item = new TreeItem<MainMenuItem>(new MainMenuItem("kyklop"));

QMetaObject::invokeMethod(item, "activate");

If i try to access regular property of my TreeItemTemplateBackend class from qml (for instance level), it works, so problem is only if calling method. I am thinking it could be something with that subclass/template class.

Registering to qml:

qmlRegisterType<TreeItemTemplateBackend>("engine.ui", 1, 0, "TreeItemTemplateBackend");
qmlRegisterType<TreeItem<InspectorItem>>("engine.ui", 1, 0, "InspectorTreeItem");
qmlRegisterType<TreeItem<MainMenuItem>>("engine.ui", 1, 0, "MainMenuTreeItem");

TreeItem.h

#ifndef TREEITEM_H
#define TREEITEM_H

#include <QObject>
#include "TreeItemTemplateBackend.h"

template <typename T>
class TreeItem :  public TreeItemTemplateBackend
{

public:
    explicit TreeItem(QObject * parent = NULL);
    explicit TreeItem(T * data, QObject * parent = NULL);
    explicit TreeItem(TreeItem<T> & other);
    void addChild(TreeItem<T> * child);
    ~TreeItem() {}
};

#endif // TREEITEM_H

TreeItemTemplateBackend.h

#ifndef TREEITEMTEMPLATEBACKEND_H
#define TREEITEMTEMPLATEBACKEND_H

#include <QList>
#include <QQmlListProperty>

class TreeItemTemplateBackend : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QObject * data READ data WRITE setData NOTIFY dataChanged)
    Q_PROPERTY(QQmlListProperty<TreeItemTemplateBackend> childs READ childs NOTIFY childsChanged)
    Q_PROPERTY(int level READ level WRITE setLevel NOTIFY levelChanged)
public:
    explicit TreeItemTemplateBackend(QObject * parent = NULL);
    QObject * data() const;
    void setData(QObject * data);
    QQmlListProperty<TreeItemTemplateBackend> childs() const;
    void addChild(TreeItemTemplateBackend * child);
    int level() const;
    void setLevel(int level);
    void dump(QString propertyName) const;
    ~TreeItemTemplateBackend() {}
signals:
    void activated(); 
    void dataChanged();
    void childsChanged();
    void levelChanged();
public slots:
    void activate(); // this is what i am trying to call
protected:
    QObject * m_data;
    QList<TreeItemTemplateBackend *> m_children;
    int m_level;
    static void append_function(QQmlListProperty<TreeItemTemplateBackend> * property, TreeItemTemplateBackend * item);
    static TreeItemTemplateBackend * at_function(QQmlListProperty<TreeItemTemplateBackend> * property, int index);
    static void clear_function(QQmlListProperty<TreeItemTemplateBackend> * property);
    static int count_function(QQmlListProperty<TreeItemTemplateBackend> * property);
};

#endif // TREEITEMTEMPLATEBACKEND_H
6
  • Is you custom type registered to the QML engine? Commented Jun 19, 2014 at 12:30
  • @ddriver: yes it is, i modified code to show the registration Commented Jun 19, 2014 at 12:37
  • Can you provide a compilable example? For example, a single main.cpp and the QML file? Commented Jun 19, 2014 at 16:56
  • @Mitch: i will post zip file with project that i created (it contains just the minimal to show the problem) Commented Jun 19, 2014 at 18:23
  • I am not sure, but it probably has to do with the template. Why don't your try scrapping the template and using QVariant instead of T * for the data, and register needed types to the meta system. Also take a look at this link: doc.qt.digia.com/qq/qq15-academic.html Commented Jun 20, 2014 at 13:11

2 Answers 2

4

QQmlDMObjectData is a wrapper for the actual object used in delegates. The original object is accessible via the property modelData, so model.modelData.activate() should work.

Sign up to request clarification or add additional context in comments.

Comments

0

If you cal for it "method" and still you are use it as a method so convert it to the method:

...
public:
void Q_INVOKABLE activate();
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.