Add a button in the rows of a QTableWidget
-
wrote on 16 Nov 2022, 14:37 last edited by
I created a table in a window with Qt Designer.
Unfortunately I can't insert a button in a cell (in the whole column).
I've read several solutions, but when I apply and try them the window crashes without returning errors.
What can I try?
Thank you -
I created a table in a window with Qt Designer.
Unfortunately I can't insert a button in a cell (in the whole column).
I've read several solutions, but when I apply and try them the window crashes without returning errors.
What can I try?
Thank youwrote on 16 Nov 2022, 14:48 last edited by@Andrea-R
Hello and welcome.We don't know what you might have tried! Did you call void QTableWidget::setCellWidget(int row, int column, QWidget *widget) passing it a
new
edQPushButton
? Although there are reasons why this approach is not great, it should work for you at least starting out. -
wrote on 16 Nov 2022, 15:13 last edited by
I tried this way for column '6' without success
for e in range(len(elencosel)): n = elencosel[e][6] cc = elencosel[e][2] nf = elencosel[e][0] sc = elencosel[e][9].strftime("%d/%m/%Y") im = elencosel[e][14] sim += im cr = elencosel[e][13] scr += cr rowPosition = window.tablew.rowCount() window.tablew.insertRow(rowPosition) window.tablew.setItem(rowPosition, 0, QtWidgets.QTableWidgetItem(n)) window.tablew.setItem(rowPosition, 1, QtWidgets.QTableWidgetItem(cc)) window.tablew.setItem(rowPosition, 2, QtWidgets.QTableWidgetItem(nf)) window.tablew.setItem(rowPosition, 3, QtWidgets.QTableWidgetItem(sc)) window.tablew.setItem(rowPosition, 4, QtWidgets.QTableWidgetItem(str(im))) window.tablew.setItem(rowPosition, 5, QtWidgets.QTableWidgetItem(str(cr))) window.pb_note = QPushButton(window.tablew) window.pb_note.setText('...') window.tablew.setCellWidget(rowPosition,6,window.pb_note) chkBoxItem = QTableWidgetItem() chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled) chkBoxItem.setCheckState(QtCore.Qt.Checked) window.tablew.setItem(rowPosition, 7, chkBoxItem)
-
I tried this way for column '6' without success
for e in range(len(elencosel)): n = elencosel[e][6] cc = elencosel[e][2] nf = elencosel[e][0] sc = elencosel[e][9].strftime("%d/%m/%Y") im = elencosel[e][14] sim += im cr = elencosel[e][13] scr += cr rowPosition = window.tablew.rowCount() window.tablew.insertRow(rowPosition) window.tablew.setItem(rowPosition, 0, QtWidgets.QTableWidgetItem(n)) window.tablew.setItem(rowPosition, 1, QtWidgets.QTableWidgetItem(cc)) window.tablew.setItem(rowPosition, 2, QtWidgets.QTableWidgetItem(nf)) window.tablew.setItem(rowPosition, 3, QtWidgets.QTableWidgetItem(sc)) window.tablew.setItem(rowPosition, 4, QtWidgets.QTableWidgetItem(str(im))) window.tablew.setItem(rowPosition, 5, QtWidgets.QTableWidgetItem(str(cr))) window.pb_note = QPushButton(window.tablew) window.pb_note.setText('...') window.tablew.setCellWidget(rowPosition,6,window.pb_note) chkBoxItem = QTableWidgetItem() chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled) chkBoxItem.setCheckState(QtCore.Qt.Checked) window.tablew.setItem(rowPosition, 7, chkBoxItem)
wrote on 16 Nov 2022, 15:18 last edited by JonB@Andrea-R
I have never usedsetCellWidget()
so I am not sure, but does it require you to have put aQTableWidgetItem
into the cell before you can add a widget, else the cell is not shown? Sowindow.tablew.setItem(rowPosition, 6, QtWidgets.QTableWidgetItem()) # add this line ... window.tablew.setCellWidget(rowPosition,6,window.pb_note)
?
Hmm, don't know if that is actually required. If it makes no difference, try reducing your code down to the bare minimum, because
setCellWidget()
should certainly work.... -
wrote on 16 Nov 2022, 15:29 last edited by
@JonB said in Add a button in the rows of a QTableWidget:
window.tablew.setItem(rowPosition, 6, QtWidgets.QTableWidgetItem())
No changes.
From DEBUG I read: 'PyQt5.uic.parser:new top widget None'I created the window with the designer and I import it with uic.
Maybe this is the problem? -
@JonB said in Add a button in the rows of a QTableWidget:
window.tablew.setItem(rowPosition, 6, QtWidgets.QTableWidgetItem())
No changes.
From DEBUG I read: 'PyQt5.uic.parser:new top widget None'I created the window with the designer and I import it with uic.
Maybe this is the problem?wrote on 16 Nov 2022, 17:48 last edited by@Andrea-R
Assuming you mean that the non-pushbutton cells in the table show their items OK, but you see nothing (? no button) in the button column, then your code should be fine no matter whether you are using Designer/uic
or not.I don't yet know what your problem is. You could try changing your
window.pb_note = QPushButton(window.tablew)
to remove thewindow.tablew
parameter. I don't think it will solve but worth checking.I will knock up a standalone PyQt5 for this tomorrow if I have a chance.
-
@Andrea-R
I have never usedsetCellWidget()
so I am not sure, but does it require you to have put aQTableWidgetItem
into the cell before you can add a widget, else the cell is not shown? Sowindow.tablew.setItem(rowPosition, 6, QtWidgets.QTableWidgetItem()) # add this line ... window.tablew.setCellWidget(rowPosition,6,window.pb_note)
?
Hmm, don't know if that is actually required. If it makes no difference, try reducing your code down to the bare minimum, because
setCellWidget()
should certainly work....wrote on 17 Nov 2022, 08:08 last edited by@JonB said in Add a button in the rows of a QTableWidget:
I have never used setCellWidget() so I am not sure, but does it require you to have put a QTableWidgetItem into the cell before you can add a widget, else the cell is not shown?
No, don't do that! You should have either a table widget item or a cell widget inside one cell. Not sure what happens otherwise.
Looking at your code I am not sure what the problem is. However, there is one question: How many columns does your table widget have? Is it (at least) 8? Otherwise adding the button and the checkbox will not work if there is not column for them.
-
@JonB said in Add a button in the rows of a QTableWidget:
I have never used setCellWidget() so I am not sure, but does it require you to have put a QTableWidgetItem into the cell before you can add a widget, else the cell is not shown?
No, don't do that! You should have either a table widget item or a cell widget inside one cell. Not sure what happens otherwise.
Looking at your code I am not sure what the problem is. However, there is one question: How many columns does your table widget have? Is it (at least) 8? Otherwise adding the button and the checkbox will not work if there is not column for them.
wrote on 17 Nov 2022, 09:45 last edited byyes, 8 columns.
it just crashes ('Process finished with exit code -1073741819 (0xC0000005))
I worked around the problem with a double click action on the row, but I would still like to insert the button, at least to understand where is the problem...😅
I will try in a clean minimum project if it works...
-
yes, 8 columns.
it just crashes ('Process finished with exit code -1073741819 (0xC0000005))
I worked around the problem with a double click action on the row, but I would still like to insert the button, at least to understand where is the problem...😅
I will try in a clean minimum project if it works...
wrote on 17 Nov 2022, 10:09 last edited by@Andrea-R
I have to go out now. I was planning to give a go at writing a minimal to show it working, for you to test at your site, in a couple of hours' time. -
yes, 8 columns.
it just crashes ('Process finished with exit code -1073741819 (0xC0000005))
I worked around the problem with a double click action on the row, but I would still like to insert the button, at least to understand where is the problem...😅
I will try in a clean minimum project if it works...
wrote on 17 Nov 2022, 17:09 last edited by JonB@Andrea-R
Sorry, was busy....The following works fine for me. PyQt 5.15.6, Qt 5.15.3, Ubuntu 22.04.
import sys from PyQt5 import QtWidgets if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) tw = QtWidgets.QTableWidget() tw.setColumnCount(3) for row in range(3): rowPosition = tw.rowCount() tw.insertRow(rowPosition) tw.setItem(rowPosition, 0, QtWidgets.QTableWidgetItem("col #0")) pb = QtWidgets.QPushButton() pb.setText("Pushbutton") tw.setCellWidget(rowPosition, 1, pb) tw.setItem(rowPosition, 2, QtWidgets.QTableWidgetItem("col #2")) tw.show() sys.exit(app.exec_())
You should copy & paste to confirm it works for you.
One possible thought about your code:
window.pb_note = QPushButton(window.tablew) window.pb_note.setText('...') window.tablew.setCellWidget(rowPosition,6,window.pb_note)
You will be doing this multiple times. You don't want to store in
window.pb_note
, it will get replaced each time by Python and there is a a small chance it might actually be disposing your previous buttons. In any case it's not useful. Replace with a local variablepb_note
:pb_note = QPushButton(window.tablew) pb_note.setText('...') window.tablew.setCellWidget(rowPosition,6,pb_note)
But I'd be surprised if that's the issue.
1/10