You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Tuesday, November 22nd 2005, 9:32am

Rich Text in a table. Help!

In a table, I am trying to a create rows that can support multiple lines and different text formating in a single table item. Below is a attached screenshot of what I am trying to achieve.

Can anyone suggest a way to do this in Qt 4? It does not necessarily be a table, as long as it can support multiple columns. For example, I have looked at tableview, treeview, tablewidget and treewidget.

In Qt 3, I have managed to do this by refering to Trolltech's Fancy List Item's example in their Qt Quarterly (http://doc.trolltech.com/qq/qq08-fancy-list-view.html).

In the paintCell method, I added the following code to allow html tags in a cell:


// Create a rich text widget to allow html tags in the cell

QSimpleRichText* richText = new QSimpleRichText( RichListViewItem::text(0), fonts[column] );

richText->setWidth(width);

richText->draw( painter, 35, 0, QRegion(), grp );


However, QSimpleRichText has been moved to the Qt3Support library and I would prefer not to use the support library.

Any help would be greatly appreciated!
wkk1000 has attached the following file:

2

Friday, November 25th 2005, 2:31am

Chicken blood and wysota + michaeln;

Pls help...:( im having some problems while writing the rich text row ..need advise...

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

3

Friday, November 25th 2005, 8:01am

There are multiple ways to do it. One of them is using a simple model and a QTreeView or QTableView with a custom delegate. I guess you can also use QTreeWidget or QTableWidget. Using QTextDocument is a way too, rendering the text on a widget directly can also be an option... As I said, you can do it in many ways :)

4

Friday, December 2nd 2005, 3:54am

Examples codes please.

Hi wysota,michaeln, chickenblood ,

The QT saga gurus, please give me some ideas on how to create my own rich text ..as u mentioned last reply. If possible , attach with some SAMPLES CODES to be shared. Appreciate.
Thanks.

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

5

Friday, December 2nd 2005, 7:44am

RE: Examples codes please.

Maybe you should try something of your own first? :)

6

Saturday, December 3rd 2005, 12:30am

As wysota said there is more than one way. As a general rule there is always more than one way to skin a cat as the saying goes. Try reading the Qt documentation. You might be suprised with what you come up with.

There is an old saying. Give a man a fish you feed him for a day teach him to fish and you feed him for a lifetime.

The Qt Documentation is your friend, and is really easy to understand as well.
Visit http://www.qtcentre.org where the owners care about the future of the forums.

hojtsy

Trainee

  • "hojtsy" is male

Posts: 65

Location: Hungary

Occupation: Software Developper

  • Send private message

7

Monday, December 5th 2005, 12:12am

I tried to put together a working example. It involves subclassing QItemDelegate, and rendering rich text inside it's paint method instead of plain text. I attempted to render rich text this way:

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
    QString text = model->data(index, Qt::DisplayRole).toString();
    
    QTextDocument textDocument;
    textDocument.setHtml(text);

    QTextLayout textLayout(textDocument.begin());
    textLayout.setFont(opt.font);

    textLayout.beginLayout();
    qreal height = 0;
    while (1) {
        QTextLine line = textLayout.createLine(); // CRASHES HERE
        if (!line.isValid())
            break;

        line.setLineWidth(100); // just dummy estimate
        height += 5; // just dummy estimate
        line.setPosition(QPoint(0, height));
        height += line.height();
    }
    textLayout.endLayout();

    ....
    textLayout.draw(painter, QPointF(textRect.left(), textRect.top()));
But it crashes on the first call to createLine, and I don't know why. Notice that I took the example code from the class docs of QTextLayout, and that one contained an invalid reference to the non-existing QTextLine::layout() method. I corrected that one to setLineWidth(), but the execution does not reach that point anyway.

hojtsy

Trainee

  • "hojtsy" is male

Posts: 65

Location: Hungary

Occupation: Software Developper

  • Send private message

8

Tuesday, December 6th 2005, 4:27pm

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 ();
}

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

9

Tuesday, December 6th 2005, 5:34pm

Now what if you don't use richtext directly, but cope with less fancy items without the use of QTextLayout? QPainter::drawText is still there...

10

Friday, December 16th 2005, 6:38am

thanks for ur reply.