Results 1 to 11 of 11

Thread: QStyledItemDelegate and editor pointer

  1. #1
    Join Date
    Jun 2010
    Posts
    8
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QStyledItemDelegate and editor pointer

    Hello,
    I've a QStyledItemDelegate derived class for a custom delegate editor:

    Qt Code:
    1. CheckBoxDelegate::CheckBoxDelegate(const QStringList &texts, QObject *parent)
    2. : QStyledItemDelegate(parent), texts(texts)
    3. { }
    4.  
    5. QWidget *CheckBoxDelegate::createEditor(QWidget *parent,
    6. const QStyleOptionViewItem &/* option */,
    7. const QModelIndex &index) const
    8. {
    9. CheckBoxDelegateWidget *editor = new CheckBoxDelegateWidget(texts, parent);
    10. ...
    11. return editor;
    12. }
    To copy to clipboard, switch view to plain text mode 
    Now I want sizeHint() to return the CheckBoxDelegateWidget sizes, like this:
    Qt Code:
    1. QSize CheckBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index ) const
    2. {
    3. return editor->sizeHint(); // what editor?
    4. }
    To copy to clipboard, switch view to plain text mode 
    Where I can obtain the editor pointer? Why createEditor() doesn't let me do a local copy of the pointer?
    Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QStyledItemDelegate and editor pointer

    You can keep a map of the widgets or size hints for each model index.

    Be sure to not delete the widget pointers yourself, the view will do that for you.
    Last edited by tbscope; 13th September 2010 at 11:24.

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

    Nero (13th September 2010)

  4. #3
    Join Date
    Jun 2010
    Posts
    8
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStyledItemDelegate and editor pointer

    Thanks tbscope,
    sorry I'm a C++ newcomer, createEditor() is const so I should not modify class variables, am I wrong?

  5. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QStyledItemDelegate and editor pointer

    Actually there's no need at all to store the pointer of the widget or the size hint etc...

    Just use the model index in the sizeHint to get the needed data.

    Qt Code:
    1. QSize CheckBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index ) const
    2. {
    3. CheckBoxDelegateWidget *editor = new CheckBoxDelegateWidget(index.data(...));
    4.  
    5. return editor->sizeHint();
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    Nero (13th September 2010)

  7. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QStyledItemDelegate and editor pointer

    Oops:

    Qt Code:
    1. QSize CheckBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index ) const
    2. {
    3. CheckBoxDelegateWidget editor(index.data(...));
    4.  
    5. return editor.sizeHint();
    6. }
    To copy to clipboard, switch view to plain text mode 

  8. #6
    Join Date
    Jun 2010
    Posts
    8
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStyledItemDelegate and editor pointer

    No problem, I've already seen that.
    Thank you.

  9. #7
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStyledItemDelegate and editor pointer

    Hello,

    Sorry for bumping the thread but after reading it all I still can't get what value to put to the index.data() to get the editor? Maybe I must set the data myself but if that is the case I can't because createEditor() gives me constant QModelIndex reference.

    Any help will be appreciated.

  10. #8
    Join Date
    Jun 2010
    Posts
    8
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStyledItemDelegate and editor pointer

    You need to call the constructor of your custom widget, in my case:
    Qt Code:
    1. CheckBoxDelegateWidget(const QStringList &texts, QWidget *parent = 0)
    To copy to clipboard, switch view to plain text mode 
    But if your delegate widget is the same for all the column, you'd better create a local widget on the constructor of the delegate and save its sizes one time for all, then in sizeHint you return the saved sizes:

    Qt Code:
    1. CheckBoxDelegate::CheckBoxDelegate(const QStringList &texts, QObject *parent)
    2. : QStyledItemDelegate(parent), texts(texts)
    3. {
    4. CheckBoxDelegateWidget editor(texts);
    5. widgeSizeHint_ = editor.sizeHint();
    6. }
    7.  
    8. QSize CheckBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index ) const
    9. {
    10. return widgeSizeHint_;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Digging in the Qt source code I've found:
    Qt Code:
    1. QWidget *QAbstractItemViewPrivate::editor(const QModelIndex &index,
    2. const QStyleOptionViewItem &options)
    To copy to clipboard, switch view to plain text mode 
    which is what we need, but is a private function of QAbstractItemView. I think it is because the editor are created only when needed (when you double click a cell), so if it isn't created and you call this function, it creates a new editor but for the ItemView no editor is open so it doesn't know when delete it and it remains in memory.

  11. The following user says thank you to Nero for this useful post:

    The Storm (20th September 2010)

  12. #9
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStyledItemDelegate and editor pointer

    Nice hint but in my case will not work because my custom widget sizeHint() changes dynamically depending on the contents in it.

    When I create the editor I have other data class which I use to populate the data in the editor widget. After the data is populated the size hint will be calculated. The constructor for the delegate is called only once per item, and if my custom editor changes it's sizeHint the item will not be with proper size.

    I have tried with mutable variable which I use in the function createEditor() to obtain correct sizeHint but it seems that it is too late, because the TreeView had already called the virtual funtion sizeHint() and I get improper item sizes...

    I hope I was able to explain my case clear and that there is solution to this.

  13. #10
    Join Date
    Jun 2010
    Posts
    8
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStyledItemDelegate and editor pointer

    Maybe I've lost you, but as tbscope said, something like this doesn't work?
    Qt Code:
    1. QSize CheckBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index ) const
    2. {
    3. CheckBoxDelegateWidget editor( yourClassDataList.value( index.row() ) );
    4. return editor.sizeHint();
    5. }
    To copy to clipboard, switch view to plain text mode 

  14. The following user says thank you to Nero for this useful post:

    The Storm (20th September 2010)

  15. #11
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStyledItemDelegate and editor pointer

    Yes this will do the trick. The only thing is that both methods are "hacks" and leads to overhead... Anyway if this is the only way I don't have much of a choise.

Similar Threads

  1. QStandardItem, QStyledItemDelegate, and QWebView
    By enlightened_j in forum Newbie
    Replies: 1
    Last Post: 16th August 2010, 08:44
  2. QStyledItemDelegate with custom QWidget editor
    By stefanadelbert in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2010, 23:19
  3. Setting Format in a QStyledItemDelegate
    By Airswoop1 in forum Qt Programming
    Replies: 0
    Last Post: 1st June 2009, 22:01
  4. Replies: 5
    Last Post: 30th March 2008, 16:53
  5. Replies: 6
    Last Post: 22nd June 2007, 22:20

Tags for this Thread

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.