[PyQt4] QTableWidget Readonly?
-
Hello,
is it possible to make a QTableWidget readonly so the user cannot edit cells?
I wan't to show some configs, each line with 3 parameters so every line represents a config set.
To edit or delete a set the user should use the dialog I create, he should not edit the Table directly.Regards
Daniel
-
@Daniel26 said in [PyQt4] QTableWidget Readonly?:
Is there an overview over the flags?
Yes, in the link @SGaist posted
-
@Daniel26 said in [PyQt4] QTableWidget Readonly?:
so I have to set Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )?
I assume this is a typo, because @SGaist said
Qt.ItemIsEditable
-
@Daniel26 You need to click on Qt::ItemFlags: https://doc.qt.io/qt-5/qt.html#ItemFlag-enum
-
@Daniel26 said in [PyQt4] QTableWidget Readonly?:
so I have to set
Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )
?You can do this, if you know you want it to be selectable and enabled. A more robust/generic to just switch off editable, no matter what other flags are already there, is:
twi.setFlags(twi.flags() & ~QtCore.Qt.ItemIsEditable)
-
@Daniel26
As @SGaist gave you the link earlier, https://doc.qt.io/qt-5/qtablewidgetitem.html#setFlags is a method of eachQTableWidgetItem
. I imagine yourself.addip_tw
is the whole of theQTableWidget
, not aQTableWidgetItem
as my variabletwi
is intended to be. -
Ok, sorry for that stupid question, but I'm new to pyQt and object orientated programming.
QTableWidgetItem is only a cell of the QTableWidget? is that correct?I create an instance of QTableWidget as self.addip_tw and added the self.addip_tw to a QGridLayout.
So I have to do the setFlags for every cell in my Table? -
@Daniel26 said in [PyQt4] QTableWidget Readonly?:
QTableWidgetItem is only a cell of the QTableWidget? is that correct?
Not exactly, it's an object that will be stored in the model coming with QTableWidget and provide all the necessary data to be rendered by a cell of the table.
A QTableWidget is a QTableView + a model all packed in one.
You should check the Model/View Programming chapter in Qt's documentation for a good overview of how it is working.
-
@Daniel26
You can indeed do it as @Denni-0 has said. To my mind, it doesn't really make the cells read-only, but it does stop the user doing anything to edit :) And it's only one line!For making the items not-editable, you could look at the code in https://stackoverflow.com/questions/7727863/how-to-make-a-cell-in-a-qtablewidget-read-only/ which is the accepted solution. Other answers there also show @Denni-0 's approach.