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. [SOLVED] Custom QVariant crash on drag in QTreeView
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Custom QVariant crash on drag in QTreeView

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 5.5k Views 1 Watching
  • 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
    marcus.fr
    wrote on last edited by
    #1

    Hi

    I use QStandardItemModel in a QTreeView. On QStandardItem(s) I set QVariant as a pointer to a custom class.

    These are the alternatives I tried:

    #1 Using void *
    @ptrQStandardItem->setData( QVariant::fromValue(static_cast<void*>(ptrMyClass)) , Qt::UserRole+MyRole);@

    #2 Using custom type registration
    @
    Q_DECLARE_METATYPE(MyClass*) //right below the class declaration
    qRegisterMetaType<MyClass*>("MyClass*"); //in main

    pxQStandardItem->setData( QVariant::fromValue(ptrMyClass) , Qt::UserRole+MyRole);
    @

    In both cases, when I just start to drag one item I get crash with debug assert message: "Invalid type to save". When I take out the ->setData line it works without problem. I don't even get the value from that QVariant anywhere in the code, and still have the crash.

    I looked the forum but the solutions are mainly related to defining stream operators, but that is the case when object is used as custom QVariant type, not pointer. I don't need to use qRegisterMetaTypeStreamOperators() for a pointer either. Why do I get this error?

    Thanks!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexisdm
      wrote on last edited by
      #2

      Draging an item causes the QStandardItem object to be serialized into the QByteArray data member of a QMimeData, which means you need to define and register the stream operators for MyClass*.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        marcus.fr
        wrote on last edited by
        #3

        [quote author="alexisdm" date="1353957735"]Draging an item causes the QStandardItem object to be serialized into the QByteArray data member of a QMimeData, which means you need to define and register the stream operators for MyClass*.[/quote]

        Can you please show me an example? Thanks a lot.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alexisdm
          wrote on last edited by
          #4

          If the item is only moved within the application, you could write its address in the QDataStream:
          @QDataStream &operator<<(QDataStream &out, MyClass * const &rhs) {
          out.writeRawData(reinterpret_cast<const char*>(&rhs), sizeof(rhs));
          return out;
          }

          QDataStream & operator >> (QDataStream &in, MyClass &rhs) {
          in.readRawData(reinterpret_cast<char
          >(&rhs), sizeof(rhs));
          return in;
          }

          // With that line in main
          qRegisterMetaTypeStreamOperators<MyClass*>("MyClass*");@

          1 Reply Last reply
          0
          • M Offline
            M Offline
            marcus.fr
            wrote on last edited by
            #5

            Perfect, thanks!!!

            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