Well, I have some new and some code to post.
Following marcel's suggestion I have modified the stardelegate example removing the editor and related parts. I have changed the parameter passed to StarRating (from int, the rating, to QString, the text to wordwrap) and used the code from this post; in this way the tableWidget contains a rich text column and long text is wrapped but it overlaps rows below: the cell height is not updated to enclose the multi-line text (hoping i was clear!).
Then i have made a new copy of the source code and substituted QTableWidget with QListWidget; after some editing i have obtained a working application but without wordwrap (it used a horizontal scrollbar if text is too long).
My questions are:
1) how disable scrollbar in qlistwidget and fix the width forcing word wrap?
2) how change row height according wordwrapping?
The application, like Star Delegate example, has 2 classes:
- TextDelegate.
- TextPainter.
Here the code (sorry for the long post):
textdelegate.h
#ifndef TEXTDELEGATE_H
#define TEXTDELEGATE_H
#include <QItemDelegate>
{
Q_OBJECT
public:
};
#endif
#ifndef TEXTDELEGATE_H
#define TEXTDELEGATE_H
#include <QItemDelegate>
class TextDelegate : public QItemDelegate
{
Q_OBJECT
public:
TextDelegate(QWidget *parent = 0) : QItemDelegate(parent) {}
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const;
};
#endif
To copy to clipboard, switch view to plain text mode
textdelegate.cpp
#include <QtGui>
#include "textdelegate.h"
#include "textpainter.h"
{
if (qVariantCanConvert<TextPainter>(index.data())) {
TextPainter textPainter = qVariantValue<TextPainter>(index.data());
if (option.
state & QStyle::State_Selected) painter->fillRect(option.rect, option.palette.highlight());
textPainter.paint(painter, option, TextPainter::ReadOnly);
} else {
}
}
{
if (qVariantCanConvert<TextPainter>(index.data())) {
TextPainter textPainter = qVariantValue<TextPainter>(index.data());
return textPainter.sizeHint();
} else {
}
}
#include <QtGui>
#include "textdelegate.h"
#include "textpainter.h"
void TextDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (qVariantCanConvert<TextPainter>(index.data())) {
TextPainter textPainter = qVariantValue<TextPainter>(index.data());
if (option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.highlight());
textPainter.paint(painter, option, TextPainter::ReadOnly);
} else {
QItemDelegate::paint(painter, option, index);
}
}
QSize TextDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (qVariantCanConvert<TextPainter>(index.data())) {
TextPainter textPainter = qVariantValue<TextPainter>(index.data());
return textPainter.sizeHint();
} else {
return QItemDelegate::sizeHint(option, index);
}
}
To copy to clipboard, switch view to plain text mode
textpainter.h
#ifndef TEXTPAINTER_H
#define TEXTPAINTER_H
#include <QMetaType>
#include <QString>
class TextPainter
{
public:
enum EditMode { Editable, ReadOnly };
private:
};
Q_DECLARE_METATYPE(TextPainter)
#endif
#ifndef TEXTPAINTER_H
#define TEXTPAINTER_H
#include <QMetaType>
#include <QString>
class TextPainter
{
public:
enum EditMode { Editable, ReadOnly };
TextPainter(QString text = "");
void paint(QPainter *painter, const QStyleOptionViewItem &option, EditMode mode) const;
QSize sizeHint() const;
private:
QString itemText;
};
Q_DECLARE_METATYPE(TextPainter)
#endif
To copy to clipboard, switch view to plain text mode
textpainter.cpp
#include <QtGui>
#include "textpainter.h"
const int PaintingScaleFactor = 20;
TextPainter
::TextPainter(QString text
){
itemText = text;
}
QSize TextPainter
::sizeHint() const {
return PaintingScaleFactor
* QSize(itemText.
length()/2-2,
1);
}
{
if (mode == Editable) {
painter->setBrush(option.palette.highlight());
} else {
painter->setBrush(option.palette.foreground());
}
painter->save();
doc.setHtml(itemText);
doc.setPageSize( option.rect.size());
painter->translate(option.rect.x(), option.rect.y()-20);
doc.documentLayout()->draw(painter, context);
painter->restore();
}
#include <QtGui>
#include "textpainter.h"
const int PaintingScaleFactor = 20;
TextPainter::TextPainter(QString text)
{
itemText = text;
}
QSize TextPainter::sizeHint() const
{
return PaintingScaleFactor * QSize(itemText.length()/2-2, 1);
}
void TextPainter::paint(QPainter *painter, const QStyleOptionViewItem &option, EditMode mode) const
{
if (mode == Editable) {
painter->setBrush(option.palette.highlight());
} else {
painter->setBrush(option.palette.foreground());
}
painter->save();
QTextDocument doc;
doc.setHtml(itemText);
QAbstractTextDocumentLayout::PaintContext context;
doc.setPageSize( option.rect.size());
painter->translate(option.rect.x(), option.rect.y()-20);
doc.documentLayout()->draw(painter, context);
painter->restore();
}
To copy to clipboard, switch view to plain text mode
Bookmarks