Question

Let's say I have a listwidget with lots of items. When the user scrolls to see more items, I want to perform an action to the items being viewed. Is it possible to trigger an event itemsViewedChanged(QListWidgetItems *items) that gives me the currently viewed by the user items?

If no, how can I implement something like this?

Thanks for any answers

Was it helpful?

Solution

I don't know of a pre-existing method, however I've implemented something similar in a QTreeWidget to handle loading of icons for only visible items, the general technique I would use for a QListWidget would be a bit simpler (forgive the syntax, I normally use PyQt, so this could be off in C++):

QList<QListWidgetItem*> MyListWidget::visibleItems() {
    QList<QListWidgetItem>* output = new QList<QListWidgetItem*>();

    // make sure we have some items
    if ( !this->count() ) {
        return output;
    }

    // calculate the beginning and end items in our range
    QListWidgetItem* minimumItem = this->itemAt(5, 5);
    QListWidgetItem* maximumItem = this->itemAt(5, this->height() - 5);

    if ( !minimumItem ) { minimumItem = this->item(0); }
    if ( !maximumItem ) { maximumItem = this->item(this->count() - 1); }

    // get the start and end rows
    int minimum_row = this->indexForItem(minimumItem)->row();
    int maximum_row = this->indexForItem(maximumItem)->row();

    for (int row = minimum_row; row <= maximum_row; row++) {
        output->append(this->item(row));
    }

    return output;
}

That would get you a list of the visible items for the list widget. To dynamically check and modify things as the user is editing, you can then connect to the valueChanged(int) and rangeChanged() signals for the list widget. That way, as the user scrolls or resizes your view, you are reacting to the signal and collecting the now visible list of items.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top