How to make QListview and QFilesystemModel item with multiple line text?
-
Hi guys:
I want to use a QListView and a QFilesystemModel to customize an icon viewer like the original one in Mac!!!
The original Mac icon viewer:
!http://dl.dropbox.com/u/24137109/icontext1.png(Mac Icon Viewer)!And the custom icon viewer:
!http://dl.dropbox.com/u/24137109/icontext2.png(Mac Icon Viewer)!The problem is that the file name in the custom icon viewer can only display single line, however, the original Mac icon viewer can display file name in two lines.
What I should do to make the custom icon viewer displays the file name in two lines.
If two line can not contain all the file name, show ellipsis at the right side of the second line.Should I subclass QStyledItemDelegate and override paint() function or something else??
Could anyone give me a hint!!
Thanks in advance!!
Kenny
-
you could try out "ElidedText...":http://doc.qt.nokia.com/4.7/qfontmetrics.html#elidedText
and use Qt::ElideMiddle
But it seems strange to me that the mac style uses it and it's not in your example because the OS style should be followed.
If you inherit QStyledItemDelegate then paint is where you can do your thing.
-
Thanks for your replied!!
Actually, I try to make a cross platform( mac/windows ) icon viewer, and the elide condition of windows seems to elide right, so I want to follow the condition in windows.
I have tried to use Qt::ElideRight, but it can not make the file name to display in two lines, and elide at the end of the second line of the file name.
Any hint about this issue??Besides, I have tried to inherit QStyledItemDelegate, but the text seems to show twice, and the text may overlap with each other, how to make it just display once??
Thanks in advance!!
Kenny
-
bq. Besides, I have tried to inherit QStyledItemDelegate, but the text seems to show twice, and the text may overlap with each other, how to make it just display once??
can you show the code for the delegate?
-
sure, I inherited QStyledItemDelegate and override paint() function as the code below
@
void CFMDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
QStyleOptionViewItemV4 opt = option;
initStyleOption( &opt, index );// draw correct background QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); QString strText = opt.text; opt.text = ""; QRect rect = opt.rect; QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled; if ( cg == QPalette::Normal && !( opt.state & QStyle::State_Active ) ) { cg = QPalette::Inactive; } // set pen color if ( opt.state & QStyle::State_Selected ) { painter->setPen(opt.palette.color(cg, QPalette::HighlightedText)); } else { painter->setPen(opt.palette.color(cg, QPalette::Text)); } QString newText = painter->fontMetrics().elidedText( strText, Qt::ElideRight, rect.width() ); int nFlags = Qt::TextWrapAnywhere | Qt::AlignBottom; painter->drawText( rect, nFlags, newText );
}@
And the overlap situation is like the image below:
!http://dl.dropbox.com/u/24137109/overlap.png(image)! -
Hi Kenny,
- in your code you have :
@style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
...
painter->drawText( rect, nFlags, newText ); @
both lines draw text. the first draws the item and the other text only. That's why you see the text double.- Also sizeHint and updateEditorGeometry(if you want editing) will need to be reimplemented to put the second line under the first. Now there is only on line.
Here is some example code. not tested for your case...
@QSize yourDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QString Value =index.data(Qt::DisplayRole).toString() ;
QSize sz = QStyledItemDelegate::sizeHint(option, index);sz.setHeight(2 * sz.height()); return sz;
}
void yourDelegate::updateEditorGeometry(QWidget editor,
const QStyleOptionViewItem &option, const QModelIndex &/ index */) const
{
QSize ExtraSize= QSize(option.rect.width(), 2 * option.rect.height());
QRect ExtraRect = option.rect;
ExtraRect.setSize(ExtraSize);
editor->setGeometry(option.rect);
}@- You will need some QString parsing if the wrapping doesn't do it's job. Let's hope not
-
Thanks for your reply, Eddy
I will try it ASAP. -
[quote author="Eddy" date="1312548300"]Hi Kenny,
@
QSize yourDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QString Value =index.data(Qt::DisplayRole).toString() ;
QSize sz = QStyledItemDelegate::sizeHint(option, index);sz.setHeight(2 * sz.height()); return sz;
}
@
[/quote]Eddy, how is Value used in this code? I do not see it referenced.