Results 1 to 5 of 5

Thread: Meta Types and QVariant

  1. #1
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Meta Types and QVariant

    I'm trying to implement my own model/delegate where the model returns pointers to a QHash<QString, QString> *. I've called Q_DECLARE_METATYPE in order to register the new type and have also called qRegisterMetaType<MyType>("MyType"). Unfortunately, when I try to convert the call to index.data(Qt:isplayType) into <QHash<QString, QString> *, all I seem to get back is a null pointer...

    Qt Code:
    1. typedef QHash<QString, QString>* MyType;
    2. Q_DECLARE_METATYPE(MyType)
    3.  
    4. class MyDelegate : public QItemDelegate
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. MyDelegate(QWidget *parent = 0);
    10.  
    11. virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
    12. const QModelIndex &index) const;
    13. };
    To copy to clipboard, switch view to plain text mode 

    So the declaration above helped me get the code to compile.

    Qt Code:
    1. MyDelegate::MyDelegate(QWidget *parent)
    2. :QItemDelegate(parent)
    3. {
    4. qRegisterMetaType<MyType>("MyType");
    5. }
    6.  
    7. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    8. const QModelIndex &index) const
    9. {
    10. QRect rect = option.rect;
    11.  
    12. QHash<QString, QString> * t = 0;
    13. t = qVariantValue<QHash<QString, QString>*>(index.data(Qt::DisplayRole));
    14. if(t != 0)
    15. {
    16. std::cout << "T size: " << t->size() << std::endl;
    17. }
    18.  
    19. if(option.state & QStyle::State_Selected)
    20. {
    21. painter->fillRect(rect, option.palette.highlight());
    22. }
    23. else
    24. {
    25. QItemDelegate::paint(painter, option, index);
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    Now something is going wrong on lines 12 and 13. Perhaps something about my data declaration below is incorrect? Of course I'm expecting that I should now have a handle on that index in the model. If I did, then I would proceed to format everything in the paint call.

    Qt Code:
    1. QVariant CustomModel::data(const QModelIndex &index, int role)const
    2. {
    3. if(!index.isValid())
    4. {
    5. return QVariant();
    6. }
    7. if(role == Qt::TextAlignmentRole)
    8. {
    9. return int(Qt::AlignRight | Qt::AlignVCenter);
    10. }
    11. else if (role == Qt::DisplayRole)
    12. {
    13. //Returning a pointer to the QHash<QString, QString> so just
    14. //cast back and then use the data in the view.
    15. return tableData.at(index.row());
    16. }
    17. return QVariant();
    18. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for taking your time

  2. #2
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Meta Types and QVariant

    Ok I have it working now. I don't know that anyone else really tries to do this sort of thing, but I finally have it working. Signals and slots might cause me some problems, but I have yet to get that far in the design.

    Anyway this is what I did:

    1) I changed the MetaType definition so that it is no longer a pointer.
    2) I moved the Q_DECLARE_METATYPE to a header file that I can include in any header files that need to access the type.
    3) After a bit of difficulty I found that the only way that I can return my special typedef as a QVariant is to use QVariant::setValue(const T &value).

    As a side note, I'm storing several hashtables in a QList (tableData).

    Qt Code:
    1. #ifndef MYTYPEDEF_H
    2. #define MYTYPEDEF_H
    3.  
    4. typedef QHash<QString, QString> MyType;
    5. Q_DECLARE_METATYPE(MyType)
    6.  
    7. #endif
    To copy to clipboard, switch view to plain text mode 

    The header with the typedef.

    Qt Code:
    1. QVariant CustomModel::data(const QModelIndex &index, int role)const
    2. {
    3. if(!index.isValid())
    4. {
    5. return QVariant();
    6. }
    7. if(role == Qt::TextAlignmentRole)
    8. {
    9. return int(Qt::AlignRight | Qt::AlignVCenter);
    10. }
    11. else if (role == Qt::DisplayRole)
    12. {
    13. //Return the QHash as a QVariant:
    14. const QHash<QString, QString> temp(tableData.at(index.row()));
    15. QVariant var;
    16. var.setValue(temp);
    17. return var;
    18. }
    19. return QVariant();
    20. }
    To copy to clipboard, switch view to plain text mode 

    Returning the QVariant, which contains the QHash<QString, QString>. As I understand it, this is only possible by using Q_DECLARE_METATYPE.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Meta Types and QVariant

    Maybe you could have substituted the hash with a map? Then you could have used QVariantMap which fits into QVariant by default.

  4. #4
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Meta Types and QVariant

    Wysota,

    Thanks for the suggestion. I didn't notice QVariantMap.

    Out of curiosity and only if you have the time, how long have you been a programmer? Are you a professional or is this just a hobby?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Meta Types and QVariant

    Hmm... let's see... Depends what you mean by a "programmer".
    I wrote my first program probably about 16 years ago, it must have been written in BASIC for an Atari microcomputer, but it was just playing around.

    I started some "real" programming probably about 12 years ago when I started learning Pascal and C. Then came other languages, my software eng. (and telecom) studies and currently I'm a PhD student at Warsaw University of Technology. I have a professional education, but obviously it's also my hobby. I wouldn't be sitting all day here otherwise regarded that nobody is paying me to do it


    Some of it is mentioned here: http://www.linkedin.com/in/wysota

    Oh, I started playing with Qt during my studies, about 3 years ago. Just look at my registration date at QtForum.org, it was just about then. Oh... and my first Qt application was a complete failure It was my university project from operating systems course that used threads and tried to manipulate widgets from non-gui threads. Furthermore I needed to paint rectangles, but I didn't know how to do it so I used QLabels instead. As I said - complete failure, it was hanging all the time and I didn't know why.

Similar Threads

  1. QVariant::toString and custom data types
    By Vladimir in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2007, 15:36
  2. Use QVariant with custon data types
    By mcosta in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 14:55
  3. QVariant types support for custom properties
    By Dusdan in forum Qt Tools
    Replies: 9
    Last Post: 11th January 2006, 09:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.