Python Forum
[PyQt] Add a function in QtextBrowser - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [PyQt] Add a function in QtextBrowser (/thread-35014.html)



Add a function in QtextBrowser - catlessness - Sep-23-2021

This could be a general question about how to add a function in an existing class:

I want the textBrowser to scroll down to the bottom every time I append sth. ofc I cannot change QtextBrowser; Since the element textBrowser is created by the .ui file, which is automatically instantiated from the QtextBrowser, I cannot (I can but it seems to be troublesome) create a new class inheriting QtextBrowser to add a new func and instantiate a textBrowser from the new class either.

so is there a general way to add a function to an existing class?
OR
is there a way to make textBrowser automatically scroll down?

Many thanks,
Christopher


RE: Add a function in QtextBrowser - deanhystad - Sep-23-2021

That kind of thing does not belong in the class (plus it is really annoying). If you want to scroll to the bottom whenever you append text, use an intermediary function that does the append and the scrolling.

You can do what you want (modify method behavior in existing class) in Python. It is called "monkey patching". Now that you know what it's called you can find lots of examples and tutorials.


RE: Add a function in QtextBrowser - Axel_Erfurt - Sep-23-2021

You can convert your ui file to a py file , then you can work without the ui file

Example:

pyuic5 mainwindow.ui -o mainwindow.py -x


RE: Add a function in QtextBrowser - catlessness - Sep-25-2021

qwert
(Sep-23-2021, 06:31 PM)deanhystad Wrote: That kind of thing does not belong in the class (plus it is really annoying). If you want to scroll to the bottom whenever you append text, use an intermediary function that does the append and the scrolling.

You can do what you want (modify method behavior in existing class) in Python. It is called "monkey patching". Now that you know what it's called you can find lots of examples and tutorials.

Thank you that worked:
def AddnRefresh(self,content):
    self.append(content)
    self.verticalScrollBar().setValue(self.verticalScrollBar().maximum())
QTextBrowser.AddnRefresh=AddnRefresh

(Sep-23-2021, 07:03 PM)Axel_Erfurt Wrote: You can convert your ui file to a py file , then you can work without the ui file

Example:

pyuic5 mainwindow.ui -o mainwindow.py -x

Yeah I know i just skipped that part but thanks anyways