hashy
hashy

Reputation: 305

how to add a qpushbutton using model.insertrow pyqt5

Basically I need to add a QPushButton to the end of every row of QTableView.

Here is how I insert a row:

for row in rows:
    timestamp = QStandardItem("%s" % (row[6]))

    tbModel.insertRow(0, (timestamp)))

I tried Things such as:

tbModel.insertRow(0, (timestamp, QStandardItemModel(QPushButton("Hi"))))

or at the end of for loop

tableWidget.setIndexWidget(tbModel.index(0, last_index), QPushButton("hi"))

But they both fail.

I Just want to add a button to the end of every row or add a button on the last column.

Upvotes: 0

Views: 158

Answers (1)

eyllanesc
eyllanesc

Reputation: 243983

You have to use the setIndexWidget method passing the proper QModelIndex:

column = tableview.model().columnCount() - 1
for row in range(tableview.model().rowCount()):
    index = tableview.model().index(row, column)
    button = QPushButton("Hi")
    tableview.setIndexWidget(index, button)

Upvotes: 1

Related Questions