Results 1 to 5 of 5

Thread: QItemEditorFactory and custom enum types.

  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

    Default QItemEditorFactory and custom enum types.

    Hello people

    i'm playing with QItemEditorFactory and creating editor widgets on-the-fly for a dynamic form i have in my application (The form shows editable values of a QVariantHash).
    Regarding to this, i have two questions.

    The first one should be rather trivial for those familiar with the GUI classes. When creating an editor widget with QItemEditorFactory::defaultFactory()->createEditor(), the look-and-feel of the widget looks somehow different than other regular widgets: i suspect these generated widgets are made to be displayed on a table rather than a regular form. What would be the right process to have those widgets skinned as normal?

    The second question (And most important), is about the QVariant type being passed to the factory when creating the widget... i would like to give it a QVariant crafted from an enum of mine (Registered with Q_ENUMS and everything), and to be able to have a QComboBox displaying possible values. Is it possible to have that "out-of-the-box" like it has been done in QtDesigner, or do i have to introspect the values using the MetaObject system?

    [Edit]Another fast question i have... When registering new editors in QItemEditorFactory, i see i have to pass a QVariant::Type value, as well as at instanciation time. What if the editor i want to register is for a custom type, higher than QVariant::UserType? When i will instanciate the editor, how will i be able to pass a QVariant::Type superior than QVariant::UserType?[/Edit]

    Thanks a lot,
    Pierre.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QItemEditorFactory and custom enum types.

    Quote Originally Posted by hickscorp View Post
    The first one should be rather trivial for those familiar with the GUI classes. When creating an editor widget with QItemEditorFactory::defaultFactory()->createEditor(), the look-and-feel of the widget looks somehow different than other regular widgets: i suspect these generated widgets are made to be displayed on a table rather than a regular form. What would be the right process to have those widgets skinned as normal?
    The widgets I get look quite normal.

    The second question (And most important), is about the QVariant type being passed to the factory when creating the widget... i would like to give it a QVariant crafted from an enum of mine (Registered with Q_ENUMS and everything), and to be able to have a QComboBox displaying possible values. Is it possible to have that "out-of-the-box" like it has been done in QtDesigner, or do i have to introspect the values using the MetaObject system?
    The latter, I think.

    [Edit]Another fast question i have... When registering new editors in QItemEditorFactory, i see i have to pass a QVariant::Type value, as well as at instanciation time. What if the editor i want to register is for a custom type, higher than QVariant::UserType? When i will instanciate the editor, how will i be able to pass a QVariant::Type superior than QVariant::UserType?[/Edit]
    Pass the value returned by qRegisterMetaType() for your type. It's likely you forgot to do this step earlier which might have caused your problems described in the other thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    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 and custom enum types.

    Thanks Wysota!

    1) My widgets look kind of "borderless"...
    2) Ok, i have done that for now to test, and it's working pretty good:
    Qt Code:
    1. for (QVariantHash::const_iterator i=_parameters.constBegin(); i!=_parameters.constEnd(); ++i) {
    2. QString const &name = i.key();
    3. QVariant const &val = i.value();
    4.  
    5. int const uTp = val.userType();
    6. QWidget *wgt = 0;
    7. if (uTp>=qMetaTypeId<RE::E::Agressivity>() && uTp<=qMetaTypeId<RE::E::DocumentType>()) {
    8. QComboBox *cmbBx = new QComboBox();
    9. QMetaObject const &mo = RE::E::staticMetaObject;
    10. QString mtName = QMetaType::typeName(uTp);
    11. mtName = mtName.mid(mtName.lastIndexOf(':')+1);
    12. qint32 const iEnum = mo.indexOfEnumerator(mtName.toLatin1());
    13. QMetaEnum const &en = mo.enumerator(iEnum);
    14. quint32 const cEnum = en.keyCount();
    15. for (quint32 i=0; i<cEnum; i++) {
    16. const char *key = en.key(i);
    17. cmbBx->addItem (RE::classNameToNaturalString(key));
    18. }
    19. wgt = cmbBx;
    20. }
    21. else
    22. wgt = QItemEditorFactory::defaultFactory()->createEditor((QVariant::Type)val.userType(), this);
    23. ui.frmMain->addRow (RE::classNameToNaturalString(name), wgt);
    To copy to clipboard, switch view to plain text mode 
    This sample piece of code assumes that all the custom enums are registered all after each other, so their metatype id follow each other (Hence the range check at the very begining of the code, to know if a given type ID is an enum or not).
    Later i'll subclass the factory to allow creation of the widget more "cleanly"

    3) No... i did everything required at compile time and at run time to register my types. The solution i have found is to not put my enums directly in my "fake emulated" namespace, but within a class in that namespace.

    Thanks for your answers!
    Pierre.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QItemEditorFactory and custom enum types.

    As I said in the other thread, moc treats the "Qt" namespace in a special way. And it's not releated to Q_MOC_RUN (at least that's not enough). There is a clause in moc source code related to the namespace. You can see that in Generator::generateCode() in src/tools/moc/generator.cpp (and likely elsewhere too).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    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 and custom enum types.

    Thank you very much wysota, you just pointed exactly why Trolltech had made the Qt namespace an exception for MOC. It's now obvious to me that the Q_GADGET / Q_MOC_RUN isnt enough. i'll stick to declaring my enums in a class named "RE::E".

Similar Threads

  1. show custom enum property in designer?
    By qlands in forum Qt Programming
    Replies: 0
    Last Post: 5th July 2011, 17:12
  2. Q_PROPERTY and deeply nested custom types
    By Sammy D'Souza in forum Newbie
    Replies: 1
    Last Post: 15th May 2010, 11:26
  3. Working with QDBus and custom types
    By JuBe in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2009, 16:20
  4. QItemEditorFactory and custom Model
    By maximAL in forum Qt Programming
    Replies: 5
    Last Post: 26th November 2007, 13:45
  5. Store an custom enum in QSettings
    By Lykurg in forum Qt Programming
    Replies: 1
    Last Post: 14th March 2007, 20:06

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.