Inserting html and plain text in QTextEdit
I think there might be a bug in QTextEdit and .insertHtml and .insertText methods.
Here an example:
Code:
import sys
from PyQt4 import *
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def __init__(self):
QWidget.__init__
(self, parent
=None
) self.resize(screen.width(), screen.height()/2)
self.move((screen.width() - self.geometry().width()) / 2, 0)
self.show()
self.cursor.insertHtml("<a href='http://www.w3schools.com/'>Link!</a>")
self.cursor.insertText("something")
def __init__(self):
self.gui = Gui()
window = Main()
sys.exit(app.exec_())
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?
Re: Inserting html and plain text in QTextEdit
This works fine on Qt4.6, Windows:
Code:
#include <QtCore>
#include <QtGui>
int main(int argc, char *argv[])
{
te.setHtml("<a href='http://www.w3schools.com/'>Link!</a>aah");
te.show();
return a.exec();
}
What version of Qt do you use?
Johannes
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.
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
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?
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?
Re: Inserting html and plain text in QTextEdit
No, it woesn't work :(
Could you try it to be sure?
1 Attachment(s)
Re: Inserting html and plain text in QTextEdit
Code:
#include <QtGui>
int main(int argc, char *argv[])
{
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();
}
Attachment 4499
So you are right.. this is unexpected beviour. But inserting as html seems to work..
BTW:
Is this correct for PyQt? Seems strange to me.
Johannes
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 ;)