Results 1 to 20 of 20

Thread: Delegate editor sizing problem

  1. #1
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Delegate editor sizing problem

    I have have implemented a custom delegate and model for editing all the QTableView cells in a very controlled way. When my delegates create the editor, it doesn't fill up the available space in the cell. Is there something that I need to do to make this happen?

    Thanks in advance,
    Susan

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delegate editor sizing problem

    Reimplement the sizeHint method, and return the cell size.

  3. #3
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delegate editor sizing problem

    How does the delegate find out the size of the cell? It has no direct access to the view that I know of.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delegate editor sizing problem

    Sorry about that... sizeHint is a wrong path.

    Try overriding QItemDelegate::updateEditorGeometry(...);
    Like this:

    Qt Code:
    1. [COLOR=#8B0000][FONT='Courier New,courier'][I][/I][/FONT][/COLOR]void MyDelegate::updateEditorGeometry( QWidget* editor, const QStyleOptionViewItem &option, const QModelIndex &index )
    2. {
    3. editor->setGeometry( option.rect );
    4. }
    To copy to clipboard, switch view to plain text mode 

    Using option.rect should resize your editor to fit perfectly into the cell.

    Regards

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

    smacchia (8th April 2007)

  6. #5
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delegate editor sizing problem

    Just what I was looking for! I'll give this a try.

    --Susan

  7. #6
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs down Re: Delegate editor sizing problem

    Unfortunately, this method never gets called!!! I am not sure why. I have an example MVC implementation from a training class I attended and the delgates never implemented this method and the editor (spinbox or combo) was always sized correctly... Ideas?

  8. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delegate editor sizing problem

    No idea why it isn't called, but take a look at the Spin Box delegate example in the Qt Demos ( Item Views section ).

    It does something similar to what you want.

  9. #8
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delegate editor sizing problem

    I think its not called because in the Qt source for QItemDelegete, it isn't virtual!! (it is supposed to be). Stepping through the Qt code for creating the delegate editor, it invokes QItemDelegate::updateEditorGeometry, not my derivation! I'll look at the example that you site, but I think this is the culprit. My example from the class is pretty straight forward too and doesn't event reimplement that method...

  10. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delegate editor sizing problem

    void QItemDelegate::updateEditorGeometry ( QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const [virtual] Updates the editor for the item specified by index according to the style option given.
    Reimplemented from QAbstractItemDelegate.
    This is the member description from QItemDelegate. As you can see it is virtual. Also this member is public and const. Perhaps you didn't declare it as public in your class?

  11. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delegate editor sizing problem

    Actually you're right. I just took a look at QItemDelegate.h and the member is not virtual.
    But it is in QAbstractItemDelegate. Couldn't you derive your delegate from QAbstractItemDelegate instead?

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delegate editor sizing problem

    Quote Originally Posted by smacchia View Post
    I think its not called because in the Qt source for QItemDelegete, it isn't virtual!!
    It is virtual. It doesn't have to be marked as virtual in QItemDelegate definition, since it was declared as virtual in QAbstractItemDelegate.

    Quote Originally Posted by marcel View Post
    Also this member is public and const. Perhaps you didn't declare it as public in your class?
    Also make sure you didn't forget about "const". It's quite common mistake.

  13. The following user says thank you to jacek for this useful post:

    smacchia (9th April 2007)

  14. #12
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delegate editor sizing problem

    Oh duh - it was the "const". Thanks! It works great now.

  15. #13
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Default Re: Delegate editor sizing problem

    i got a similar problem with delegates and a QTreeView, but i'm using editing widgets that contain other widgets, eg. a QWidget containing a QLineEdit.
    first, i tried adding a layout to the container widget and adding sub-widgets to it, but the result was that the item didn't seem to be editable anymore at all (or the widget has hone somewhere / was zero-sized or whatever).
    i tried overriding the setGeometry function for my subclasses but when the delegate calls setGeometry, allways the standard QWidget function is called.
    i declared setGeometry as:
    Qt Code:
    1. virtual void setGeometry(const QRect &r)
    To copy to clipboard, switch view to plain text mode 

  16. #14
    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: Delegate editor sizing problem

    If you use a completely custom widget for editing, you need to reimplement setModelData() and setEditorData() in the delegate. And probably you need to set a proper focus policy and maybe even a focus proxy. See docs for details.

  17. #15
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Default Re: Delegate editor sizing problem

    i allready overloaded setModelData() and setEditorData(). editing works just fine - only the layout is messed up.

  18. #16
    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: Delegate editor sizing problem

    Can we see the code of the widget and its layouts?

  19. #17
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Default Re: Delegate editor sizing problem

    here it is, the layout stuff has been commented out:

    Qt Code:
    1. class ItemEditor : public QWidget
    2. {
    3. public:
    4. ItemEditor(QWidget *i_pParent = NULL): QWidget(i_pParent)
    5. {
    6. //
    7. //QVBoxLayout *p_pLayout = new QVBoxLayout();
    8. //this->setLayout(p_pLayout);
    9.  
    10. };
    11.  
    12. ~ItemEditor(void){};
    13.  
    14. virtual QVariant data() = 0;
    15. virtual void setData(QVariant &i_Data) = 0;
    16.  
    17. };
    18.  
    19. class ItemLineEditor : public ItemEditor
    20. {
    21. private:
    22. QLineEdit *m_pLineEdit;
    23. public:
    24. ItemLineEditor(QWidget *i_pParent = NULL): ItemEditor(i_pParent)
    25. {
    26. // without layout
    27. m_pLineEdit = new QLineEdit(this);
    28.  
    29. //using a layout doesn't work at all
    30. //m_pLineEdit = new QLineEdit(this);
    31. //this->layout()->addWidget(m_pLineEdit);
    32. }
    33.  
    34. virtual QVariant data()
    35. {
    36. return m_pLineEdit->text();
    37. }
    38.  
    39. virtual void setData(QVariant &i_Data)
    40. {
    41. this->m_pLineEdit->setText(i_Data.toString());
    42. }
    43.  
    44. // gets never called
    45. virtual void setGeometry(const QRect& rect)
    46. {
    47. ItemEditor::setGeometry(rect);
    48. this->m_pLineEdit->setGeometry(rect);
    49. }
    50. };
    To copy to clipboard, switch view to plain text mode 

    as you may see, i'm doing this because i need a unified way of accessing the user input from different widgets (combobox, doublespinbox, lineedit etc.)

  20. #18
    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: Delegate editor sizing problem

    Quote Originally Posted by maximAL View Post
    as you may see, i'm doing this because i need a unified way of accessing the user input from different widgets (combobox, doublespinbox, lineedit etc.)
    Qt already supports that in form of "user properties".
    J-P Nurmi

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

    maximAL (10th December 2007)

  22. #19
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Default Re: Delegate editor sizing problem

    hmm, i actually didn't think of this.
    i tried to implement it now, by ran into the problem that i can set a property for any kind of widget, but then there is no unified way to initialize the actual content of the widget.
    eg, for a QLineEdit i could set the data property to the old value before editing, but how do i actually set the value as text for editing? i can't do it in the c'tor, since the property won't be set yet at this time.

  23. #20
    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: Delegate editor sizing problem

    1. setGeometry() is not virtual, so overriding it doesn't make much sense,
    2. You never set the geometry of the line edit - you should either be using layouts or reimplement resizeEvent() and use setGeometry() to set the geometry of all child widgets
    3. I don't really know what you mean about a unified way to initialize the contest, but maybe seeing how it is done for a widget mapper helps:
    http://doc.trolltech.com/qq/qq21-dat...toofferchoices (what's important is the content of set*Data methods).

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

    maximAL (10th December 2007)

Similar Threads

  1. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  2. Problem in Cut Operation
    By ankurjain in forum Qt Programming
    Replies: 4
    Last Post: 14th April 2006, 12:23
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.