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?
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?