Results 1 to 4 of 4

Thread: Text block centering in Qt4.4 QTextEdit

  1. #1
    Join Date
    Jul 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Text block centering in Qt4.4 QTextEdit

    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!

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Text block centering in Qt4.4 QTextEdit

    How about the following -
    Qt Code:
    1. m_NewText = m_text + moreLines;
    2. QString data = QString("<center><table><tr><td>%1</td></tr></table></center>").arg(m_newText);
    To copy to clipboard, switch view to plain text mode 

    Hope you get the idea and it works for u

  3. #3
    Join Date
    Jul 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: Text block centering in Qt4.4 QTextEdit

    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!

  4. #4
    Join Date
    Jul 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: Text block centering in Qt4.4 QTextEdit

    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?

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 14:36
  3. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:05
  4. How to block the textedit'd text to be copied
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 2nd November 2007, 10:54
  5. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.