Visible region in Qscrollarea



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


  • Moderators

    @hjohn
    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.


  • Qt Champions 2017

    @hjohn
    Hi
    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.
    0_1533791259708_Untitled.png


  • Qt Champions 2017

    @hjohn
    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.


  • Qt Champions 2017

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



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


  • Qt Champions 2017

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



  • @mrjj

    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
    
          area=new QScrollArea(this);
          area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
        for(int i=0;i<23;i++){
            button=new QPushButton;
           button->setText(QString::number(i));
            layOut->addWidget(button);
        }
    setlabels();
    

    setlabels() function:

     P=area->findChildren<QPushButton*>();
        for(int i=0;i<P.count() ;i++)
    {
            if(!P.at(i)->visibleRegion().isEmpty())
          {
                    visibleButtonVectorBottom.push_back(P.at(i));
         }
     }
       
    

  • Qt Champions 2017

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

    You could use /override
    http://doc.qt.io/qt-5/qwidget.html#showEvent
    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;
    
    widget_in_ScrollAra->setLayout(VLayout);
    
    for(int i=0;i<20;i++){
         QLabel *Label=new QLabel;
         Label->setText(QString::number(i));
         VLayout->setWidget(Label);
    }
    ScrollArea->setWidget(widget_in_ScrollAra);
    
    

    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?


  • Moderators

    @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.