Python Forum
[PyQt] Add a function in QtextBrowser
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Add a function in QtextBrowser
#1
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
Reply
#2
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.
catlessness likes this post
Reply
#3
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
Reply
#4
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020