Results 1 to 6 of 6

Thread: QItemEditorFactory and custom Model

  1. #1
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Default QItemEditorFactory and custom Model

    Hi,
    i tried to define my own delegate editor for a custom Tree model used with a standard QTreeView.

    seup is like this:
    Qt Code:
    1. QItemEditorCreatorBase *lineEditorCreator =
    2. new QStandardItemEditorCreator<LineEditorCreator>();
    3. factory->registerEditor(QVariant::String, lineEditorCreator);
    4. QItemEditorFactory::setDefaultFactory(factory);
    5.  
    6. SceneModel model("test.xml");
    7.  
    8. QTreeView view;
    9. view.setModel(&model);
    10. view.show();
    To copy to clipboard, switch view to plain text mode 
    this is the delegate (yes, it's only for testing):
    Qt Code:
    1. class LineEditorCreator : public QLineEdit
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(QString text READ text WRITE setText USER true)
    5.  
    6. public:
    7. LineEditorCreator(QWidget *widget = 0):
    8. QLineEdit(widget)
    9. {
    10. };
    11.  
    12. QString text() const
    13. {
    14. return QLineEdit::text();
    15. };
    16.  
    17. void setText(QString text)
    18. {
    19. QLineEdit::setText(text);
    20. };
    21.  
    22. };
    To copy to clipboard, switch view to plain text mode 

    i can also add the code for the model, but to my understanding it shouldn't interfere with the delegate.
    without the custom delegate, editing works fine (so, yes, items are editable), with the delegate just nothing happens (and it's functions aren't called at all).

    for testing purpose, i also tried with a standard QTreeWidget and the delegate works just fine there.

    are there any special additions to be made to an model, so it supports the delegate?
    Last edited by maximAL; 23rd November 2007 at 11:17.

  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: QItemEditorFactory and custom Model

    You did not provide code of the delegate but of the widget used for editing - don't mix those two. The problem here might be that you register the editor for QString and by default the model doesn't contain strings (if it's empty, for example). In your case I'd suggest to subclass the delegate class and reimplement QAbstractItemDelegate::createEditor() to return your widget instead of QLineEdit.

  3. The following user says thank you to wysota for this useful post:

    maximAL (23rd November 2007)

  4. #3
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Default Re: QItemEditorFactory and custom Model

    Quote Originally Posted by wysota View Post
    The problem here might be that you register the editor for QString and by default the model doesn't contain strings (if it's empty, for example).
    could you elaborate on that? the model uses custom SceneItem objects storing the actual data (strictly following the official QT tutorial on creating customs models). the data is definitely handled as QVariant::String (read: QString).
    Quote Originally Posted by wysota View Post
    In your case I'd suggest to subclass the delegate class and reimplement QAbstractItemDelegate::createEditor() to return your widget instead of QLineEdit.
    yes, i tried this and it works. nevertheless, providing editors via the factory would simply be the more elegant way for my purpose, so i'd really like to get it working.

    btw: i tried this approach:
    Qt Code:
    1. // no default factory!
    2. //QItemEditorFactory::setDefaultFactory(factory);
    3. ...
    4. QItemDelegate delegate;
    5. delegate.setItemEditorFactory(factory);
    6. ...
    7. view.setItemDelegate(&delegate);
    To copy to clipboard, switch view to plain text mode 
    this has no effect. now, the standard editor instead of my own one is used again.

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QItemEditorFactory and custom Model

    I second Wysota. Most likely the problem is that model's data() does not return a QString for Qt::EditRole for an empty index. Therefore, when you start editing an empty cell, you'll get an editor for QVariant::Invalid which is the default editor, a QLineEdit (or QExpandingLineEdit to be exact). You can get past the problem by simply registering your custom editor for QVariant::Invalid too (if that's what you want):
    Qt Code:
    1. factory->registerEditor(QVariant::String, lineEditorCreator);
    2. factory->registerEditor(QVariant::Invalid, lineEditorCreator); // <---
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    maximAL (23rd November 2007)

  7. #5
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Default Re: QItemEditorFactory and custom Model

    it also doesn't work with cells containing data.
    but indeed, it works when setting the editor for QVariant::Invalid -> for all cells! so for some reason editing passes around invalid qvariants. maybe indeed a bug in my model, hmm...

    edit: so for all who care to know what the mistake was:
    my model's data(..) function returned an empty QVariant() for any index role besided DisplayRole
    Last edited by maximAL; 23rd November 2007 at 15:19. Reason: solved

  8. #6
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Default Re: QItemEditorFactory and custom Model

    so, after digging deeper into the whole issue, i'm struggling now with the mime stuff for drag and drop and i'm really starting to wonder if i'm not completely heading the wrong direction (because it becomes so overly complicated to do basic things).

    thats what i want to achive basically:
    i have a tree model and view displaying the contents of an XML file.
    the data has to be represented if different ways, eg. for some strings i might just need a plain lineedit, for others a combobox filled with standard values etc., also doubles might need different validators etc.
    my approach was now to build custom types for all elements and provide specialized editors for each type.
    now i ran in quite a mess with all that qvariant stuff, casting my own data from and to it, mime data handling and what not.

    another approach i could imagine is just using standard qvariant types and use custom item roles to identifiy the type of an element and provide different editors via a delegate->createEditor.

    the first approach seems to be the intended way to go but is quiet complicated, the second one is pretty straight forward, but will also result in some not-so-elegant code (big switch - case blocks instead of decoupled editor classes etc.).

    maybe someone could share his experiences on that. i don't want to use the straight forward method just to realize it would have been worth getting into the more complicated stuff later one.

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.