Results 1 to 9 of 9

Thread: Inserting html and plain text in QTextEdit

  1. #1
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Exclamation Inserting html and plain text in QTextEdit

    I think there might be a bug in QTextEdit and .insertHtml and .insertText methods.

    Here an example:

    Qt Code:
    1. import sys
    2. from PyQt4 import *
    3. from PyQt4 import QtCore
    4. from PyQt4 import QtGui
    5. from PyQt4.QtCore import *
    6. from PyQt4.QtGui import *
    7.  
    8. class Gui(QWidget):
    9. def __init__(self):
    10. QWidget.__init__(self, parent=None)
    11. screen = QDesktopWidget().screenGeometry()
    12. self.resize(screen.width(), screen.height()/2)
    13. self.move((screen.width() - self.geometry().width()) / 2, 0)
    14. self.textArea = QTextEdit(self)
    15. self.cursor = QTextCursor(self.textArea.document())
    16. self.show()
    17. self.cursor.insertHtml("<a href='http://www.w3schools.com/'>Link!</a>")
    18. self.cursor.insertText("something")
    19.  
    20. class Main(QMainWindow):
    21. def __init__(self):
    22. QMainWindow.__init__(self, parent=None)
    23. self.gui = Gui()
    24.  
    25.  
    26.  
    27. app = QtGui.QApplication(sys.argv)
    28. window = Main()
    29. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    A normal webbrowser will render "<a href='http://www.w3schools.com/'>Link!</a>something" as "Link!something", but instead of that QTextEdit renders it as "Link!something"

    Where is the problem? And how can I prevent QTextEdit acting like that?

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Inserting html and plain text in QTextEdit

    This works fine on Qt4.6, Windows:

    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. te.setHtml("<a href='http://www.w3schools.com/'>Link!</a>aah");
    9. te.show();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    What version of Qt do you use?

    Johannes

  3. #3
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Inserting html and plain text in QTextEdit

    I know that setting it all together it will work. But I want to be able to set html data, and then append plain text, then html data, plain text, etc etc etc...
    I'm using 4.6 on Arch.

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Inserting html and plain text in QTextEdit

    You can add your plaintext as html code and it will be shown as plaintext, as long as it doesn't contain any html-formatting.. So you can compile one big QString with all your links and plaintext and set it as html-text of your textedit. Or do you intend to ignore the html-tags of your plaintext? Does that even work?

    Johannes

  5. #5
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Inserting html and plain text in QTextEdit

    I can't do that because I don't know what will be the next string that I'd append. That's why I need to be able to insertHtml and PlaintText.
    Is there any way to force PlainText or something?

  6. #6
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Inserting html and plain text in QTextEdit

    I don't understand. Why don't you just add it as html? The same text. Just the other method. Does it work then?

  7. #7
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Inserting html and plain text in QTextEdit

    No, it woesn't work
    Could you try it to be sure?

  8. #8
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Inserting html and plain text in QTextEdit

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. QTextCursor cursor = te.textCursor();
    8. cursor.insertHtml("<a href='http://www.w3schools.com/'>Link!</a>oops<br>");
    9. cursor.insertHtml("<a href='http://www.w3schools.com/'>Link!</a>");
    10. cursor.insertHtml("something");
    11. cursor.insertHtml("<br>");
    12. cursor.insertHtml("<a href='http://www.w3schools.com/'>Link!</a>");
    13. cursor.insertText("something");
    14. te.show();
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 
    LinkEdit..png
    So you are right.. this is unexpected beviour. But inserting as html seems to work..

    BTW:
    Qt Code:
    1. self.cursor = QTextCursor(self.textArea.document())
    To copy to clipboard, switch view to plain text mode 
    Is this correct for PyQt? Seems strange to me.

    Johannes
    Last edited by JohannesMunk; 13th April 2010 at 19:38.

  9. #9
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Inserting html and plain text in QTextEdit

    I guess I'll be using only .insertHtml

    About the self.cursor = QTextCursor(self.textArea.document())

    textArea is a QTextEdit, and .document() returns a QTextDocument.
    On the other hand, QTextCursor can be initialized with a QTextDocument, so, yes, it's correct (at least according to docs )
    QTextCursor can also be inialized with QTextFrame, QTextCursor or without args.


    Thanks for the help

Similar Threads

  1. How to convert text to HTML in QTextEdit
    By Roszko in forum Newbie
    Replies: 5
    Last Post: 31st December 2009, 09:40
  2. put html text into a qtextedit
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2008, 19:44
  3. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 16:45
  4. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  5. Problem with inserting text into QTextEdit
    By xorrr in forum Qt Programming
    Replies: 0
    Last Post: 6th February 2006, 11:45

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.