Reputation: 37
I have created a Wizard in PyQt4 using Qt Designer
. On one page of the wizard ,there exists a 'text_Browser' object of type QTextBrowser
. I'm using the function QTextBrowser.append()
to add text to it based on some processing.
I wish to execute the append function after the display of this page rather than the connecting the action(signal) to the Next or any other buttons on the previous page . How do I go about doing this ?
Upvotes: 2
Views: 3328
Reputation: 14584
You can reimplement showEvent
in the QTextBrowser.
# from PySide import QtGui
from PyQt4 import QtGui
class CustomTextBrowser(QtGui.QTextBrowser):
'''Reimplment show event to append text'''
def showEvent(self, event):
# do stuff here
#self.append('This random text string ')
event.accept()
Please be warned that this will append to the QTextBrowser's string every time the widget is shown, meaning other Qt events that toggle the visibility of this widget may cause unexpected behavior. Using signals and slots is preferable for this reason, but since you explicitly not to use signal/slots, here is a QEvent version on the showEvent
with a fair warning.
One solution to avoid appending text multiple times would be to set an instance variable, and toggle the value after the widget has been shown:
# from PySide import QtGui
from PyQt4 import QtGui
class CustomTextBrowser(QtGui.QTextBrowser):
'''Reimplment show event to append text'''
def __init__(self, *args, **kwds):
super(CustomTextBrowser, self).__init__(*args, **kwds)
self._loaded = False
def showEvent(self, event):
if not self._loaded:
# do stuff here
#self.append('This random text string ')
self._loaded = True
event.accept()
Another solution would be to use the signal/slot strategy as mentioned above, or to override __init__
to automatically append text in your subclass. Probably the signal/slot mechanism is the most intuitive, and logical for Qt programmers.
Upvotes: 2