[Qt-interest] QTextBrowser in a QListWidget

Mark Summerfield list at qtrac.plus.com
Tue Mar 24 08:18:06 CET 2009


On 2009-03-24, wim.delvaux at adaptiveplanet.com wrote:
> Hi all,
>
> I want to show rich text as content for the rows of a list widget.
> I have inserted a QTextBrowser using setItemWidget().
> Then I set the text.  This works fine.
>
> However the vertical size of the QTextBrowser is small (it shows
> scrollbars). I want the size of the textbrowser to be as large as its
> content and to make sure the row height of the corresponding qlistwidget
> cell to show the complete QTextBrowser.
>
> How can I do that ?
>
> If there are other - more elegant/performant/efficient - ways to show rich
> text in a QListWidget I would love to have references/example code.

That seems a pretty heavyweight approach.

I'd use a QListView with a custom delegate. If you only need read-only
data you could just reimplement the delegate's paint method and either
use a QTextDocument and render() it to the painter, or (IMO easier) use
a QLabel and QWidget::grabPixmap().

I show this technique (as part of an editable rich text editor for list
items and table cells) in my book "Rapid GUI Programming with Python and
Qt" - ISBN 0132354187; I also cover the technique (using C++/Qt) in my
"Qt 4's Model/View Delegates" whitepaper which is hosted by ICS:
http://www.ics.com/learning/learning_center_downloads/
(Note that since that paper came out Qt has added support for row and
column delegates, so the generic delegate the paper describes is no
longer needed---but the column delegates shown are fine and include one
for presenting and editing Qt rich text (simple HTML).)

And even if you didn't use a delegate I think the code shows how to
switch off the scrollbars etc., so it should give you some useful ideas.
(I hope to present an even nicer version in my new C++/Qt book, but that
won't be out for another year.)

-- 
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
    C++, Python, Qt, PyQt - training and consultancy
	"C++ GUI Programming with Qt 4" - ISBN 0132354160




More information about the Qt-interest-old mailing list