Qml + c++
-
Hi
i run with success this "http://doc.qt.nokia.com/4.7/qtbinding.html#calling-c-methods-from-qml":http://doc.qt.nokia.com/4.7/qtbinding.html#calling-c-methods-from-qml ,but when in run my application (my
c++ class inherits from QSqlRelationalTableModel within Q_INVOCABLE fake funciton), i have this error:"Bar.qml:50 TypeError: Result of expressionon 'modelListItems.fake [undefined] is not a function"
what's wrong?
[edit: fixed link / chetankjain]
-
- Q_INVOKABLE, not Q_INVOCABLE
- modelListItems.fake() , not modelListItems.fake
-
[quote author="nicola1" date="1285600625"]I have Q_INVOKABLE and modelListItems.fake() in my code.[/quote]
So, help us help you... more information needed.
-
main.qml
@
import Qt 4.7
import "content"Rectangle {
width: 400
height: 240
function showListItems() {
list.visible= false;
listItems.visible = true;} function newList() { modelListItems.clear(); ; } List { id: list } ListItems { id: listItems visible: false } Bar { onClickItems: showListItems(); onClickNewList: modelListItems.fake(); }
}
@main.cpp
@
QDeclarativeContext *ctxt = view.rootContext();
ctxt->setContextProperty("modelListItems", &model);view.setSource(QUrl::fromLocalFile("listview/Main.qml");
@
My model.h
@
Q_INVOKABLE void fake() const;
@My model.cpp
@
void Mymodel::fake() const
{qDebug() << "FAKE " << endl;
}
@[edit: highlight added / Denis Kormalev]
-
Is there Q_OBJECT define in Mymodel class declaration?
I see that you use object in stack, not in heap. Maybe object is out of scope and deleted because of it and so it is not accessible from Qml?
-
[quote author="Denis Kormalev" date="1285604321"]Is there Q_OBJECT define in Mymodel class declaration?[/quote]
Denis is correct.
Q_INVOKABLE member functions are called via the meta-object system, so you must add Q_OBJECT in your class declaration, then moc can do its job =)
-
nicola1, Jus make it
@
class QLSqlTableModel : public QSqlRelationalTableModel
{
Q_OBJECT
QHash<int, QByteArray> roles;
public:
QLSqlTableModel(QObject *parent = 0);
QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
Q_INVOKABLE void fake() const;
};
@ -
[quote author="nicola1" date="1285657479"]I added Q_OBJECT and now is working, Thanks. I should understand because i need Q_OBJECT when y class inherit from QSqlTableModel.
[/quote]
"http://doc.trolltech.com/4.7/qobject.html#Q_OBJECT":http://doc.trolltech.com/4.7/qobject.html#Q_OBJECT