C++ Qml ctor problem
-
@
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
-
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...
-
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.
-
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.
-
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
}}@
-
is it working? where did you go wrong?