Hi,
Just noticing you that Trolltech confirmed both the documentation bug in QTextLayout, and the crash while calling QTextLayout::createLine() from a QItemDelegate subclass. After this it is quite a question what "many ways" are left to go if I want rich text in itemview cells?
1) Subclassing QItemDelegate was the only realistic way, but crashes during laying out the text.
2) QTextDocument can not render text, so I don't really see how does that qualify as an independent alternative. You will rather need to use QTextDocument as part of an other alternative.
3) Own custom widget. Seems quite far fetched to reimplement most of what QItemView does (scrolling, dragging, cell resizing, selection modes, editors, etc) just to replace the cell plain text with fancy text.
4) QTextEdit or QTextBrowser does not provide many itemview features (scrolling, dragging, cell resizing, selection modes, editors, etc)
5) Sorry but the only way I found to remain is opening persistent editors on all fields, and display each cell in a separate widget. Some of the many drawbacks: cursor movement will only move within the current cell, selected fields will not look different, and drag-selection will only select within a single cell, even if you drag throught multiple cells.
6) AFAIK Qt 4.1 will offer a feature to directly place widgets in itemview cells, but the same drawbacks remain as in #5.
If you are interested here is the minimal dummy example which can reproduce the mentioned crash.
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include <QApplication>
#include <QtGui>
class RichDelegate : public QItemDelegate
{
public:
void paint ( QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const;
};
void RichDelegate::paint(
QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const
{
// stripped down to the minimum sufficient to reproduce the crash
QString text = "<b>Test</b> text";
QTextDocument textDocument;
textDocument.setHtml(text);
QTextLayout textLayout(textDocument.begin());
textLayout.beginLayout();
//QTextLine line = textLayout.createLine(); // UNCOMMENT THIS TO CRASH
textLayout.endLayout();
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTableView mainWindow;
QStringList cells = QStringList() << "<b>Dummy field</b>";
QStringListModel model(cells, &mainWindow);
mainWindow.setModel(&model);
RichDelegate richDelegate;
mainWindow.setItemDelegate(&richDelegate);
mainWindow.show();
return app.exec ();
}
|