Fixed size array as Qt Property?



  • Hi -

    I'd like to make an array of 10 characters as a Q_PROPERTY in my class. How would I go about doing it? I tried the obvious:

    @
    Q_PROPERTY(quint8[10] fcfBcdTime READ getFcfBcdTime WRITE setFcfBcdTime)
    @

    which of course the compiler hated. Tried the googles, forum search but didn't come across any good info.



  • Not tried, just brain to keyboard: typedef + Q_DECLARE_METATYPE it?



  • typedef does not work, it's even impossible to create the typedef at first.

    bq. From Q_DECLARE_METATYPE docs:
    This macro makes the type Type known to QMetaType as long as it provides a public default constructor, a public copy constructor and a public destructor. It is needed to use the type Type as a custom type in QVariant.

    As that type does not have any of these methods/constructors, it cannot be declared as a meta type either.

    You can put it into a tiny wrapper class that provides the necessary code.

    I personally would consider [[Doc:QByteArray]] as an alternative.



  • Yeah, what I ended up doing was using QByte array and in the constructor of the class using resize() to set it to the size I want. Not exactly what I wanted, but I think it will work.

    M



  • Wow, I'm getting a seg fault when trying to access the property. My code looks like this:

    @
    Q_PROPERTY(QByteArray fcfBcdTime READ getFcfBcdTime WRITE setFcfBcdTime)

    QByteArray fcfBcdTime;

    QByteArray getFcfBcdTime() { return fcfBcdTime; }

    void setFcfBcdTime(QByteArray newBcdTime) { fcfBcdTime = newBcdTime; }

    @

    All inside of a class, of course. The problem comes when I try to access the property later via this:

    @
    QVariant currentValue = this->property(pMetaObj->property(i).name());
    @

    Where i is some number in a for loop. Turns out getFcfBcdTime is what causes the seg fault, down in qatomic_i386.h

    Any thoughts?



  • Well, the most obvious reason is that <code>i</code> is out of bounds.

    Side question: is there any reason it isn't <code>void setFcfBcdTime(const QByteArray &newBcdTime)</code> and <code>QByteArray getFcfBcdTime() const</code>?



  • no, i is not out of bounds. It is specifically the QByteArray. All my other properties which are basic types (quin8, quin16, etc) work just fine, but when I added the QByteArray it blows up. The loop code looks like this:

    @
    //Iterate over properties and byte swap, shove in array
    const QMetaObject* pMetaObj = this->metaObject();

    quint32 propertyCount = pMetaObj->propertyCount();

    //Start at 1 - skipping the class name property (from QObject)
    for(quint32 i = 1; i < propertyCount; i++)
    {
    QVariant currentValue = this->property(pMetaObj->property(i).name());

                      .... do stuff
    

    }
    @



  • A good try when falling over weird errors is to completely rebuild your project. Sometimes one has leftovers from old code that behave erratic.

    If it still crashes, you're dealing with some other error - probably in the "do stuff" part. The pasted code works in principle.



  • Unbelievable.... a clean/build did it. Sheesh. :)



  • Sometimes, for whatever reason, some of the autogenerated files (moc, for example) are not update on a new build. By cleaning the project and rebuilding completely one make sure that those are generated from scratch and resemble the current state of your actual source files.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.