PyQt5 - Reading values from a custom QTablewidget cell
My requirement is to get multiple widget values from a cell from a QTableWidget.
in one of my QTableWidget's coluumn having a QCheckbox, QLineEdit and another QCheckbox. For adding this I am using QHBoxLayout and QVBoxlayout. But while reading the values, how I can read the values from QTableWidget? I can get the widget while using olumnLvlTbl.cellWidget(0,1). But how I can get the values from nested Widgets?
colmumnLayoutRow=QHBoxLayout()
colmumnLayout1=QVBoxLayout()
dsfLblRowCount=QLabel(str(dataC))
rowCountChkBox = QCheckBox(parent=columnLvlTbl)
colmumnLayout1.addWidget(dsfLblRowCount)
colmumnLayout1.addWidget(rowCountChkBox)
colmumnLayout2=QVBoxLayout()
dsfLblRowCount2=QLabel("Pass %:")
rowCountLineEdit2 = QLineEdit(parent=columnLvlTbl)
rowCountLineEdit2.setMaximumWidth(50)
rowCountLineEdit2.setValidator(QtGui.QIntValidator (1, 100, self))
colmumnLayout2.addWidget(dsfLblRowCount2)
colmumnLayout2.addWidget(rowCountLineEdit2)
colmumnLayout3=QVBoxLayout()
dsfLblRowCount3=QLabel("Notification:")
rowCountChkBox3 = QCheckBox(parent=columnLvlTbl)
colmumnLayout3.addWidget(dsfLblRowCount3)
colmumnLayout3.addWidget(rowCountChkBox3)
cellWidgetRow1 = QWidget()
cellWidgetRow1.setLayout(colmumnLayout1)
cellWidgetRow2 = QWidget()
cellWidgetRow2.setLayout(colmumnLayout2)
cellWidgetRow3 = QWidget()
cellWidgetRow3.setLayout(colmumnLayout3)
colmumnLayoutRow.addWidget(cellWidgetRow1)
colmumnLayoutRow.addWidget(cellWidgetRow2)
colmumnLayoutRow.addWidget(cellWidgetRow3)
Whenever I am trying to do one action, I need to get the values from the QTableWidget.
Thanks in Advance
Re: PyQt5 - Reading values from a custom QTablewidget cell
In C++ you would create your own QWidget subclass that either provides access method to get at the sub-widget values, or exposes the sub-widgets as public members that can be directly queried from outside. When you access the cell widget you get a generic QWidget pointer that you need to qobject_cast (in C++):
Code:
MyCustomWidget *mywidget = qobject_cast<MyCustomWidget *>(tableWidget->cellWidget(row, col));
if (mywidget) {
// access the member functions/variables through mywidget
}
Exactly how this is achieved in Python I am not sure.... it might "Just Work" with the object returned by cellWidget() and some Pythonic type magic.
Re: PyQt5 - Reading values from a custom QTablewidget cell
Thanks a lot Chris. I tried to print myTable.cellWidget(0,1) and I am getting
<PyQt5.QtWidgets.QWidget object at 0x0000028F6E58ADC8>.
I convert the layout to widget then I added to the QTableWidget's cell. So I need to get the layout and then each widget's values from a cell. This columns are auto-generated depends the database entries.