[Qt-interest] QTextCursor::insertBlock() at the beginning of a QTextEdit will prepend an empty line

Atlant Schmidt aschmidt at dekaresearch.com
Tue Jul 5 12:58:20 CEST 2011


Mahammed:

> Any better way ?

  Use Qt's ability to display HTML text?

  So without doing any fancy Qt-specific text formatting,
  just set the QTextEdit to contain:

    <center>Centered Line</center>Non centered line

  Remember, though, that if you don't explicitly tell
  Qt that this is an HTML text block, it will use its
  "heuristics" to determine whether it's plain text or
  HTML text. That means that you must have an HTML tag
  (of any kind) within the first line of the text you
  write to the block, otherwise Qt will consider it
  to be plain text and all of your HTML tags will be
  displayed rather than acted-upon.

                         Atlant


-----Original Message-----
From: qt-interest-bounces+aschmidt=dekaresearch.com at qt.nokia.com [mailto:qt-interest-bounces+aschmidt=dekaresearch.com at qt.nokia.com] On Behalf Of Mohammed Sameer
Sent: Sunday, July 03, 2011 12:48
To: qt-interest at trolltech.com
Subject: [Qt-interest] QTextCursor::insertBlock() at the beginning of a QTextEdit will prepend an empty line

Hi,

I'm trying to make the first line in a QTextEdit centered by using this sample code:

  QTextEdit e;
  QTextCursor cursor(e.textCursor());
  QTextBlockFormat fmt;
  fmt.setAlignment(Qt::AlignHCenter);
  cursor.insertBlock(fmt);
  cursor.insertText("Centered line");
  cursor.insertBlock(QTextBlockFormat());
  cursor.insertText("Non centered line");
  e.setTextCursor(cursor);

My problem is I get an empty line before the centered line. Seems the only way to
hack around it is to set the cursor position to 0 after I insert all the text
then use QTextCursor::deleteChar().

Any better way ?

Cheers,

--
GPG-Key: 0xA3FD0DF7 - 9F73 032E EAC9 F7AD 951F  280E CB66 8E29 A3FD 0DF7
Debian User and Developer.
Homepage: www.foolab.org
_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest

This e-mail and the information, including any attachments, it contains are intended to be a confidential communication only to the person or entity to whom it is addressed and may contain information that is privileged. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the sender and destroy the original message.

Thank you.

Please consider the environment before printing this email.



More information about the Qt-interest-old mailing list