PDA

View Full Version : Inserting html and plain text in QTextEdit



alexandernst
13th April 2010, 18:51
I think there might be a bug in QTextEdit and .insertHtml and .insertText methods.

Here an example:



import sys
from PyQt4 import *
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Gui(QWidget):
def __init__(self):
QWidget.__init__(self, parent=None)
screen = QDesktopWidget().screenGeometry()
self.resize(screen.width(), screen.height()/2)
self.move((screen.width() - self.geometry().width()) / 2, 0)
self.textArea = QTextEdit(self)
self.cursor = QTextCursor(self.textArea.document())
self.show()
self.cursor.insertHtml("<a href='http://www.w3schools.com/'>Link!</a>")
self.cursor.insertText("something")

class Main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self, parent=None)
self.gui = Gui()



app = QtGui.QApplication(sys.argv)
window = Main()
sys.exit(app.exec_())


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

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

JohannesMunk
13th April 2010, 19:48
This works fine on Qt4.6, Windows:



#include <QtCore>
#include <QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QTextEdit te;
te.setHtml("<a href='http://www.w3schools.com/'>Link!</a>aah");
te.show();
return a.exec();
}


What version of Qt do you use?

Johannes

alexandernst
13th April 2010, 19:59
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.

JohannesMunk
13th April 2010, 20:07
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

alexandernst
13th April 2010, 20:16
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?

JohannesMunk
13th April 2010, 20:20
I don't understand. Why don't you just add it as html? The same text. Just the other method. Does it work then?

alexandernst
13th April 2010, 20:25
No, it woesn't work :(
Could you try it to be sure?

JohannesMunk
13th April 2010, 20:34
#include <QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QTextEdit te;
QTextCursor cursor = te.textCursor();
cursor.insertHtml("<a href='http://www.w3schools.com/'>Link!</a>oops<br>");
cursor.insertHtml("<a href='http://www.w3schools.com/'>Link!</a>");
cursor.insertHtml("something");
cursor.insertHtml("<br>");
cursor.insertHtml("<a href='http://www.w3schools.com/'>Link!</a>");
cursor.insertText("something");
te.show();
return a.exec();
}
4499
So you are right.. this is unexpected beviour. But inserting as html seems to work..

BTW:

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

Is this correct for PyQt? Seems strange to me.

Johannes

alexandernst
13th April 2010, 20:53
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 ;)