I am trying to extract data from a QTableView that is populated by data in 5000 rows and 13 columns.
I am using the code shown below and expect (hope!) that this will get the data when I select a row, selectionMode is set to SingleSelection and selectionBehavior is set to SelectRows.It’s returning the error “AttributeError: ‘NoneType’ object has no attribute ‘selectedRows’” and I have absolutely no idea why and what it means. I have scoured the internet and can find nothing to help me resolve the problem.
I should add that I am a noob with PySide6 and hope someone can point me in the direction of a solution. Many thanks in advance for any help.
from PySide6 import QtCore, QtGui, QtWidgets
from PySide6.QtCore import Qt, QAbstractTableModel, QModelIndex, QRect, QItemSelectionModel
from PySide6.QtGui import QColor, QPainter
from PySide6.QtWidgets import QMainWindow, QApplication, QTableView, QGridLayout, QHeaderView, QWidget
import sys
import sqlite3
import pandas as pd
from ui.vrpsselect_window import Ui_mw_Main
class RPModel(QtCore.QAbstractTableModel):
def __init__(self, df_RP):
super(RPModel, self).__init__()
self._data = df_RP
def data(self, index, role):
if role == Qt.ItemDataRole.DisplayRole:
value = self._data.iloc[index.row(), index.column()]
return str(value)
def rowCount(self, index):
return self._data.shape[0]
def columnCount(self, index):
return self._data.shape[1]
def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return str(self._data.columns[section])
if orientation == Qt.Vertical and role == Qt.DisplayRole:
return f"{section + 1}"
class MainWindow(QMainWindow, Ui_mw_Main):
def __init__(self):
super().__init__()
self.setupUi(self)
# signals
self.tv_RP.clicked.connect(self.get_row_data)
# run functions on startup
self.setup()
self.load_VRPsS10()
self.initialise_table()
def setup(self):
# connect to the database and create a cursor
conn = sqlite3.connect('data/VRPsS10.db')
c = conn.cursor()
# create a table VRPs
c.execute("""CREATE TABLE if not exists VRPs(
Country text,
Lat D text,
Lon D text,
Name text,
Ident text,
Airport text,
Source text,
Cycle text,
Elevation text,
Lat DD text,
Lon DD text,
Lat DMS text,
Lon DMS text)""")
# create a table Updates
c.execute("""CREATE TABLE if not exists Updates(
Effective text,
Cycle text)""")
# commit the changes and close connection
conn.commit()
conn.close()
def load_VRPsS10(self):
# connect to the database and create a cursor
conn = sqlite3.connect('data/VRPsS10.db')
c = conn.cursor()
# read from the 'Updates' table
self.df_lu = pd.read_sql_query('SELECT * from Updates', conn)
self.setWindowTitle(
f'VRPsSelect Latest Update - AIRAC Cycle: {self.df_lu.iat[0, 1]} - Date: {self.df_lu.iat[0, 0]}'
f' See the Reporting Points table for the latest updates for each country')
# read from the 'VRPs' table
self.df_RP_all = pd.read_sql_query('SELECT * from VRPs', conn)
self.df_RP_temp = self.df_RP_all.copy()
# close the connection
conn.close()
# display the final output
self.display_update()
def initialise_table(self):
# set up the Reporting Points table
self.model_rp = RPModel(self.df_RP)
self.tv_RP.setModel(self.model_rp)
self.tv_RP.setColumnWidth(0, 60)
self.tv_RP.setColumnWidth(1, 60)
self.tv_RP.setColumnWidth(2, 60)
self.tv_RP.setColumnWidth(3, 250)
self.tv_RP.setColumnWidth(4, 60)
self.tv_RP.setColumnWidth(5, 105)
self.tv_RP.setColumnWidth(6, 60)
self.tv_RP.setColumnWidth(7, 60)
self.tv_RP.setColumnWidth(8, 70)
self.tv_RP.setColumnWidth(9, 80)
self.tv_RP.setColumnWidth(10, 80)
self.tv_RP.setColumnWidth(11, 110)
self.tv_RP.setColumnWidth(12, 110)
self.tv_RP.horizontalHeader().setDefaultAlignment(Qt.AlignmentFlag.AlignLeft)
self.tv_RP = QTableView()
def display_update(self):
# copies the temp dataframe to the main dataframe
self.df_RP = self.df_RP_temp.copy()
self.lb_AvailableRP.setText(f'Available: {self.df_RP.shape[0]}')
def get_row_data(self, index):
indexes = self.tv_RP.selectionModel().selectedRows()
model = self.tv_RP.model()
for index in indexes:
print(model.data(model.index(index.row(), 1)))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())