Problem in TextWord Wrapping in QListView[Unable to do both TextWordWrap & TextWrapAnywhere together ].
-
wrote on 26 Feb 2013, 06:14 last edited by
Hi,
I am working on a chat application where i have a QtextEdit and QListView . What ever i type in QTextedit its shown as new entry in QListView.
I am using custom delegate for QListView to show whole text in an item in QlistView . In that delegate i am using sizeHint and paint function as described below
@
void ChatViewItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex & index) const
{QString uri = index.model()->data(index, Qt::UserRole+2).toString(); QString textMessage = index.model()->data(index, Qt::UserRole+5).toString(); QRect messageRect = option.rect; QRect uriRect = option.rect; QFont font = QApplication::font(); font.setBold(true); QFont messageFont = QApplication::font(); messageFont.setWeight(10); QRectF boundryRect(option.rect.topLeft(), option.rect.bottomRight()); boundryRect.setRight(messageRect.right()); boundryRect.setTop(messageRect.top() + 1); if (option.state & QStyle::State_Selected) painter->fillRect(boundryRect, option.palette.highlight()); painter->save(); painter->drawRect(boundryRect); messageRect.setLeft(messageRect.left() + 20); messageRect.setTop(messageRect.top() + 5); messageRect.setRight(messageRect.right() - 5); messageRect.setBottom(messageRect.bottom() - 5); painter->drawText(messageRect, Qt::AlignBottom | Qt::TextWrapAnywhere , textMessage); painter->setFont(font); painter->drawText(uriRect, Qt::AlignLeft , uri); painter->restore();
}
QSize ChatViewItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QAbstractItemView view = dynamic_cast<QAbstractItemView>(parent());
QFontMetricsF fontMetrics(view->fontMetrics());
QString str = index.data().toString().trimmed();
QStringList words = str.split(QChar(' '));
QStringList lines = str.split(QChar('\n'));
int noOfLines = lines.size();
if(noOfLines > 2)
noOfLines = 1;
else
noOfLines =0;
QRectF boundingRect = fontMetrics.boundingRect(str);
int width = view->viewport()->width()- 25;
int height = boundingRect.height();
int times = 0;
while (words.size() > 0)
{
times++;
qreal lineWidth = 0;
bool enoughSpace = true;
do
{
QString word = words.first();
qreal wordWidth = fontMetrics.width(word);
if(wordWidth > width)
{
times++;
while(wordWidth > width )
{
times++;
wordWidth -= width;
}
lineWidth = wordWidth;
words.removeFirst();
}else if (wordWidth + lineWidth < width) { lineWidth += wordWidth; lineWidth += fontMetrics.width(QChar(' ')); words.removeFirst(); } else enoughSpace = false; } while (enoughSpace && words.size() > 0); } height = height * times + (2 + noOfLines)* boundingRect.height(); return QSize(width, height);
}
@
Problem: please see the attachments
I am using Qt::TextWrapAnywhere for text wrapping. The problem with that is that it truncate the word in the end of line and show it in next line.If i Use QT::TextWordWrap then if the word is too long then it doesnt truncate it ... on maximizing the window it appears.
If i use flags as @int flags = Qt::AlignBottom|Qt::TextDontClip|Qt::TextWordWrap|Qt::TextWrapAnywhere|Qt::TextJustificationForced;@ still it works like Qt::TextWrapAnywhere
Can someone please help me that what i should do to fix this.
!http://www2.picturepush.com/photo/a/12284140/640/12284140.jpg(word wrap)!
!http://www5.picturepush.com/photo/a/12284153/640/12284153.jpg!
!http://www2.picturepush.com/photo/a/12284155/640/12284155.jpg(word wrap anywhere)! -
wrote on 26 Feb 2013, 07:30 last edited by
try with Qt::WrapAtWordBoundaryOrAnywhere
-
wrote on 26 Feb 2013, 09:53 last edited by
Hi, this is not working in my case. Can u give me suggestion how to use it?
-
wrote on 26 Feb 2013, 09:55 last edited by
Hi, this is not working in my case. Can u give me suggestion how to use it?
1/4