In ListWidgetItemDelegate::sizeHint you can pass parent() to SectionItem::sizeHint(). All you have to do is to add another parameter to SectionItem's sizeHint method.
SectionItem is not a QAbstractItemDelegate, isn't it?
Regards
In ListWidgetItemDelegate::sizeHint you can pass parent() to SectionItem::sizeHint(). All you have to do is to add another parameter to SectionItem's sizeHint method.
SectionItem is not a QAbstractItemDelegate, isn't it?
Regards
No marcel, SectionItem is not a QAbstractItemDelegate. Now I edit the code and then tell you the result.
Thanks
Giuseppe CalÃ
Yes. Just make sure you pass the parent of ListWidgetItemDelegate.
Regards
I'm making some sort of mistake because i got errors during compiling; here the modifications:
listwidgetitemdelegate.cpp
Qt Code:
... return sectionItem.sizeHint(parent, option, index); ...To copy to clipboard, switch view to plain text mode
sectionitem.h
Qt Code:
... ...To copy to clipboard, switch view to plain text mode
sectionitem.cpp
Qt Code:
{...}To copy to clipboard, switch view to plain text mode
For clearness here the code for the header files:
listwidgetitemdelegate.h
Qt Code:
{ Q_OBJECT public: };To copy to clipboard, switch view to plain text mode
sectionitem.h
Qt Code:
class SectionItem { public: ~SectionItem(); private: QString itemText; }; Q_DECLARE_METATYPE(SectionItem)To copy to clipboard, switch view to plain text mode
Bye
Giuseppe CalÃ
Who is parent? The one that you pass to SectionItem::sizeHint().
It should be (QListWidget*)parent().
And in maindialog.cpp, it should be:
Qt Code:
listWidget->setItemDelegate(new ListWidgetItemDelegate(listWidget));To copy to clipboard, switch view to plain text mode
jiveaxe (1st September 2007)
Ok marcel, now works! Here the modifications I made:
maindialog.cpp
Qt Code:
listWidget->setItemDelegate(new ListWidgetItemDelegate(listWidget));To copy to clipboard, switch view to plain text mode
listwidgetitemdelegate.cpp
Qt Code:
... return sectionItem.sizeHint(parent(), option, index); ...To copy to clipboard, switch view to plain text mode
sectionitem.h
Qt Code:
To copy to clipboard, switch view to plain text mode
sectionitem.cpp
Qt Code:
{ ... }To copy to clipboard, switch view to plain text mode
Thanks a lot
Giuseppe CalÃ
I would like rendering html text instead of plain text; changing SectionItem:aint like this:
Qt Code:
void SectionItem::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex & index) const { painter->save(); QTextDocument doc; doc.setHtml(itemText); doc.setPageSize(option.rect.size()); painter->translate(option.rect.x(), option.rect.y()); doc.documentLayout()->draw(painter, context); painter->restore(); // painter->drawText(option.rect, Qt::AlignLeft | Qt::TextWordWrap, index.data().toString()); }To copy to clipboard, switch view to plain text mode
It renders html text but the last row of each phrase (when wrapped) is always drawn in the following item and a blank row remains in own item (see the attached image for better explanation).
Any hint?
Cheers
Giuseppe CalÃ
I have found a possible solution even if for some widths it renders not optimally. Attached are 2 images, one with correct draw and one with the unwanted behavior.
Here the new code for SectionItem:aint
Qt Code:
void SectionItem::paint(QObject *parent, QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex & index) const { painter->save(); QTextDocument doc; doc.setHtml(itemText); doc.setPageSize(QSize(option.rect.size().width(), option.rect.size().height() + p->viewport()->size().height())); painter->translate(option.rect.x(), option.rect.y()); doc.documentLayout()->draw(painter, context); painter->restore(); }To copy to clipboard, switch view to plain text mode
Regards
Giuseppe CalÃ
It might have to do with the HTML itself. Do you have any <p> tags or any other tag that enforces spacings around it?
Can you paste the html?
BTW: don't tell me that you too are working on (yet another) IDE.
Regards
There aren't <p> tags in the html, only <b> and </b>, but here the code:
Qt Code:
QStringList list; list << "New Section" << "The <b>QListWidgetItem</b> class provides an item for use with the QListWidget item view class. The <b>QListWidgetItem</b> class provides an item for use with the QListWidget item view class. 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.";To copy to clipboard, switch view to plain text mode
...and not, I am not working on a new ide.
Bye
Giuseppe CalÃ
I can't say what's wrong.
Maybe if you can post a minimal example or event the whole code, so I can be able to test it.
Regards
Attached there is the code.
Thanks for your time
Giuseppe CalÃ
Maxz (12th August 2009)
Qt Code:
void SectionItem::paint(QObject *parent, QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex & index) const { painter->save(); QTextDocument doc; doc.setHtml(itemText); doc.setTextWidth(option.rect.size().width()); painter->translate(option.rect.x(), option.rect.y()); doc.documentLayout()->draw(painter, context); painter->restore(); } { float rw = float(p->viewport()->size().width()); float tw = fm.width(itemText); float ratio = tw/rw; int lines = 0; if(ratio - int(ratio) < 0.1f) lines = int(ratio); else lines = int(ratio) + 1; }To copy to clipboard, switch view to plain text mode
Problem 1: don't give the height to the text document. you already set it with the size hint.
Problem 2: ocassionaly, due to rounding errors, you got 1 extra line of text that remained empty( the +1 in the sizeHint).
Regards
jiveaxe (1st September 2007), liuyanghejerry (26th August 2010)
Thank for your tips, marcel, helpful as usual; I have corrected the code.
Regards
Giuseppe CalÃ
Bookmarks