Results 1 to 2 of 2

Thread: Q_DECLARE_METATYPE() questions.

  1. #1
    Join Date
    Dec 2006
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    MacOS X Unix/X11 Windows

    Default Q_DECLARE_METATYPE() questions.


    i have a question related to QVariant's user types. As i may have already said in a previous post, i'm implementing a derived class from "QItemEditorFactory" so it can handle my own types, and also for some built-in types (QRect/F, QPoint/F, etc).

    In my factory, i use a switch to compare the given type (in createEditor(QVariant::Type, QWidget *parent)) to types built-into QVariant (QRect/F, QPoint/F).

    But i don't understand how i am supposed to compare the given argument (Type) to know wether it's one of my own types because it's always UserType in the case of a specific user type item (But no detail on what user type)... i'm a bit lost into the QVariant typing thingy.

    As far as i understand (And since i'm calling myself createEditor on the default installed item editor factory, i may want to pass my very specific custom type (UserType+1, UserType+2), or something?


  2. #2
    Join Date
    Dec 2006
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    MacOS X Unix/X11 Windows

    Default Re: Q_DECLARE_METATYPE() questions.

    Just some precisions.... For now, to allow my editor factory to work, i'm doing like this:
    Qt Code:
    1. QItemEditorFactory const *f = QItemEditorFactory::defaultFactory();
    2. _widget = f->createEditor((QVariant::Type)_param.value.userType(), this);
    3. QByteArray vpName = f->valuePropertyName((QVariant::Type)_param.value.userType());
    To copy to clipboard, switch view to plain text mode 

    Then, in my QItemEditorFactory subclass:
    Qt Code:
    1. QWidget* CustomItemEditorFactory::createEditor (QVariant::Type type, QWidget* parent) const {
    2. QWidget *ed;
    3. // Custom boolean editor.
    4. if (type==QVariant::Bool)
    5. ed = new WgtBoolEditor(parent);
    6. // Custom QPoint editor.
    7. else if (type==QVariant::Point)
    8. ed = new WgtPointEditor(false, INT_MIN, INT_MAX, parent);
    9. // Custom QPointF editor.
    10. else if (type==QVariant::PointF)
    11. ed = new WgtPointEditor(true, LONG_MIN, LONG_MAX, parent);
    13. // Custom RelativeRect editor.
    14. else if (type==qMetaTypeId<REA::RelativeRect>())
    15. ed = new WgtRelativeRectEditor(false, INT_MIN, INT_MAX, parent);
    16. // Custom RelativeRectF editor.
    17. else if (type==qMetaTypeId<REA::RelativeRectF>())
    18. ed = new WgtRelativeRectEditor(true, INT_MIN, INT_MAX, parent);
    20. else
    21. ed = _standardFactory->createEditor(type, parent);
    22. return ed;
    23. }
    25. QByteArray CustomItemEditorFactory::valuePropertyName (QVariant::Type type) const {
    26. if (type==QVariant::Bool || type==QVariant::Point || type==QVariant::PointF)
    27. return "value";
    28. else
    29. return _standardFactory->valuePropertyName(type);
    30. }
    To copy to clipboard, switch view to plain text mode 

    Is there any better way to do that (Without casting some ints into QVariant::Type)? Actually, this works pretty good... But i'm not sure if it's the only way to achieve correctly what i'm trying to do... Without being able to get my custom types detailed as QVariant::Type from my custom factory, i really don't see the point of registering custom types.


    [EDIT]And for the records, here is my custom types declaration:
    Qt Code:
    1. #ifndef REATypes_h
    2. #define REATypes_h
    4. #include <QRect>
    5. #include <QRectF>
    7. namespace REA {
    9. // Unsigned integers list.
    10. typedef QList<unsigned int> UIntList;
    11. // Signed integers list.
    12. typedef QList<int> IntList;
    13. // Color to unsigned int hash.
    14. typedef QHash<QRgb,unsigned int> ColorToUIntHash;
    16. // Relative rect class.
    17. class RelativeRect : public QRect {};
    18. // Relative float rect class.
    19. class RelativeRectF : public QRectF {};
    21. };
    23. // Declare meta-types.
    26. Q_DECLARE_METATYPE(REA::ColorToUIntHash);
    27. Q_DECLARE_METATYPE(REA::RelativeRect);
    28. Q_DECLARE_METATYPE(REA::RelativeRectF);
    30. #endif
    To copy to clipboard, switch view to plain text mode 
    Last edited by hickscorp; 29th June 2010 at 16:08.

Similar Threads

  1. Replies: 11
    Last Post: 24th November 2009, 00:30
  2. Problem to declare a stl map - Q_DECLARE_METATYPE
    By Althor in forum Qt Programming
    Replies: 5
    Last Post: 9th December 2008, 09:41
  3. How to use Q_DECLARE_METATYPE
    By lni in forum Qt Programming
    Replies: 6
    Last Post: 21st July 2008, 17:13
  4. Q_DECLARE_METATYPE and qVariantCanConvert
    By manojmka in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2008, 06:13
  5. Replies: 2
    Last Post: 12th December 2007, 13:15


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.