×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Athens, Greece
    Posts
    4

    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);
    and handled in qml as follows:

    Code:
    onReadTagBCard:{            
                txtFullName.text = tagc.lastName;
            }
    the method is lastName is declared as follows:

    Code:
    QString lastName() const {return mlastName;};
    How can I access methods on custom type variables in QML?
    Last edited by penguin_chris; 2011-05-20 at 11:25.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    17

    Re: C++ signal and custom type variable use in QML

    Error: Cannot assign [undefined] to QString
    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}
    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}
    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
    ...
    }
    Does it helps?
    Regards, Andrzej

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Athens, Greece
    Posts
    4

    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.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    17

    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: http://www.google.pl/url?sa=t&source...-QDHjQ&cad=rja
    http://www.google.pl/url?sa=t&source...CfroaA&cad=rja
    Last edited by akrynski; 2011-05-20 at 13:39.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Athens, Greece
    Posts
    4

    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.
    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.

  6. #6
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: C++ signal and custom type variable use in QML

    Stupid question: Did you use the Q_OBJECT macro in your class's H file?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    17

    Re: C++ signal and custom type variable use in QML

    let us see some piece of code

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    2

    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)
    }
    }
    }
    }
    }

    ------------------------------------------------

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    2

    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

  10. #10
    Registered User
    Join Date
    Aug 2010
    Location
    Athens, Greece
    Posts
    4

    Re: C++ signal and custom type variable use in QML

    Quote Originally Posted by danhicksbyron View Post
    Stupid question: Did you use the Q_OBJECT macro in your class's H file?
    Yes, the macro is there

    @akrynski: I will post some code later when I have some time to reopen that project...

Similar Threads

  1. QML: disconnect signal object from all slots from QML
    By semlanik in forum [Archived] Qt Quick
    Replies: 1
    Last Post: 2012-03-07, 02:39
  2. How to pass and use extern variable in thread
    By sudeepa2005 in forum Open C/C++
    Replies: 1
    Last Post: 2009-07-06, 10:22
  3. Replies: 1
    Last Post: 2008-05-10, 10:13
  4. Signal Strength and Type
    By Osprey in forum Symbian
    Replies: 4
    Last Post: 2006-09-22, 12:45

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×