QListView - Get selected item

183 Views Asked by At

I have a QListView filled with a QSqlTableModel and data from my_database.db.

When clicking on a pushButton, how can I know which item of the list is selected and its index in the database ?

Below are a MWE, a minimum version of the SQL database and the Ui_MainWindow class generated by QtCreator.

I checked this question but I do not understand how to adapt the answer to my case with a SQL database.

MWE

############### LIBRARIES ###############

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel

from main_window_MWE import Ui_MainWindow

############### CLASS ###############

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, connection, *args, obj=None, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.setupUi(self)
                
        # Set up the model for the list view
        self.model = QSqlTableModel(db=connection)
        self.model.setTable("my_table")
        self.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
        self.model.select()
        
        # Set up the view for the list
        self.listView.setModel(self.model)
        self.listView.setModelColumn(1)
        
        # Connect the buttons
        self.pushButton.clicked.connect(self.button_clicked)

    # Define method for the button to display the selected list item
    def button_clicked(self):
        print("plop")

############### MAIN FUNCTION ###############

# Create an instance of QApplication
lab_book_app=QApplication([])

# Create connection
connection=QSqlDatabase.addDatabase("QSQLITE", "connection")
connection.setDatabaseName("my_database.db")

connection.open()

# Create instance of MainWindow
GUI=MainWindow(connection)
GUI.show()

# Close connection to be datbase
connection.close()

# Start the event loop
lab_book_app.exec()

my_database.db | id | name | | --- | --- | | 01 | first_item | | 02 | second_item|

main_window_MWE.py Created with QtCreator then converted to .py with pyuic5

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'main_window_MWE.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.listView = QtWidgets.QListView(self.centralwidget)
        self.listView.setObjectName("listView")
        self.verticalLayout.addWidget(self.listView)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Load"))
0

There are 0 best solutions below