Results 1 to 8 of 8

Thread: Custom Item Delegate (QDateEdit) and Size Hints

  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Custom Item Delegate (QDateEdit) and Size Hints

    G'day,

    After a discussion in another thread I am experimenting with a custom delegate (QStyledItemDelegate) to handle "date" fields coming from an Sqlite database. I've built the delegate and, for the most part, got it working. I have called QTableView::resizeColumnsToContents() and set QHeaderView::setResizeMode(QHeaderView::ResizeToContents) so the display mode has the date column resized to fit.

    The problem comes when I go into edit mode. The text string ISO data coming from Sqlite is converted to QDate and passed to the QDateEdit control the delegate has set up to edit it. The editor is rendered in the QTableView cell but the spin box controls invariably overlay the right end of the date.

    I am struggling to find how I can have the column resize to fit the QDateEdit spin box controls and still have the date displayed without truncation. I'm guessing that some magic is required in the delegate's size hint but cannot see how I can determine a good hint. There must be some way to get at the size of the edit control (no pointer to this is passed to sizeHint()) to add to the string size but I'm not familiar enough with the style options stuff to locate it.

    Any nudges in the right direction would be greatly appreciated.
    Chris W

    QT4.5.1
    Last edited by ChrisW67; 27th May 2009 at 09:14. Reason: Better title

  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: Custom Item Delegate (QDateEdit) and Size Hints

    You can try emitting a sizeHintChanged() signal from the delegate once you open the editor and reutrn a proper (bigger) size hint from the respective method in the delegate when asked for it.
    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
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Custom Item Delegate (QDateEdit) and Size Hints

    Thanks for the suggestion wysota.

    I can't emit sizeHintChanged() or set a member variable to the sizeHint() of the editor while inside the createEditor() or setEditorData() methods because these actions violate the method const-ness. In the sizeHint() method itself I don't have access to the editor widget to ask for a "sane" sizeHint() to pass back, which is why I was looking to capture the size at the point of widget creation. I can do something like:
    Qt Code:
    1. QSize s = QStyledItemDelegate::sizeHint(option, index);
    2. s.setWidth(s.width() * 1.5);
    3. return s;
    To copy to clipboard, switch view to plain text mode 
    in the sizeHint() method and the cell does get bigger but the result is not related to the size the editor thinks it should be. Is there a better way to determine a good size?

  4. #4
    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: Custom Item Delegate (QDateEdit) and Size Hints

    Quote Originally Posted by ChrisW67 View Post
    I can't emit sizeHintChanged() or set a member variable to the sizeHint() of the editor while inside the createEditor() or setEditorData() methods because these actions violate the method const-ness.
    But you can use invokeMethod() to schedule a delayed signal emission from a custom slot. Use "mutable" if necessary.
    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
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Custom Item Delegate (QDateEdit) and Size Hints

    Thanks again.

    I'm still not quite there though. My createEditor() looks like:
    Qt Code:
    1. QWidget * DateDelegate::createEditor(QWidget *parent,
    2. const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. if (index.data().canConvert(QVariant::Date)) {
    6. QDateEdit *editor = new QDateEdit(parent);
    7. editor->setDisplayFormat("dd MMM yyyy");
    8.  
    9. // Capture the size hint for later
    10. DateDelegate *obj = const_cast<DateDelegate *>(this);
    11. QSize size = editor->sizeHint();
    12. QMetaObject::invokeMethod(obj, "captureSizeHint", Qt::QueuedConnection,
    13. Q_ARG(QModelIndex, index),
    14. Q_ARG(QSize, size) );
    15.  
    16. connect(editor, SIGNAL(editingFinished()),
    17. this, SLOT(commitAndCloseEditor()));
    18. return editor;
    19. }
    20. else
    21. return QStyledItemDelegate::createEditor(parent, option, index);
    22. }
    To copy to clipboard, switch view to plain text mode 
    and the custome slot captureSizeHint():
    Qt Code:
    1. void DateDelegate::captureSizeHint(const QModelIndex & index, QSize hint)
    2. {
    3. m_size = hint;
    4. emit sizeHintChanged(index);
    5. }
    To copy to clipboard, switch view to plain text mode 
    The sizeHint() returns m_size if it is valid otherwise it just hands off to the default implementation.

    The code works but the column does not resize until the editor is closed. Tracing the execution I see the editor size hint being captured at creation time, but the delegate's sizeHint() method is not called again until after the editor closes. Using Qt::DirectConnection makes no difference.

    Forgive me if I'm missing the blindingly obvious but I am in the middle of a steep C++ and Qt learning curve.

  6. #6
    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: Custom Item Delegate (QDateEdit) and Size Hints

    If it doesn't work this way then there is no easy way of obtaining what you want. I would reimplement QAbstractItemView::edit() slot (you have to redeclare it as a slot in the subclass) and there I would do the trick with the sizeHintChanged() signal and only after the signal is emitted I would call the base class implementation of edit(). I can't guarantee it will work but there is a chance it will.

    There is also an alternative - get rid of the arrows in the spinbox or set a smaller font for the editor.
    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.


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

    ChrisW67 (28th May 2009)

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Custom Item Delegate (QDateEdit) and Size Hints

    Thanks for your time. I'll ponder what to do over the next day or two.

  9. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Custom Item Delegate (QDateEdit) and Size Hints

    What I'm doing for now is creating a QDateEdit with the right display format on the stack in the delegate's constructor. I then store its sizeHint() in a member variable before it goes out of scope. The delegate's sizeHint() returns the cached size hint, and can do so the first time the table asks for it (in display mode).

    This approach has some downsides in terms of allowing the user to set a date format in a QSettings value or the like: the delegate has to be reconstructed to pick up the change. This is not a show stopper for me though.

Similar Threads

  1. Replies: 5
    Last Post: 10th August 2009, 10:50
  2. Delegate for a certain item?
    By somebody in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 22:55
  3. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37
  4. QMovie from inside a custom item delegate
    By Crazy_Hopper in forum Qt Programming
    Replies: 3
    Last Post: 7th May 2008, 15:18
  5. list of custom item views?
    By Beluvius in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2008, 06:43

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.