Results 1 to 6 of 6

Thread: What should I write into DataDelegate::paint?

  1. #1
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default What should I write into DataDelegate::paint?

    Like in topic:

    What should I write into DataDelegate:: paint when I want simply put text into a cell?


    Where DataDelegate is:

    Qt Code:
    1. DataDelegate::DataDelegate( QObject *parent ) : QAbstractItemDelegate( parent ) {
    2.  
    3. }
    To copy to clipboard, switch view to plain text mode 

    I've tried:
    Qt Code:
    1. void DataDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    2. {
    3. if( option.state & QStyle::State_Selected )
    4. painter->fillRect( option.rect, option.palette.highlight() );
    5.  
    6. painter->drawText(option.rect.x()+4, option.rect.y()+20, index.model()->data( index, Qt::DisplayRole ).toString());
    7. }
    To copy to clipboard, switch view to plain text mode 
    But when I tried to resize a kolumn by pressing between the columns, the column resize to much:

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What should I write into DataDelegate::paint?

    You have to elide the text (cut it at the end and add the dots). QFontMetrics::elidedText() does the trick for you. For instance:

    Qt Code:
    1. void DataDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    2. {
    3. if( option.state & QStyle::State_Selected )
    4. painter->fillRect( option.rect, option.palette.highlight() );
    5.  
    6. QFontMetrics metrics(option.font);
    7. QString text = index.model()->data( index, Qt::DisplayRole ).toString();
    8. text = metrics.elidedText(text, option.textElideMode, option.rect.width()-4);
    9. painter->drawText(option.rect.x()+4, option.rect.y()+20, text);
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by victor.fernandez; 3rd August 2009 at 09:16.

  3. #3
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What should I write into DataDelegate::paint?

    Quote Originally Posted by victor.fernandez View Post
    You have to elide the text (cut it at the end and add the dots). QFontMetrics::elidedText() does the trick for you.
    Thanks, but
    I don't want elide text to column, I want resize column to the size of text (for example resizeToContent work)

  4. #4
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What should I write into DataDelegate::paint?

    Then you can set the resizeMode of the column to QHeaderView::ResizeToContents.

    Qt Code:
    1. view->header()->setResizeMode(QHeaderView::ResizeToContents);
    To copy to clipboard, switch view to plain text mode 

    Or you can use QHeaderView::resizeSections(). If you do this in the paint() method, the program might enter an infinite loop, since resizing the column will cause a paint event that resizes the column causing another paint event and so on.

    However, you can't do any of these in the delegate unless you pass the view to the delegate yourself.

  5. #5
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What should I write into DataDelegate::paint?

    Quote Originally Posted by victor.fernandez View Post
    Then you can set the resizeMode of the column to QHeaderView::ResizeToContents.

    Qt Code:
    1. view->header()->setResizeMode(QHeaderView::ResizeToContents);
    To copy to clipboard, switch view to plain text mode 

    Or you can use QHeaderView::resizeSections(). If you do this in the paint() method, the program might enter an infinite loop, since resizing the column will cause a paint event that resizes the column causing another paint event and so on.

    However, you can't do any of these in the delegate unless you pass the view to the delegate yourself.

    Thanks, but there's no setResizeMode method in QTableView class :/

  6. #6
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What should I write into DataDelegate::paint?

    setResizeMode() doesn't belong to QTableView but to QHeaderView, which is the header of the table. You may get the horizontal QHeaderView of a QTableView using QTableView::horizontalHeader().

    PS: I made a mistake in the previous post since it's horizontalHeader() and not header(). Sorry for that.
    Last edited by victor.fernandez; 3rd August 2009 at 09:37. Reason: Fixed the link to horizontalHeader()

Similar Threads

  1. Problem on set QGraphicsTextItem write protect.
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2007, 20:53
  2. FSWriteFork in MAC OS to write data to a file.
    By vishal.chauhan in forum General Programming
    Replies: 5
    Last Post: 2nd July 2007, 06:48
  3. who can give me a tutorials of write .pro file
    By fengtian.we in forum Qt Programming
    Replies: 5
    Last Post: 20th May 2007, 16:25
  4. How to write on a socket in another thread?
    By Valheru in forum Qt Programming
    Replies: 7
    Last Post: 12th October 2006, 10:52
  5. how to write a structure in a shared mem segment.
    By nass in forum General Programming
    Replies: 9
    Last Post: 26th September 2006, 13:11

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.