I'm trying to dynamically fill a QTableWidget (table_proj).
It works so far that the Table is filled and it looks right. The Code for the items in the Table:
for row in range(nb_row):
for col in range(nb_col):
item = QtWidgets.QTableWidgetItem(str(data[row][col]))
self.table_proj.setItem(row, 0, item)
self.table_proj.setCellWidget(row, 1, EditButtonWidget())
Where the nb_col is always two, and the nb_row is the len(data). Data is the dictionary that gets all the information.
The result looks like:
The Code for the EditButton's looks like:
class EditButtonWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(EditButtonWidget,self).__init__(parent)
###
self.dw = DockWidget()
###Layout###
self.layout = QtWidgets.QHBoxLayout(self)
self.layout.setContentsMargins(0,0,0,0)
self.layout.setSpacing(0)
self.b1 = QtWidgets.QPushButton()
self.b1.setText('Webseite')
self.b1.clicked.connect(self.doButtonB1)
self.b2 = QtWidgets.QPushButton()
self.b2.setText('Link kopieren')
self.b2.clicked.connect(self.doButtonB2)
self.layout.addWidget(self.b1)
self.layout.addWidget(self.b2)
self.setLayout(self.layout)
def doButtonB1(self):
row = self.dw.table_proj.currentRow()
print(row)
def doButtonB2(self):
pass
My problem is, that when I click on the Button "Webseite" it doesn't give me the correct row, but instead i just receive -1.
So if someone could give me a hint, where my mistake is, that would be great.
Just make the row a member of the EditButtonWidget
class as suggested on the Qt forum here .
For example:
class EditButtonWidget(QtWidgets.QWidget):
def __init__(self, row, parent=None):
super(EditButtonWidget,self).__init__(parent)
self.row = row
### etc ...
def doButtonB1(self):
print(self.row)
and
for row in range(nb_row):
for col in range(nb_col):
item = QtWidgets.QTableWidgetItem(str(data[row][col]))
self.table_proj.setItem(row, 0, item)
self.table_proj.setCellWidget(row, 1, EditButtonWidget(row))
This might not work if you allow users to insert/delete/move rows. In that case you probably need to use indexAt
as in the qt forum post or pass more information (in your case the website url) to the EditButtonWidget
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.