Visible region in Qscrollarea

  • How to Get number of Visible widgets in scrollarea as scrollarea gets created?

  • Moderators

    what exactly are you trying to achieve?

  • @raven-worx Thanks for responding.

    There's scrollbar in my scenario and I've put a few labels in it.
    Now i have a slot which gets called when I scroll, and it works fine.
    That slot gives the visibleRegion of the QScrollArea.
    In constructor I have put 20 Qlabels in the scroll area so when the code gets executed the scrollbar allready has 20 Labels.
    on that time i want to know how many labels are visible in the scrollArea.

  • Lifetime Qt Champion

    you could do

     // count visible
      int count = 0;
      QList<QLabel*> labels = ui->scrollArea->findChildren<QLabel*>();
      for ( QLabel* label : labels) {
        if ( ! label->visibleRegion().isEmpty() ) { count++; }
      qDebug() << count;

    Note that if last label is only partly visible, its still counted.

  • @mrjj yeah I have used it before.It works only i scroll the scrollbar.
    But at beggining it counts total buttons.
    for example At the begging : only 10 buttons are visible from 23.
    above logic works only if i scroll the scrollbar but It does not work for start up scenario.I want to display 10 count when widgets added in scrollarea.

  • Lifetime Qt Champion

    Im not sure how u use it.

    For me it counted the visible lables for me.
    With or without scrollbar. with/without scrolling.

    but they must be visible on screen before u count.

  • @mrjj yeah In My case It does count but It counts 23 instead of 10. It gives right answer when i Scroll.

  • Lifetime Qt Champion

    ok. cant guess. works 100% as expected for me.

  • @mrjj okay thanks...Have a Good Day :)

  • Lifetime Qt Champion

    same to you :)
    If you provide a small sample that shows it
    count wrong, i will look into it.

  • @mrjj

    Widget::Widget(QWidget *parent) :
        ui(new Ui::Widget)
          area=new QScrollArea(this);
        for(int i=0;i<23;i++){
            button=new QPushButton;

    setlabels() function:

        for(int i=0;i<P.count() ;i++)

  • Lifetime Qt Champion

    well, you call it from constructor so widget is not yet visible and
    visibleRegion make no sense then.

    You could use /override
    to first call it when shown.

    Alternatively, you must know size of scroll area and calculate how many is shown using
    buttons height. Since its not visible yet, it is not scrolled so should be pretty forward.

    Can i ask WHY you need to know how many is visible ?

  • @mrjj Actually I want to Display how may widgets in Scrolllarea are not visible at a time.

  • @mrjj Thanks , It worked
    I have a further question...

    QScrollArea *ScrollArea=new QScrollArea;
    QVBoxLayout *VLayout=new QVBoxLayout;
    QWidget *widget_in_ScrollAra=new QWidget;
    for(int i=0;i<20;i++){
         QLabel *Label=new QLabel;

    That's how I'm setting my ScrollArea, But is there a way to add those Label into ScrollArea by making Labels children of widget_in_ScrollAra without using any Layout?

  • Qt Champions 2018

    @hjohn Sure it is. Just don't create any layout and put the labels manually somewhere in the widget_in_ScrollAra.
    Use widget_in_ScrollAra as parent for the labels.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.