Search code examples
pythonpython-3.xpyqtpyqt5qscrollarea

How to determine whether the scrollbar of scrollArea is visible in PyQt5?


Well. The question seems to be very easy, but the result I get is different from what I expect to.

I have an app, which height depends on its content. Here is auto-resize code:

def resizeApp(self):
    n = len(self.listOfWidgets) 
    if self.screenHEIGHT - 40 > n * 125 + 50:
        self.resize(self.width(), n * 125 + 50)
        self.scrollArea.resize(self.scrollArea.width(), n * 125 + 20)
    else:
        self.resize(self.width(), self.screenHEIGHT - 40)
        self.scrollArea.resize(self.scrollArea.width(), self.screenHEIGHT - 40 - 30)
    print(self.scrollArea.verticalScrollBar().isVisible())

You can see that after the resize we can define the visibility of the verticalScrollbar. Look at the app and console screenshot. We can see the that app has verticalScrollbar, but console says that it doesn't. It seems isVisible() lags a bit and tells me previous state.

For example. When I add widgets, while there is no verticalScrollbar, the console says False (verticalScrollbar is not visible). When the amount of widgets requires verticalScrollbar, scrollArea creates this verticalScrollbar, however I receive message that the verticalScrollbar is not visible. And later, after adding one more widget, I get message that the verticalScrollbar is visible.

And vice versa. When I delete widgets, I receive message that the verticalScrollbar is visible after one more widget is deleted. However it should say me "False" one step earlier.

Am I missing something?

UPDATE1. As requested, I add a working example without redundant code. You can download a zip archive from my dropbox >here<

>Virustotal.com analysis<

App screenshot


Solution

  • After trying to apply many different solutions, I've come to the easiest one. This one isn't the most proper, however it solves my problem.

    If I have two branches of code, which determine widget height and depend on the amount of inner widgets, therefore I can predict when I need scrollbar. That's why we can change main widget's width in these two branches of "if". Here is how the resize function was changed:

    def resizeApp(self):
        n = len(self.listOfWidgets) 
        if self.screenHEIGHT - 40 > n * 125 + 50:
            self.resize(135, n * 125 + 50)
            self.scrollArea.resize(self.scrollArea.width(), n * 125 + 20)
        else:
            self.resize(150, self.screenHEIGHT - 40)
            self.scrollArea.resize(self.scrollArea.width(), self.screenHEIGHT - 40 - 30)
    

    And that's it!