0

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_()
neydroydrec
  • 6,973
  • 9
  • 57
  • 89

2 Answers2

3

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()
Avaris
  • 35,883
  • 7
  • 81
  • 72
0

QScrollArea::ensureWidgetVisible would do.

You can use QSCrollArea::childAt ( int x, int y ).isvisble() for checking widget visibility.

ScarCode
  • 3,074
  • 3
  • 19
  • 32
  • QScrollArea.ensureWidgetVisible will merely move the widget so that it is visible. It won't tell me whether the widget was visible or not in the first place. QScrollArea.childAt will merely return the widget at the given position and then QWidget.isVisible should return whether the widget is visible or not. That is not different from what I did in the code above, where I returned all children widgets, and which does not work, since it returns True for all widgets, even though they are not visible in the QScrollArea. – neydroydrec May 17 '12 at 07:48