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, August 31st 2004, 2:10pm

How to prevent QTextEdit from converting tags

Hello all!

There is one aspect of QTextEdit behavior that makes a problem for me. I mean auto-conversion of HTML formatting tags into corresponding font settings (font face, weight, etc.).

In my application, it is necessary to keep track of the current state of "style" of the text at cursor position, but if I create a document in QTextEdit containing e.g. <h1> tag, then it becomes quite difficult to match font parameters to <h1> when the cursor is in corresponding paragraph, and update "Paragraph style" combo box in a toolbar similarly to MS Word.

QTextEdit provides convenient means for keeping track of other simple text styles, like bold, italic, etc. But there's no support for even a bit more complex things like headings...why? It seems that there's no means to keep semantics of the original tag, as soon as it has been processed by QTextEdit.

Or maybe I don't know how to use it.

Any advice how to deal with my problem would be highly appreciated! Thanks in advance!

Sergey

2

Tuesday, August 31st 2004, 2:54pm

RE: How to prevent QTextEdit from converting tags

Just to make things clear, I would like to correct the title of my question:

"How to obtain initial HTML tags (like <h1>) from the text processed by QTextEdit?"

or even

"Is it possible to associate any user-defined information with arbitrary piece of text in QTextEdit, apart from font settings?"

djanubis

Professional

  • "djanubis" is male

Posts: 1,370

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

3

Tuesday, August 31st 2004, 4:04pm

RE: How to prevent QTextEdit from converting tags

Set property textFormat to PlainText:

myTextEdit->setTextFormat( Qt::PlainText ) ;
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

4

Wednesday, September 1st 2004, 6:44am

RE: How to prevent QTextEdit from converting tags

:-)
But I will see plain text with all tags instead of formatted text in such a case...this is not what the user expects from my application.

djanubis

Professional

  • "djanubis" is male

Posts: 1,370

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

5

Wednesday, September 1st 2004, 7:05am

RE: How to prevent QTextEdit from converting tags

Rereading the whole thing, sounds you should dig in Qt examples directory from your Qt source tree. In textedit, you get realy waht you want, with style, colors, font size, paragraph alignment...
You could enhance it with QStyleSheet class wich allows for user defined style sheets for most tags.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

6

Wednesday, September 1st 2004, 8:01am

RE: How to prevent QTextEdit from converting tags

Quoted

Originally posted by djanubis
Rereading the whole thing, sounds you should dig in Qt examples directory from your Qt source tree. In textedit, you get realy waht you want, with style, colors, font size, paragraph alignment...


This is what I always start from: studying Qt examples. Unfortunately, TextEdit example doesn't deal with headings For other things which can be easily bound to font parameters, QTextEdit does provide convenient interface, no doubt.

Quoted

You could enhance it with QStyleSheet class wich allows for user defined style sheets for most tags.


Yes. And no. QStyleSheet simply provides QTextEdit with bindings "tag name --> font/paragraph parameters" which are used immediately after adding some HTML text into the widget. The result is processed HTML text containing tags DIFFERENT from the original. Roughly, most tags are converted into font parameters or something like this, so any additional semantics contained in original tags is lost :-( Therefore, I cannot understand, if a cursor is currently within text fragment previously marked with <h1></h1> tags...

It would be great if Trolltech developers thought a while about this problem and invented some solution, that would satisfy both them and those who want to have original HTML tags - or some information about them in processed HTML text - avaiable.

djanubis

Professional

  • "djanubis" is male

Posts: 1,370

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

7

Wednesday, September 1st 2004, 8:21am

RE: How to prevent QTextEdit from converting tags

Could you send a sample of what you want with full explanation of the goal. Maybe a solution already exists.
Maybe you should use some QDomDocument or a context statck to deal with what you want.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

8

Wednesday, September 1st 2004, 12:04pm

RE: How to prevent QTextEdit from converting tags

Below is a sample user scenario:

1. User enters some text in QTextEdit
2. User selects some text fragment in QTextEdit and then selects "Heading 1" style for the selected text (among Normal, Heading 1, Heading 2, Heading 3) with help of additional "Style" combo box
3. User enters some more text -> "Normal" item becomes current in "Style" combo box (by default, Normal text style is used for new user input)
4. User moves a text cursor into the text fragment having "Heading 1" style -> "Heading 1" item becomes current in "Style" combo box (in other words, this combo box tracks the text style at the cursor position).

Of course, I can implement some workaround to achieve such behavior. But it would be more elegant if QTextEdit itself supported it.

djanubis

Professional

  • "djanubis" is male

Posts: 1,370

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

9

Wednesday, September 1st 2004, 1:40pm

RE: How to prevent QTextEdit from converting tags

This can be achieved by extending the textedit sample to have a style box and use the same mechanism as already used for other blocks.
You could also use a QSyntaxHighlighter for this purpose, but it's somewhat complex to implement.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

10

Wednesday, September 1st 2004, 2:30pm

RE: How to prevent QTextEdit from converting tags

Quoted

Originally posted by djanubis
This can be achieved by extending the textedit sample to have a style box and use the same mechanism as already used for other blocks.


QTextEdit provides no means to analyze such more or less complicated thing as "style". For bold, italic and friends - it does provide. But not for style.

djanubis

Professional

  • "djanubis" is male

Posts: 1,370

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

11

Wednesday, September 1st 2004, 3:07pm

RE: How to prevent QTextEdit from converting tags

In such a case, you can use the QSyntaxHighlighter to complete the thing. I know, syntax highlighting is a nighmare, but it works.
Else, you could find a word processor plugin, if such a beast is available.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

12

Thursday, September 2nd 2004, 6:13am

RE: How to prevent QTextEdit from converting tags

Thank you so much for making me completely sure that there are even several solutions for my problem!

Anyhow, please agree that it wouldn't be so bad if QTextEdit widget ITSELF provided us with some convenient means to do the thing, instead of being smart enough to convert almost all formatting tags to font settings...

djanubis

Professional

  • "djanubis" is male

Posts: 1,370

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

13

Thursday, September 2nd 2004, 6:22am

RE: How to prevent QTextEdit from converting tags

Yes, would be even better, but as most users only need some fancy and fast formating, standad QTextEdit is perfect for this. For more advanced uses, the tweaks are there, but not as well explained as standard behaviour.

You should have a look to the (yet to come) Qt 4 preview, with really enhanced widgets with bells and whistles. As even the interfaces are not yet stable, one should not use Qt 4 for production, but sounds impressive.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project