C++ signal and custom type variable use in QML
I have created a custom type inheriting from QObject.
I have declared the type with Q_DECLARE_METATYPE() with qmlDeclareType() and also with qRegisterMetaType().
From another object declared as a component in QML I emit a signal which has the type as a parameter now when the signal is handled from the QML I cannot access any of the properties of the type.
In debug output while debugging
Error: Cannot assign [undefined] to QString
is shown whenever I try to access any property of the variable and
Error: <variableName> is not defined
is shown.
my signal is declared as follows:
[CODE]signals:
void readTagBCard(const BCard& tagc);[/CODE]
and handled in qml as follows:
[CODE]onReadTagBCard:{
txtFullName.text = tagc.lastName;
}[/CODE]
the method is lastName is declared as follows:
[CODE]QString lastName() const {return mlastName;};[/CODE]
How can I access methods on custom type variables in QML?
Re: C++ signal and custom type variable use in QML
[QUOTE]Error: Cannot assign [undefined] to QString[/QUOTE]
I got this error whenever I tried assign something by object and not by object's id.
I.e. I have[CODE] MyComponent{ id: mySomething //with many non visible properties} [/CODE]and I tried to use it like in this:
[CODE]Text{id: sometext; text: "I want to see my class property of QString type. Here it is: " + MyComponent.string_property}
[/CODE] It generates an error and property is undefined.
It should be done so:
[CODE]
MyComponent{ id: mySomething} //first initialize instance of class :)
Text{ id: sometext
text: mySomething.string_property
...
}
[/CODE]
Does it helps?
Regards, Andrzej
Re: C++ signal and custom type variable use in QML
My problem is a little bit different since I don't have the component declared in QML. What I am trying to do is to use properties of a class instance sent in signal without declaring it in the QML.
Re: C++ signal and custom type variable use in QML
ok, but i think this undefined value can be essential to find what happens. let us ask a question - why it is undefined?
if your type es really registered it can be instanced in any way and here seems it isn't
I am non sure what are you working on, but perhaps this links will help: [URL="http://www.google.pl/url?sa=t&source=web&cd=2&ved=0CBwQFjAB&url=http%3A%2F%2Flists.trolltech.com%2Fqt-interest%2F2005-10%2Fthread00784-0.html&rct=j&q=qregistermetatype&ei=Ol3WTemvD4_pOeD5rKwH&usg=AFQjCNFPVQPMfTOkKFxn0Z1E406K-QDHjQ&cad=rja"]http://www.google.pl/url?sa=t&source=web&cd=2&ved=0CBwQFjAB&url=http%3A%2F%2Flists.trolltech.com%2Fqt-interest%2F2005-10%2Fthread00784-0.html&rct=j&q=qregistermetatype&ei=Ol3WTemvD4_pOeD5rKwH&usg=AFQjCNFPVQPMfTOkKFxn0Z1E406K-QDHjQ&cad=rja[/URL]
[URL="http://www.google.pl/url?sa=t&source=web&cd=3&ved=0CC4QFjAC&url=http%3A%2F%2Fwww.qtforum.org%2Farticle%2F24051%2Fqregistermetatype-howto.html&rct=j&q=qregistermetatype&ei=Ol3WTemvD4_pOeD5rKwH&usg=AFQjCNEYv7cPyeMpE76OF9TVaZnJCfroaA&cad=rja"]http://www.google.pl/url?sa=t&source=web&cd=3&ved=0CC4QFjAC&url=http%3A%2F%2Fwww.qtforum.org%2Farticle%2F24051%2Fqregistermetatype-howto.html&rct=j&q=qregistermetatype&ei=Ol3WTemvD4_pOeD5rKwH&usg=AFQjCNEYv7cPyeMpE76OF9TVaZnJCfroaA&cad=rja[/URL]
Re: C++ signal and custom type variable use in QML
Thanks for the links but I still can not get it to work... While playing around with it I am now getting the following error
[CODE]Result of expression 'nfcTagBCard.firstName' [undefined] is not a function.[/CODE]
In my class definition the result of the call to firstName method is a QString and it is declared as a slot so it should be visible from the QML.
Re: C++ signal and custom type variable use in QML
Stupid question: Did you use the [URL="http://doc.qt.nokia.com/4.7/qobject.html#Q_OBJECT"]Q_OBJECT[/URL] macro in your class's H file?
Re: C++ signal and custom type variable use in QML
let us see some piece of code
Re: C++ signal and custom type variable use in QML
Hello, I've exactly the same problem than penguin_chris. I have created a custom type inheriting from QObject and this type is instanciated and emitted from the c++ part in a queued connection
No problem to receive the signal in a c++ slot ; In this case I'm able to read properties of the custom type. However in QML I receive the signal but when I want to read the custom type's properties it returns "undefined"
Just below the code snippet :
// .h : custom types
class EqtEventList : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name)
//number of events in the list
Q_PROPERTY(int eventscount READ eventscount)
//list property containing file names as QString
Q_PROPERTY(QDeclarativeListProperty<EqtEvent> events READ events CONSTANT )
public:
EqtEventList();
EqtEventList(const EqtEventList &eqtEventList);
~EqtEventList();
//properties' read functions
QString name() const { return QString("tutu");};
int eventscount() const;
QDeclarativeListProperty<EqtEvent> events();
QList<QSharedPointer<EqtEvent>> m_eqtEventList;
};
Q_DECLARE_METATYPE(EqtEvent) // EqtEvent class definition is not included in the code snippet, but it is a class which inherits from QObject, like EqtEventList
Q_DECLARE_METATYPE(EqtEventList)
------------------------------------------------
// main.cpp :
qmlRegisterType<EqtEvent>("Events",1,0,"EqtEvent");
qmlRegisterType<EqtEventList>("Events", 1, 0, "EqtEventList");
qRegisterMetaType<EqtEvent>("EqtEvent");
qRegisterMetaType<EqtEventList>("EqtEventList");
------------------------------------------------
// interface.h : abstract class, implementation of virtual methods are done in derived class, (signals are emitted in CEqtProxySoap)
class IEqtProxy : public QObject
{
Q_OBJECT
signals:
void eventsReceived(const EqtEventList eventlist);
....
------------------------------------------------
// eqtProxySoap.cpp
void CEqtProxySoap::notifyEventsReceived(
const EqtEventList eventlist)
{
emit eventsReceived(eventlist);
}
------------------------------------------------
// another.cpp : eventsReceived is not emitted in the GUI thread, so it is a queued connection
EqtEventList eqtEventList;
eqtEventList.m_eqtEventList.append(QSharedPointer<EqtEvent>(new EqtEvent(QString("Test"), QDateTime::currentTime(), QString("1")));
TheEqtInterface.notifyEventsReceived(eqtEventList);
------------------------------------------------
// test.qml
import QtQuick 1.0
import Events 1.0
import "content"
Rectangle {
id: window
width: 600; height: 800
color: "#d9d3b5"
Column {
Text {
id: test
text: "This is a test"
Connections {
target: eqtproxy
onEventsReceived: {
console.log("Received c++ signal : eventlist.name = " + eventlist.name)
}
}
}
}
}
------------------------------------------------
Re: C++ signal and custom type variable use in QML
I you have an idea it would be great ! I'm stucked on that topic for 2 days now...
Thanks
Re: C++ signal and custom type variable use in QML
[QUOTE=danhicksbyron;843024]Stupid question: Did you use the [URL="http://doc.qt.nokia.com/4.7/qobject.html#Q_OBJECT"]Q_OBJECT[/URL] macro in your class's H file?[/QUOTE]
Yes, the macro is there
@akrynski: I will post some code later when I have some time to reopen that project...