[SOLVED] How to find out whether child widget of QScroll Area is visible?
-
Hello,
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.ensureVisible(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. What's the solution?
-
I got an answer from "StackOverflow":http://stackoverflow.com/questions/10631067/how-to-find-out-whether-child-widget-of-qscrollarea-is-visible:
I proposed the following code:
@
#!/usr/bin/env pythonimport sys
from PyQt4 import QtGui, QtCoreapplication = 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_()
@And it was suggested I change the wheelEvent() method as such:
@
def wheelEvent(self, event):
print "Wheel Event:"
for child in self.children()[1:]:
print child.text(), 'is visible?', not child.visibleRegion().isEmpty()
event.ignore()
@And that does the job! :)