PDA

View Full Version : What should I write into DataDelegate::paint?



TomASS
3rd August 2009, 08:49
Like in topic:

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


Where DataDelegate is:


DataDelegate::DataDelegate( QObject *parent ) : QAbstractItemDelegate( parent ) {

}

I've tried:

void DataDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
if( option.state & QStyle::State_Selected )
painter->fillRect( option.rect, option.palette.highlight() );

painter->drawText(option.rect.x()+4, option.rect.y()+20, index.model()->data( index, Qt::DisplayRole ).toString());
}
But when I tried to resize a kolumn by pressing between the columns, the column resize to much:
http://imgshare.org/f/1/4f22dc5617e9f1cceed1ac5a34a59814.JPG

victor.fernandez
3rd August 2009, 09:10
You have to elide the text (cut it at the end and add the dots). QFontMetrics::elidedText() (http://doc.qtsoftware.com/4.5/qfontmetrics.html#elidedText) does the trick for you. For instance:



void DataDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
if( option.state & QStyle::State_Selected )
painter->fillRect( option.rect, option.palette.highlight() );

QFontMetrics metrics(option.font);
QString text = index.model()->data( index, Qt::DisplayRole ).toString();
text = metrics.elidedText(text, option.textElideMode, option.rect.width()-4);
painter->drawText(option.rect.x()+4, option.rect.y()+20, text);
}

TomASS
3rd August 2009, 09:17
You have to elide the text (cut it at the end and add the dots). QFontMetrics::elidedText() (http://doc.qtsoftware.com/4.5/qfontmetrics.html#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)

victor.fernandez
3rd August 2009, 09:25
Then you can set the resizeMode (http://doc.qtsoftware.com/4.5/qheaderview.html#setResizeMode) of the column to QHeaderView::ResizeToContents.



view->header()->setResizeMode(QHeaderView::ResizeToContents);


Or you can use QHeaderView::resizeSections() (http://doc.qtsoftware.com/4.5/qheaderview.html#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.

TomASS
3rd August 2009, 09:30
Then you can set the resizeMode (http://doc.qtsoftware.com/4.5/qheaderview.html#setResizeMode) of the column to QHeaderView::ResizeToContents.



view->header()->setResizeMode(QHeaderView::ResizeToContents);


Or you can use QHeaderView::resizeSections() (http://doc.qtsoftware.com/4.5/qheaderview.html#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 :/

victor.fernandez
3rd August 2009, 09:35
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() (http://doc.qtsoftware.com/4.5/qtableview.html#horizontalHeader).

PS: I made a mistake in the previous post since it's horizontalHeader() and not header(). Sorry for that.