PDA

View Full Version : QListWidget or QListView with QLabel(s)



lukass
7th August 2008, 18:47
Hello.

How can I insert QLabel(as item) to QListWidget (or QListView)? I want this for adding rich text messages to list widget.
Can anyone please post some examples?

wysota
7th August 2008, 21:21
Oh, that's a bad idea because of performance reasons. A much better one is to implement a custom delegate that would render rich text using QTextDocument. I think there's even an example of such a delegate on the forum somewhere.

lukass
8th August 2008, 11:52
Oh, that's a bad idea because of performance reasons. A much better one is to implement a custom delegate that would render rich text using QTextDocument. I think there's even an example of such a delegate on the forum somewhere.

You are thinking about this: http://www.qtcentre.org/forum/f-qt-programming-2/t-word-wrap-in-qlistview-8371.html?

wysota
8th August 2008, 12:20
Post #19 in the thread looks promising, although I was referring to some other thread. You can also download my wwWidgets and look at the QwwRichTextButton implementation and implement a similar thing as a delegate.

lukass
8th August 2008, 13:28
For first I must explain something.
I would like to do the chat widget.
So I started with the QTextBrowser.
After adding 80 lines(every line contained 30 pictures(16x16px gif)), the QTextBrowser turned out to be too slow(slow selecting text, slow srolling, etc.).
So perhaps would the QListWidget(with rich text items) be good for chat presentation?

OK. I downloaded http://www.qtcentre.org/forum/attachment.php?attachmentid=1523&d=1188673979.
I changed in maindialog.cpp lines:


<< "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:


<< "<font size=\"6\">The <b>QListWidgetItem</b> class."
<< "The <b>QObject</b> class is the base class of all Qt objects."
<< "Qt's <b>drag and drop</b>";
Result is wrong, because the text "The QListWidgetItem class." is covering the different text(See attachment.)

In sectionitem.cpp I changed in function:

QSize SectionItem::sizeHint(QObject *parent, 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());
}
one line:

int lines = int(ratio) + 2;
That works fine, but only when I don't resize widget and when text is "<font size=\"6\">The <b>QListWidgetItem</b> class.".

How to calculate the number of lines dynamically, if the text have different lenght and font sizes?

lukass
10th August 2008, 13:04
Can anyone help?

wysota
11th August 2008, 09:48
The text document can calculate the size of its contents, so just use it.