Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Fixed size array as Qt Property?
QtWS25 Last Chance

Fixed size array as Qt Property?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 8.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    medvedm
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

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

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        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.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • M Offline
          M Offline
          medvedm
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            medvedm
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                medvedm
                wrote on last edited by
                #7

                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
                

                }
                @

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  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.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    medvedm
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #10

                      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.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved