PDA

View Full Version : Text block centering in Qt4.4 QTextEdit



mla1290
9th July 2008, 23:56
Please excuse a possibly trivial/naive question -- I am on a very steep learning curve with Qt.

I am working on an app which maintains a scrolling output, implemented via QTextEdit. What I cannot work out (having been all over the Qt documentation) is how to implement the equivalent of the HTML <center><table><tr><td>some_text_lines</td></tr></table></center> (i.e. writing a block of text centered on the screen). The simplistic approach of actually inserting that HTML in a block of its own, doesn't *quite* work, for reasons which I do not understand. I do get a block of text offset towards the center of the display area, but not centered. Instead if I have multiple such blocks, they are all right(!)-aligned at or just past the center of the display.

What am I missing??

BTW, I've looked at using WebKit, but that doesn't seem to offer me the option of just inserting some text at the end of the page body. I've looked at the DOM stuff too, but that also doesn't offer a simple solution. Yet I don't think I am trying to do anything particularly unusual, so there almost certainly is a simple way to do it. It's just that I can't see it!

Help, please!

aamer4yu
10th July 2008, 06:39
How about the following -


m_NewText = m_text + moreLines;
QString data = QString("<center><table><tr><td>%1</td></tr></table></center>").arg(m_newText);

Hope you get the idea and it works for u :)

mla1290
10th July 2008, 15:14
Thanks, but that doesn't solve the problem. I have no trouble with constructing the string to insert-- it's just that it doesn't have the desired effect. Yes, I get effectively a single cell table with the lines of text left justified within it, but it is the alignment of the table which is wrong. The table itself is not centered but right-aligned at the center. I.e. if I output several such tables of different width, their right hand sides are aligned, not their centers!

mla1290
10th July 2008, 17:46
Sorry, I should have added... The tables which are meant to be centered are each in a separate QTextEdit blocks, because there is likely to be some non-centered text between them. And I should have also added that separate blocks with just "<center>some_text_lines</center> work just as I would expect them to. It's the addition of HTML tables that gives a strange effect.

To illustrate what I mean, here's some sample code:

cursor.insertBlock();
cursor.insertHtml(
"<center><table><tr><td>xx<br />xx</td></tr></table></center>");
cursor.insertBlock();
cursor.insertHtml(
"<center><table><tr><td>xxxxxx<br />xxxxxx</td></tr></table></center>");

The result is four lines of x-es near the center of the display, but I would expect the 3rd and the 4th lines to extend two x-es both to the left and to the right of the first two lines. Instead, the four lines are aligned on the right (at the center of the display), i.e. the bottom two lines extend by four x-es to the left.

If I remove the <br />, i.e. if I have a single line in both inserts, the resulting lines are centered exactly as I would expect. It's almost as if something calculated the alignment on the whole string, without allowing for it consisting of separate lines.

It gets stranger if I replace <br /> with </td></tr><tr><td>. That gives xx correctly centered, then blank line, than xxxxxx correctly centered and another blank line. I.e. two of the intended lines of text have been replaced by blank lines.

Completely baffled I am... Mebbe this is not the right approach?

How *should* I do the equivalent of a centered HTML table of one cell, containing multiple lines of left-aligned text?