PDA

View Full Version : Editable forms with QTextEdit



kiozen
31st May 2011, 11:26
Hi,

I need some advice about the best approach to solve the following task with QTextEdit:

I have some GPS related data like waypoints and tracks. Each element is an object with information like a name and a comment. I would like to create a diary from that information. The easy way: read the information from all objects and convert it into HTML with tables and stuff. Works great.

However the next, natural desire is to edit the displayed information. Of course I could edit each object by a dialogue and recreate the HTML in the text edit again. It would be nice to edit the content of the diary in the QTextEdit and to write back the changes to the data objects.

Now I wonder what is the simplest approach to do so? Rendering the diary content via HTML is nice and easy. However reading back the changes is a bit complicated. It would take to parse the HTML, recognize the text elements and to map the found fragments with the originating data object. Ok can do, but this whole parsing thing sounds like asking for problems.

The other approach is to create the document from scratch by creating QTextBlock instances and so on. That would make the relation data object <-> text object a bit easier. But the diary creation will get really tough.

A nice idea would be to enclose the editable text with special tags and an id to get access to the underlying text block object. Something like that. But is it possible with less afford than the other two solutions? What do you think?

Thanks for help

Oliver

joyer83
31st May 2011, 12:16
The other approach is to create the document from scratch by creating QTextBlock instances and so on. That would make the relation data object <-> text object a bit easier. But the diary creation will get really tough.

Personally, I would go with this option. However, there are easier ways to generate the HTML code from your data than that. I would recommend that you check out some template engine libraries.
Here's a couple:
* Teng: http://teng.sourceforge.net
* Grantlee: http://www.gitorious.org/grantlee/pages/Home

kiozen
31st May 2011, 15:13
Grantlee looks interesting. I have to look a bit closer.

I was just playing around with the HTML/parser approach. Results are frustrating. In a table I do:



"<td><div id='xyz'>some text</div></td>"


I use setHtml() and then toHtml() to read it back. I get



<td bgcolor="#ffffff">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><a name="xyz"></a>some text</p></td>


QTextEdit makes my <div> to <a>:confused: An it does not enclose the text at all. Looks like this approach fails.

Added after 1 14 minutes:

Hm, this is more complicated than expected. This complete QText.... API is pretty irritating compared to the rest of Qt (which is from my of view very straight forward). Maybe someone can give me a slap to push me into the right direction. Here is what I try to do:



textEdit->clear();
QTextCursor cursor = textEdit->textCursor();
cursor.setBlockFormat(blockHeading1);
cursor.setCharFormat(textHeading1);
cursor.insertText(data.title);

diaryFrame = cursor.insertFrame(frameStandard);
{
QTextCursor cursor = diaryFrame->firstCursorPosition();
cursor.setBlockFormat(blockStandard);
cursor.setCharFormat(textStandard);
cursor.insertText(tr("Add your own text here..."));
}

cursor.insertBlock(blockHeading2, textHeading2);
cursor.insertText(tr("Waypoints"));


This will generate some text. In the frame "diaryFrame" the user can edit what ever is desired. The editor allows font selection, fontweight, color and all the usual stuff. In the end I want to read back the text within that frame as HTML. But how? Do I miss the obvious?

Oliver

MarekR22
31st May 2011, 16:08
In my opinion this kind of data should be presented in table/list or other kind of data view, so subclassing QAbstractItemModel or QAbstractTableModel is natural way to do it.
Creating own view or supporting new delegate to existing view should solve all you requirements in most effective way.

kiozen
31st May 2011, 17:59
Well, in fact, this is how the data is presented in the application (www.qlandkarte.org) right now. But the goal is to present it in a printable document style with some additional text. This is what is done right now. But many users wish to edit the data, e.g. a comment on a waypoint, directly in the document and expect the changes to be saved in the data object. Imho a good idea if the QTextEdit supports it.

joyer83
31st May 2011, 22:12
I checked from Qt's source code of what the toHtml() method does. Looks like it uses an internal class named QTextHtmlExporter to generate the HTML-code. That class just seems to iterate through those QTextBlocks and then turns each block into HTML-code and adds it to the HTML-output. So, if the default format of the toHtml() is not good enough, then you might need to write your own exporter.
The exporter is defined in src/gui/text/qtextdocument_p.h and src/gui/text/qtextdocument.cpp.

wysota
31st May 2011, 22:23
I would use QTextDocument API which is not that bad as you think, you just need to get familiar with it. It's a bit low-level, that's true. Anyway... if you want WYSIWYG then that's basically all -- you don't have to do anything more apart wrapping that API in a nice set of toolbar buttons. If you want some special treatment, I suggest to aid yourself with widgets shown either as dialogs or embedded in the text edit's viewport for manipulating properties and stuff like that. You can associate any data you want with each text block (using QTextBlockUserData) which can give you some additional benefits.

kiozen
1st June 2011, 08:41
joyer83, good idea! I just had a look at it too and extracted the class as 3rd party lib to my project. A few tweaks to silence calls into the private space of QTextDocument and a new method to export html for a QTextFrame and the thing works. I checked in the results to the project's repository if anyone comes across the same problem.

@wysota, there is nothing wrong with QTextEdit/QTextDocument and I really appreciate the ease to use it as editor. In my project I took over the Qt example for a light weight editor. I was just stunned that there is no easy way to read formatted text from the single parts of a document. The current assumption is, that a document is always stored as one html document. However if a document is created from application generated data as well as from user input via the editor a partly export to html is desired.

The current solution is not as nice as if the functionality would be in QTextDocument natively. But it's still a feasible, low effort way for my requirements.

Thanks everybody for the help!

Oliver

wysota
1st June 2011, 08:59
@wysota, there is nothing wrong with QTextEdit/QTextDocument and I really appreciate the ease to use it as editor. In my project I took over the Qt example for a light weight editor. I was just stunned that there is no easy way to read formatted text from the single parts of a document. The current assumption is, that a document is always stored as one html document.
I think the problem is with the assumption that it is stored as HTML. You lose a lot of info this way. It'd be better to store it in your own format and only to export it to HTML when requested.