How to scroll QListWidget to selected item

2024/9/8 10:27:06

The code below creates a single dialog window with QListWidget and QPushButton. Clicking the button fires up a scroll() function which finds and selects an "ITEM-0011".

I wonder if there is a way to scroll the list widget so the selected ITEM-0011 is at the top edge of QListWidget? Here is how the end result should look like:

enter image description here

from PyQt4 import QtCore, QtGui
app=QtGui.QApplication([])def scroll():item = listWidget.findItems('ITEM-0011', QtCore.Qt.MatchRegExp)[0]item.setSelected(True)window = QtGui.QDialog()
window.setLayout(QtGui.QVBoxLayout())
listWidget = QtGui.QListWidget()
window.layout().addWidget(listWidget)for i in range(100):QtGui.QListWidgetItem('ITEM-%04d'%i, listWidget)btn = QtGui.QPushButton('Scroll')
btn.clicked.connect(scroll)
window.layout().addWidget(btn)
window.show()
app.exec_()
Answer

The list-widget has a scrollToItem method that will scroll an item to a specific position:

def scroll():item = listWidget.findItems('ITEM-0011', QtCore.Qt.MatchRegExp)[0]item.setSelected(True)listWidget.scrollToItem(item, QtGui.QAbstractItemView.PositionAtTop)
https://en.xdnf.cn/q/72844.html

Related Q&A

Declaring Subclass without passing self

I have an abstract base class Bicycle:from abc import ABC, abstractmethodclass Bicycle(ABC):def __init__(self, cadence = 10, gear = 10, speed = 10):self._cadence = cadenceself._gear = gear self…

Flask-OIDC with keycloak - oidc_callback default callback not working

Im trying to use Flask-oidc in a simple flask application in order to add authentication via keycloak. However, once I log-in with valid credentials it goes back to /oidc_callback which doesnt exist. T…

Matplotlib: reorder subplots

Say that I have a figure fig which contains two subplots as in the example from the documentation:I can obtain the two axes (the left one being ax1 and the right one ax2) by just doing:ax1, ax2 = fig.a…

Filter values in a list using an array with boolean expressions

I have a list of tuples like this:listOfTuples = [(0, 1), (0, 2), (3, 1)]and an array that could look like this:myArray = np.array([-2, 9, 5])Furthermore, I have an array with Boolean expressions which…

Show two correlation coefficients on pairgrid plot with hue (categorical variable) - seaborn python

I found a function to compute a correlation coefficient and then add it to a pair plot (shown below). My issue is that when I run a pairplot with hue (a categorical variable) the correlation coefficien…

How to properly setup vscode with pyside? Missing suggestions

Im very new to pyside, qt and python. I managed to setup a project with a basic window and a push button which closes the app. My problem is, that somehow vscode wont show all properties available, eve…

Split marker and line in Legend - Matplotlib

I want to make a legend where I specify the value for the markers and the value for the lines but not the combination of both.This example should help to illustrate my goal:import matplotlib.pyplot as …

How do I loop over all items in a DynamoDB table using boto?

Id like to query a DynamoDB table and retrieve all the items and loop over them using boto. How do I structure a query or scan that returns everything in the table?

install pyopencv with pip on Mac OS X

I am trying to install pyopencv with pip in OS X Mountain Lion and it fails by import setuptools. Following is my work. what is "Library" in setuptools? I have not seen that before. I alread…

OpenCV remap interpolation error?

Im using opencv remap function to map an image to another coordinate system. However, my initial tests indicate that there are some issues with the interpolation. Here, I give a simple example of a co…