Hi,
I'm planning use two different delegate to draw items so I choose QListWidget. For providing support to the two delegates I created a new class, named ListWidgetItemDelegate (like StarDelegate example in the manual). Now from the MainDialog I can decide which delegate using. All works fine. I then decided to modify one of the delegate for supporting the marcel's word-wrap solution:
QSize SectionItem
::sizeHint(const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const {
QString text
= index.
data().
toString();
float rw = float(p->viewport()->size().width());
float tw = fm.width(text);
float ratio = tw/rw;
int lines = int(ratio) + 1;
return QSize(rw,lines
*fm.
height());
}
QSize SectionItem::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QListWidget *p = (QListWidget*)parent();
QString text = index.data().toString();
QFontMetrics fm(p->font());
float rw = float(p->viewport()->size().width());
float tw = fm.width(text);
float ratio = tw/rw;
int lines = int(ratio) + 1;
return QSize(rw,lines*fm.height());
}
To copy to clipboard, switch view to plain text mode
but i get an error during compiling because parent is not declared in the scope. Now from maindialog passing by ListWidgetItemDelegate to SectionItem I'm little lost and i don't know which row modify for passing the right parent.
Here the code that have used hoping someone, read marcel, can help me:
listwidgetitemdelegate.cpp
#include "listwidgetitemdelegate.h"
#include "headeritem.h"
#include "sectionitem.h"
{
if (qVariantCanConvert<HeaderItem>(index.data()))
{
HeaderItem headerItem = qVariantValue<HeaderItem>(index.data());
headerItem.paint(painter, option, index);
}
else if (qVariantCanConvert<SectionItem>(index.data()))
{
SectionItem sectionItem = qVariantValue<SectionItem>(index.data());
sectionItem.paint(painter, option, index);
}
else
{
}
}
{
if (qVariantCanConvert<SectionItem>(index.data())) {
SectionItem sectionItem = qVariantValue<SectionItem>(index.data());
return sectionItem.sizeHint(option, index);
} else {
}
}
#include "listwidgetitemdelegate.h"
#include "headeritem.h"
#include "sectionitem.h"
void ListWidgetItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (qVariantCanConvert<HeaderItem>(index.data()))
{
HeaderItem headerItem = qVariantValue<HeaderItem>(index.data());
headerItem.paint(painter, option, index);
}
else if (qVariantCanConvert<SectionItem>(index.data()))
{
SectionItem sectionItem = qVariantValue<SectionItem>(index.data());
sectionItem.paint(painter, option, index);
}
else
{
QItemDelegate::paint(painter, option, index);
}
}
QSize ListWidgetItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (qVariantCanConvert<SectionItem>(index.data())) {
SectionItem sectionItem = qVariantValue<SectionItem>(index.data());
return sectionItem.sizeHint(option, index);
} else {
return QItemDelegate::sizeHint(option, index);
}
}
To copy to clipboard, switch view to plain text mode
sectionitem.cpp
#include "sectionitem.h"
SectionItem
::SectionItem(QString text
){
itemText = text;
}
SectionItem::~SectionItem()
{
}
{
painter->save();
painter
->setPen
(QPen(Qt
::darkGreen));
painter->drawText(option.rect, Qt::AlignLeft | Qt::TextWordWrap, itemText);
painter->restore();
}
QSize SectionItem
::sizeHint(const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const {
float rw = float(p->viewport()->size().width());
float tw = fm.width(itemText);
float ratio = tw/rw;
int lines = int(ratio) + 1;
return QSize(rw,lines
*fm.
height());
}
#include "sectionitem.h"
SectionItem::SectionItem(QString text)
{
itemText = text;
}
SectionItem::~SectionItem()
{
}
void SectionItem::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex & index) const
{
painter->save();
painter->setPen(QPen(Qt::darkGreen));
painter->drawText(option.rect, Qt::AlignLeft | Qt::TextWordWrap, itemText);
painter->restore();
}
QSize SectionItem::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QListWidget *p = (QListWidget*)parent();
QFontMetrics fm(p->font());
float rw = float(p->viewport()->size().width());
float tw = fm.width(itemText);
float ratio = tw/rw;
int lines = int(ratio) + 1;
return QSize(rw,lines*fm.height());
}
To copy to clipboard, switch view to plain text mode
maindialog.cpp
...
listWidget->setItemDelegate(new ListWidgetItemDelegate);
listWidget->setAlternatingRowColors(true);
list << "New Section"
<< "The <b>QListWidgetItem</b> class provides an item for use with the QListWidget item view class."
<< "The <b>QObject</b> class is the base class of all Qt objects."
<< "Qt's <b>drag and drop</b> infrastructure is fully supported by the model/view framework. Items in lists, tables, and trees can be dragged within the views, and data can be imported and exported as MIME-encoded data. ciao da giuseppe";
item1->setData(0, qVariantFromValue(HeaderItem(list.at(0))));
listWidget->insertItem(0, item1);
for(int i = 1; i < list.size(); i++)
{
item->setData(0, qVariantFromValue(SectionItem(list.at(i))));
listWidget->insertItem(i, item);
}
...
...
listWidget = new QListWidget;
listWidget->setItemDelegate(new ListWidgetItemDelegate);
listWidget->setAlternatingRowColors(true);
listWidget->setResizeMode(QListWidget::Adjust);
QStringList list;
list << "New Section"
<< "The <b>QListWidgetItem</b> class provides an item for use with the QListWidget item view class."
<< "The <b>QObject</b> class is the base class of all Qt objects."
<< "Qt's <b>drag and drop</b> infrastructure is fully supported by the model/view framework. Items in lists, tables, and trees can be dragged within the views, and data can be imported and exported as MIME-encoded data. ciao da giuseppe";
QListWidgetItem *item1 = new QListWidgetItem;
item1->setData(0, qVariantFromValue(HeaderItem(list.at(0))));
listWidget->insertItem(0, item1);
for(int i = 1; i < list.size(); i++)
{
QListWidgetItem *item = new QListWidgetItem;
item->setData(0, qVariantFromValue(SectionItem(list.at(i))));
listWidget->insertItem(i, item);
}
...
To copy to clipboard, switch view to plain text mode
Regards
Bookmarks