Adding Qpush button to each row of a QTableView -Python
-
I'm quite new in QT usage in Python and I have the following need:
I created a form in QT Designer containing a QTableView to manage data loaded from a SQLite table.
What I would need to achieve is to add a QPushButton beside each row of the QTableView to handle the modification request of a field on a specific row.
I already did a similar thing using the QTableWidget but, as I understood, the QTableView manage the update of data base data according to the 'edit strategy' option.
I'm wondering whether there's also a possibility to trigger the update in another way, such as on a QPushButton click which, in my case, should be placed beside each row of the QTableView. -
Hi,
How did you do it with QTableWidget ?
-
Here below the part of the code with QTableWidget (referenced as tableWidget_mat) where the QPushbutton is added (in column 3) beside each row of the QTableWidget.
'result' contains the table loaded from a SQL dbclass IndexedButtonWidget(QPushButton):
def init(self, text='', parent=None):
super(QPushButton, self).init(text, parent=parent)
self.button_row = 0
self.button_column = 0
self.button_text=""tablerow=0
for row_number, row_data in enumerate(result):
self.tableWidget_mat.insertRow(row_number+1)
for column_number, data in enumerate(row_data):
self.tableWidget_mat.setItem(row_number, column_number,QTableWidgetItem(str(data)))self.btn_sell = IndexedButtonWidget('Modifica') header_item = QtWidgets.QTableWidgetItem("") self.btn_sell.button_row = tablerow self.btn_sell.button_column = 3 self.btn_sell.button_text='Modifica' self.btn_sell.clicked.connect(self.handleButtonClicked) self.tableWidget_mat.setCellWidget(row_number,3,self.btn_sell) self.tableWidget_mat.setHorizontalHeaderItem(3, header_item)
-
Why one button per row since you are calling the same slot ?
A single save button would make more sense.