Results 1 to 2 of 2

Thread: QItemEditorFactory for QList.

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

    Question QItemEditorFactory for QList.

    Hello everyone,

    i'm facing a problem for which i'm not finding any solution... i've tried pretty much everything, but i believe i may not understand something in the required logic here.
    My project has reached a certain complexity point "code" speaking, and it's hard to provide all the relevant source code since everything depends on a lot of other stuff.

    Basically, i have defined custom types:
    Qt Code:
    1. // File is being Moc'ed.
    2.  
    3. namespace REA {
    4. class IntList : public QList<qint32> {};
    5. class UIntList : public QList<quint32> {};
    6. class RgbList : public QList<QRgb> {};
    7. class StringList : public QStringList {};
    8. class CharList : public QList<QChar> {};
    9. class RgbToUIntHash : public QHash<QRgb,quint32> {};
    10. class StringToStringHash : public QHash<QString,QString> {};
    11. class StringToImageHash : public QHash<QString,QImage> {};
    12. // And a lot more types...
    13.  
    14. // ------------------------------------- Rectangles ------------------------------------- //
    15. // Rectangle.
    16. class Rect : public QRect {
    17. public:
    18. // Members.
    19. bool isRelative;
    20. // Constructors.
    21. Rect (QPoint const &p=QPoint(0,0), QSize const &s=QSize(0,0), bool r=false)
    22. : QRect(p, s), isRelative(r) {};
    23. // QDataStream friend serialization stuff.
    24. friend QDataStream& operator<< (QDataStream& s, Rect const &r) { return s << *(QRect*)&r << r.isRelative; };
    25. friend QDataStream& operator>> (QDataStream& s, Rect &r) { return s >> *(QRect*)&r >> r.isRelative; };
    26. };
    27. // Float rectangle.
    28. class RectF : public QRectF {
    29. public:
    30. // Members.
    31. bool isRelative;
    32. // Constructors.
    33. RectF (QPointF const &p=QPointF(0,0), QSizeF const &s=QSizeF(0,0), bool r=false)
    34. : QRectF(p, s), isRelative(r) {};
    35. // QDataStream friend serialization stuff.
    36. friend QDataStream& operator<< (QDataStream& s, RectF const &r) { return s << *(QRectF*)&r << r.isRelative; };
    37. friend QDataStream& operator>> (QDataStream& s, RectF &r) { return s >> *(QRectF*)&r >> r.isRelative; };
    38. };
    39. // And a lot more custom types...
    40. };
    41. Q_DECLARE_METATYPE(REA::IntList);
    42. Q_DECLARE_METATYPE(REA::UIntList);
    43. Q_DECLARE_METATYPE(REA::RgbList);
    44. Q_DECLARE_METATYPE(REA::StringList);
    45. Q_DECLARE_METATYPE(REA::CharList);
    46. Q_DECLARE_METATYPE(REA::RgbToUIntHash);
    47. Q_DECLARE_METATYPE(REA::StringToStringHash);
    48. Q_DECLARE_METATYPE(REA::StringToImageHash);
    49. Q_DECLARE_METATYPE(REA::Rect);
    50. Q_DECLARE_METATYPE(REA::RectF);
    51. // And the same for any other type...
    To copy to clipboard, switch view to plain text mode 

    Then at runtime, i register everything:
    Qt Code:
    1. #define RegisterStreamOperators(name) qRegisterMetaTypeStreamOperators<name>(#name)
    2. // Register lists / maps / hashes meta-types for streaming.
    3. RegisterStreamOperators(REA::IntList);
    4. RegisterStreamOperators(REA::UIntList);
    5. RegisterStreamOperators(REA::RgbList);
    6. RegisterStreamOperators(REA::StringList);
    7. RegisterStreamOperators(REA::CharList);
    8. RegisterStreamOperators(REA::RgbToUIntHash);
    9. RegisterStreamOperators(REA::StringToStringHash);
    10. RegisterStreamOperators(REA::StringToImageHash);
    11. RegisterStreamOperators(REA::Rect);
    12. RegisterStreamOperators(REA::RectF);
    13. #undef RegisterStreamOperators
    To copy to clipboard, switch view to plain text mode 

    Then, i have this very special class called "Variable", which acts a very similar way as QVariant does (It actually uses a QVariant internally). The Variable class allows me to define an i/o direction for a value, a name, and defines streaming operators to save / load lists of variables.

    Then on my GUI project, i have subclassed QItemEditorFactory to allow me to dynamically instantiate editors based on the type of variable being edited:
    Qt Code:
    1. #include "CustomItemEditorFactory.h"
    2. // Editor classes.
    3. #include "REATypes.h"
    4. // Basic types.
    5. #include <QLineEdit>
    6. // Custom types.
    7. #include "WgtBoolEditor.h"
    8. #include "WgtPointEditor.h"
    9. #include "WgtPointFEditor.h"
    10. #include "WgtSizeEditor.h"
    11. #include "WgtSizeFEditor.h"
    12. #include "WgtRectEditor.h"
    13. #include "WgtRectFEditor.h"
    14. #include "WgtWFNodeUidEditor.h"
    15. #include "WgtMemberPicker.h"
    16.  
    17. using namespace REA;
    18.  
    19. CustomItemEditorFactory::CustomItemEditorFactory (const QItemEditorFactory* standardFactory)
    20. : QItemEditorFactory(), _standardFactory(standardFactory) {
    21. }
    22.  
    23. QWidget* CustomItemEditorFactory::createEditor (QVariant::Type type, QWidget* parent) const {
    24. switch (type) {
    25. case QVariant::String: return new QLineEdit(parent);
    26. case QVariant::Bool: return new WgtBoolEditor(parent);
    27. case QVariant::Point: return new WgtPointEditor(parent);
    28. case QVariant::PointF: return new WgtPointFEditor(parent);
    29. case QVariant::Size: return new WgtSizeEditor(parent);
    30. case QVariant::SizeF: return new WgtSizeEditor(parent);
    31. default:
    32. if (type==qMetaTypeId<REA::Rect>()) return new WgtRectEditor(parent);
    33. else if (type==qMetaTypeId<REA::RectF>()) return new WgtRectFEditor(parent);
    34. else if (type==qMetaTypeId<REA::UniqueIdentifier>()) return new WgtWFNodeUidEditor(parent);
    35. else if (type==qMetaTypeId<REA::MemberPath>()) return new WgtMemberPicker(parent);
    36. else return _standardFactory->createEditor(type, parent);
    37. };
    38. }
    39.  
    40. QByteArray CustomItemEditorFactory::valuePropertyName (QVariant::Type type) const {
    41. // Any custom type...
    42. if ( type==QVariant::Bool ||
    43. type==QVariant::Point || type==QVariant::PointF ||
    44. type==QVariant::Size || type==QVariant::SizeF ||
    45. type>=QVariant::UserType)
    46. return "value";
    47. else
    48. return _standardFactory->valuePropertyName(type);
    49. }
    50.  
    51. void CustomItemEditorFactory::installAsStandardFactory () {
    52. QItemEditorFactory::setDefaultFactory(new CustomItemEditorFactory(QItemEditorFactory::defaultFactory()));
    53. }
    To copy to clipboard, switch view to plain text mode 

    All this works flawlessly, i have no problem doing something like:
    Qt Code:
    1. WgtVariableEditor *wgt = new WgtVariableEditor(this, var);
    To copy to clipboard, switch view to plain text mode 
    The Variable editor widget important part looks like that:
    Qt Code:
    1. WgtVariableEditor::WgtVariableEditor (QWidget *parent, Variable const &var)
    2. : QWidget(parent, 0), _widget(0), _variable(var) {
    3. // Create and set layout for this widget.
    4. QHBoxLayout *lyt = new QHBoxLayout(this);
    5. lyt->setSpacing(0);
    6. lyt->setMargin(0);
    7. setLayout(lyt);
    8.  
    9. // Retrieve editor widget object.
    10. QItemEditorFactory const *f = QItemEditorFactory::defaultFactory();
    11.  
    12. // Create the widget and store it's metaobject.
    13. _widget = f->createEditor((QVariant::Type)_variable.value.userType(), this);
    14. lyt->addWidget(_widget);
    15.  
    16. // Retrieve metaobject.
    17. QMetaObject const *mo = _widget->metaObject();
    18. // Retrieve value property name.
    19. QByteArray vpName = f->valuePropertyName((QVariant::Type)_variable.value.userType());
    20. // Set widget value.
    21. _widget->setProperty(vpName, _variable.value);
    22. // Get property index.
    23. int pIndex = mo->indexOfProperty(vpName);
    24. // Retrieve metamethod for notification signal.
    25. QMetaMethod signal = mo->property(pIndex).notifySignal();
    26. // Finally generate signal name.
    27. QString sName = QString("2%1").arg(signal.signature());
    28. connect(_widget, sName.toLatin1(), this, SLOT(onValueEdition()));
    29. }
    To copy to clipboard, switch view to plain text mode 

    Now, my problem is when it comes to editing lists and my QList-based custom types (REA::UIntList, REA::StringList, etc). i have no idea how to proceed. i first have created a template class (WgtListEditor), but Qt doesn't allow to subclass QObject (Nor QWidget, logic) in a template class...

    i have tried then many and many other solutions... None of them works, and i got lost in the Qt editor logic.

    Can someone please provide me with serious advices, and if possible with a diagram or some pseudo-code based on how my objects are made?

    Thank you very much for your time, i appreciate any kind of help you could provide.
    Pierre.

    [EDIT]i notived my code was complettely messed up with the tabulations and everything... i recomend you switch to "plain" view of code, and copy paste into your favorite editor for a better anc clear view [/EDIT]

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

    Default Re: QItemEditorFactory for QList.

    Bump... Anyone?

Similar Threads

  1. Replies: 7
    Last Post: 15th May 2014, 19:29
  2. Replies: 4
    Last Post: 20th August 2010, 13:54
  3. How to connect signal/slot in QItemEditorFactory?
    By yyalli in forum Qt Programming
    Replies: 1
    Last Post: 4th June 2010, 14:56
  4. Q_ENUMS, QItemEditorFactory
    By Kunchok in forum Qt Programming
    Replies: 8
    Last Post: 21st January 2008, 12:20
  5. QItemEditorFactory and custom Model
    By maximAL in forum Qt Programming
    Replies: 5
    Last Post: 26th November 2007, 13:45

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
  •  
Qt is a trademark of The Qt Company.