Results 1 to 8 of 8

Thread: Adding an editor to the already existing list of QItemEditorFactory

  1. #1
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Adding an editor to the already existing list of QItemEditorFactory

    Hi,

    the Color Editor Factory example from the QT Documentation shows how to use the ColorListEditor as the default editor for a data from type QVariant::Color.

    I noticed that by using the code from this example, the already existing default editors for a variety of data types (bool, double, int, QString,....) are not available anymore as the new created factory will contain ONLY the editor for the type QVariant::Color.

    Qt Code:
    1. QItemEditorCreatorBase *colorListCreator = new QStandardItemEditorCreator<ColorListEditor>();
    2. factory->registerEditor(QVariant::Color, colorListCreator);
    3. QItemEditorFactory::setDefaultFactory(factory);
    To copy to clipboard, switch view to plain text mode 


    So I would like to know if this is the correct way to actually add the ColorListEditor to the already existing list of editors:

    Qt Code:
    1. QItemEditorCreatorBase *colorListCreator = new QStandardItemEditorCreator<ColorListEditor>();
    2. factory->registerEditor(QVariant::Color, colorListCreator);
    3. QItemEditorFactory::setDefaultFactory(factory);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: Adding an editor to the already existing list of QItemEditorFactory

    Could you provide a minimal compilable example reproducing the problem?
    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
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Adding an editor to the already existing list of QItemEditorFactory

    I would like to know if there is a possiblity to actually add the ColorListEditor to the already existing list of editors.

    The color editor factory example provided with Qt is a compilable example.

    In the example provided with Qt, if I do put the line ItemEditorFactory::setDefaultFactory(factory); from the function Window::Window() in comment I can edit the names (Alice, Neptun, Ferdinand) AND the color using the QLineEdit Editor.

    If I leave this line out of the comment I can ONLY edit the color using the colorListEditor widget but I loose the possibility to edit the names (Alice, Neptun, Ferdinand).

    What I would like to do is to use all standard editors for all the types that are already recognized (f.e. QLineEdit for QString) and use in addition custom editors for the other types (ColorListEditor for QVariant::color).

    In other words: I would like to be able to edit the names (Alice, Neptun, Ferdinand) by using QLineEdit and the color by using the custom editor for QVariant::Color without the need to add lines like:
    QItemEditorCreatorBase *stringCreator = new QStandardItemEditorCreator<QLineEdit>();
    factory->registerEditor(QVariant::String, stringCreator);

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding an editor to the already existing list of QItemEditorFactory

    thats a very good catch! but i dont know how you are able to edit both data types when commenting setDefaultFactory()...anyways, couple of ways to resolve your issue:

    1. using delegate: dont set this factory as default, just call setItemEditorFactory(QItemEditorFactory *) on QStyledItemDelegate, Qt's right hand for drawing views. thus when QVariant::Type is Color, it will create your custom editor, for all other QVariant types, it will use default factory.

    2. derive from QItemEditorFactory, implement createEditor(..) in a way that if type is Color, it will use ColorEditor, otherwise it will call default factory's createEditor(..). next, set this new class as the default editor factory.

    i think second one will work better for you, i.e. without interfering with any delegate.

  5. The following user says thank you to talk2amulya for this useful post:

    schall_l (25th May 2009)

  6. #5
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Adding an editor to the already existing list of QItemEditorFactory

    Thank you talk2amulya, the second one did effectively worked for me.

    Here is the code if someone is interested in:

    Qt Code:
    1. class IdentifiersEditor : public QItemEditorFactory
    2. {
    3. public:
    4. inline IdentifiersEditor() {}
    5. QWidget *createEditor(QVariant::Type type, QWidget *parent) const;
    6. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. IdentifiersEditor::createEditor(QVariant::Type type, QWidget *parent) const {
    2. switch (type) {
    3. case QVariant::Color: {
    4. ColorListEditor *cb = new ColorListEditor(parent);
    5. cb->setFrame(false);
    6. return cb;
    7. }
    8. default:
    9. return QItemEditorFactory::createEditor(type, parent);
    10. break;
    11. }
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding an editor to the already existing list of QItemEditorFactory

    I have a doubt. You will still need to set your factory somewhere right ?
    Also in the example in post #1, you create a QItemEditorFactory and thats why you dont get the default editors with it.

    Why cant you obtain the factory from item delegate and register you editor ??
    QItemDelegate::itemEditorFactory and QStyledItemDelegate::itemEditorFactory

    Wont it be easier ?

  8. #7
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding an editor to the already existing list of QItemEditorFactory

    Hi

    I had the same problem. schall_l's idea to subclass QItemEditorFactory is basically right, but it doesn't work, since QItemEditorFactory ist a base class with no functionality. You have to use the previously installed default factory (which is an instance of the private class QDefaultItemEditorFactory).

    Qt Code:
    1. class ItemEditorFactory : public QItemEditorFactory
    2. {
    3. public:
    4. explicit ItemEditorFactory(const QItemEditorFactory* standardFactory);
    5. virtual QWidget* createEditor(QVariant::Type type, QWidget* parent) const;
    6. static void installAsStandardFactory();
    7. private:
    8. const QItemEditorFactory* m_standardFactory;
    9. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. ItemEditorFactory::ItemEditorFactory(const QItemEditorFactory* standardFactory)
    2. , m_standardFactory(standardFactory)
    3. {
    4. }
    5.  
    6. QWidget* ItemEditorFactory::createEditor(QVariant::Type type, QWidget* parent) const
    7. {
    8. switch (type)
    9. {
    10. case QVariant::Color:
    11. {
    12. ColorListEditor *cb = new ColorListEditor(parent);
    13. cb->setFrame(false);
    14. return cb;
    15. }
    16. default:
    17. return m_standardFactory->createEditor(type, parent);
    18. }
    19. }
    20.  
    21. static void ItemEditorFactory::installAsStandardFactory()
    22. {
    23. QItemEditorFactory::setDefaultFactory(new ItemEditorFactory(QItemEditorFactory::defaultFactory()));
    24. }
    To copy to clipboard, switch view to plain text mode 

    Just call ItemEditorFactory::installAsStandardFactory() in your code.

    Happy Qoding.

  9. #8
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows

    Default Re: Adding an editor to the already existing list of QItemEditorFactory

    Shentian's code works perfectly in Qt4. However, the declaration of the virtual function createEditor(...) is slightly different in Qt5:

    Qt Code:
    1. QWidget* ItemEditorFactory::createEditor(int userType, QWidget* parent) const
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Adding items to a QGraphicsScene's selection list?
    By mooreaa in forum Qt Programming
    Replies: 2
    Last Post: 1st July 2008, 20:01

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.