I am displaying multiple QLabels
on a QFrame
, placed into a QScrollArea
.
I am able to tell QScrollArea
to make any of the QLabels
visible with QScrollArea.ensureWidgetVisible(QLabel)
, but I cannot seem to find a method to find out whether the child widget is currently visible or not. I would expect something like QScrollArea.isWidgetVisible(QWidget)
.
I tried using the child's own method, i.e. QLabel.isVisible()
but no matter whether the QLabel
is visible or not in the QScrollArea
, it always returns True
(see example below). What's the solution to this?
#!/usr/bin/env python
import sys
from PyQt4 import QtGui, QtCore
application = QtGui.QApplication(sys.argv)
class Area(QtGui.QScrollArea):
def __init__(self, child):
super(Area, self).__init__()
self.child = child
self.setWidget(self.child)
self.setFixedSize(100, 100)
class MainWidget(QtGui.QFrame):
def __init__(self, parent=None):
QtGui.QFrame.__init__(self, parent)
self.layout = QtGui.QVBoxLayout()
n = 1
while n != 10:
label = QtGui.QLabel('<h1>'+str(n)+'</h1>')
self.layout.addWidget(label)
n += 1
self.setLayout(self.layout)
def wheelEvent(self, event):
print "Wheel Event:"
for child in self.children()[1:]:
print child.isVisible()
event.ignore()
mainwidget = MainWidget()
area = Area(mainwidget)
area.show()
application.exec_()
isVisible
is different from what you want to do. It tells whether the widget is hidden or not. Even though it is not in the viewport a widget is visible unless you hide
it.
You could use visibleRegion
. It is the region of the widget that paint events should occur. If the label is outside of the viewport, then it's region should be an empty region.
def wheelEvent(self, event):
print "Wheel Event:"
for child in self.children()[1:]:
print child.text(), 'is visible?', not child.visibleRegion().isEmpty()
event.ignore()