Displaying Panda Images with PyQt6 Lists

A simple viewer can be built by combining a QListView with a QLabel. The list holds pandda names; clicking an entry updates the displayed photo.

First, create the data model and the list view. Use QStringListModel to hold the names and attach it to the list:

names = ['Fu Bao', 'Meng Lan', 'Jin Hu']
model = QStringListModel(names)
list_view = QListView()
list_view.setModel(model)

Next, set up a label that will show the images. Constraining its size ensures all pictures appear with the same dimensions. setScaledContents(True) makes the image stretch to fill the label:

self.image_label = QLabel()
self.image_label.setMaximumSize(480, 340)
self.image_label.setScaledContents(True)
self.image_label.setPixmap(QPixmap('images/xm.jpg'))

Connect the list’s clicked signal to a slot that switches the displayed pixmap. The slot retrieves the selectde name from the model and maps it to an image file:

list_view.clicked.connect(self.on_selection)

def on_selection(self, index):
    name = index.data(Qt.DisplayRole)
    image_path = self.image_map.get(name, 'images/default.jpg')
    self.image_label.setPixmap(QPixmap(image_path))

A dictionary simplifies the name-to-path mapping:

self.image_map = {
    'Fu Bao': 'images/fb.jpg',
    'Meng Lan': 'images/xm.jpg',
    'Jin Hu': 'images/jh.jpg'
}

Finally, arrange the label and the list in a vertical layout inside a QWidget:

layout = QVBoxLayout(self)
layout.addWidget(self.image_label)
layout.addWidget(list_view)
self.setLayout(layout)

The comlpete window class may look like this:

import sys
from PyQt6.QtCore import Qt, QStringListModel
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QListView, QVBoxLayout

class PandaGallery(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Panda Viewer')
        self.resize(500, 350)

        names = ['Fu Bao', 'Meng Lan', 'Jin Hu']
        self.image_map = {
            'Fu Bao': 'images/fb.jpg',
            'Meng Lan': 'images/xm.jpg',
            'Jin Hu': 'images/jh.jpg'
        }

        model = QStringListModel(names)
        list_view = QListView()
        list_view.setModel(model)
        list_view.clicked.connect(self.on_selection)

        self.image_label = QLabel()
        self.image_label.setMaximumSize(480, 340)
        self.image_label.setScaledContents(True)
        self.image_label.setPixmap(QPixmap(self.image_map['Meng Lan']))

        layout = QVBoxLayout(self)
        layout.addWidget(self.image_label)
        layout.addWidget(list_view)

    def on_selection(self, index):
        name = index.data(Qt.DisplayRole)
        path = self.image_map.get(name, 'images/default.jpg')
        self.image_label.setPixmap(QPixmap(path))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = PandaGallery()
    window.show()
    sys.exit(app.exec())

Tags: pyqt6 QListView python gui

Posted on Sun, 02 Aug 2026 16:11:20 +0000 by missyevil