C++ Qml ctor problem
-
I created a qml extension in c++ (inherits QObject) and when debugging it i discivered that some of the READ and WRITE functions of some properties are called before the ctor has finished.
Is it possible or am I missing something ? -
Could you show us your code, please?
-
@
class MyItem: public QObject
{
Q_OBJECTQ_PROPERTY(int key READ key WRITE setKey NOTIFY keyChanged)
public:
explicit MyItem(QObject* _parent = 0);const int key() const;
void setKey(const int key);
@setKey is called in the middle of the ctor
Edit: please use @ tags around code sections; Andre
-
OK, nothing weird in that code section... I assume you're not, directly or indirectly, calling it yourself?
Otherwise, it would be time to break out the debugger, set a breakpoint, and see what happens with the actual use of your item. -
In the ctor i set the val of m_key to be 8 but when key() is called on the first time (because of qml object that reads it) it return 0
When i use the debugger i can see that it goes to key() in the middle of the ctor code
-
Can you trace how you get from the creator code to the key() method then?
-
Was confused what this "ctor" is.... after some thinking and guessing.. it looks like its "constructor" :).
-
Its time you post some more code.. through out your class function definitions and your qml code where this class variables are referenced. You said you are setting value of m_key inside your constructor, are you using setKey() to do that or you are doing a direct assignment...
-
I'm doing a direct assignment
-
And how are you accessing this variables in qml side.. are you binding it to another variable and then reading that variable.. if this is the case, direct assignment would not help.
@
MyItem {
id: myItem
}property int myVar : myItem.key
@In the above code, myVar will not be updated to 8 when you directly assign 8 to m_key in C++ code. You will have to use setKey(8) for that to happen. Can you check this.
-
Where should i call the setKey() ? In the ctor?
-
In your constructor, you should be safe, but you do not show us the constructor, so we can't be sure what happens there. Note that you might want to change this:
@
MyItem::MyItem(QObject* _parent):
QObject(_parent)
{
m_key = 8;
}
@to
@
MyItem::MyItem(QObject* _parent):
QObject(_parent),
m_key(8)
{
}
@ -
yes.. try that.
[quote author="Andre" date="1309169104"]Can you trace how you get from the creator code to the key() method then?[/quote]
Like Andre said, can you trace where are you getting this key() call from. Is it coming form qml variable binding??
Pasting some code always helps.. Can you paste your qml code in which you create Myitem class instance and how you are accessing key value ??
-
@MyItem::MyItem(QObject* _parent): QObject(_parent), m_key(8) { }
@this is exactly what i'm doing.
key is a read only property and in my qml file the code is:
@MyItem
{
id: myItem
}Text { id: textElement anchors.fill: parent horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter text: " Hello " font.pointSize: myItem.key }@
-
Interesting problem.. let me make my hands dirty and try this out.
-
Weird. I'd say it is impossible that your key is read before the constructor is finished then. Only a decent trace will show where the issue comes from, I think.
-
works like a charm for me.. copying my code.. can you please check where you are going wrong
myitem.h
@#ifndef MYITEM_H
#define MYITEM_H
#include <QObject>
class MyItem : public QObject
{
Q_OBJECT
Q_PROPERTY(int key READ key WRITE setKey NOTIFY keyChanged)
public:
explicit MyItem(QObject *parent = 0);
const int key() const;
void setKey(const int key);
signals:
void keyChanged();
public slots:
private:
int m_key;};
#endif // MYITEM_H@myitem.cpp
@#include "myitem.h"
MyItem::MyItem(QObject *parent) :
QObject(parent),m_key(24)
{
}
const int MyItem::key() const
{
return m_key;
}
void MyItem::setKey(const int key)
{
m_key = key;
}@main.cpp
@#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QDeclarativeContext>
#include <QtDeclarative>
#include "myitem.h"int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDeclarativeView *viewer = new QDeclarativeView;
qmlRegisterType<MyItem>("MyItemLibrary", 1, 0, "MyItem");
viewer->setSource(QUrl::fromLocalFile("qml/Example/main.qml"));
viewer->setResizeMode(QDeclarativeView::SizeRootObjectToView);
viewer->showFullScreen();
return app.exec();
}@main.qml
@import QtQuick 1.0
import MyItemLibrary 1.0
Rectangle {
id: mainItem
MyItem
{
id: myItem
}
Text
{
id: textElement
anchors.fill: parent
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
text: " Hello "
font.pointSize: myItem.key
}}@
-
key should be a read only property without setkey()
-
is it working? where did you go wrong?