[PyQt4] QTableWidget Readonly?
-
wrote on 12 Feb 2020, 08:36 last edited by aha_1980 2 Dec 2020, 09:01
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
-
Hi and welcome to devnet,
Sure thing, use the setFlags method of QTableWidgetItem and
Qt.ItemIsEditable
flag. -
wrote on 12 Feb 2020, 09:23 last edited by Daniel26 2 Dec 2020, 09:25
@SGaist said in [PyQt4] QTableWIdget Readonly?:
Qt.ItemIsEditable
Hello,
so I have to set Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )?
Thats all?
Is there an overview over the flags?
RegardsDaniel
-
@SGaist said in [PyQt4] QTableWIdget Readonly?:
Qt.ItemIsEditable
Hello,
so I have to set Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )?
Thats all?
Is there an overview over the flags?
RegardsDaniel
@Daniel26 said in [PyQt4] QTableWidget Readonly?:
Is there an overview over the flags?
Yes, in the link @SGaist posted
-
@SGaist said in [PyQt4] QTableWIdget Readonly?:
Qt.ItemIsEditable
Hello,
so I have to set Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )?
Thats all?
Is there an overview over the flags?
RegardsDaniel
@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 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
-
@J-Hilk I don't want it editable, so I didn't set Qt.ItemIsEditable.
Or is that wrong?@jsulm In the link I Can't find an overview of the QT-Flags.
Regards
Daniel
@Daniel26 You need to click on Qt::ItemFlags: https://doc.qt.io/qt-5/qt.html#ItemFlag-enum
-
@SGaist said in [PyQt4] QTableWIdget Readonly?:
Qt.ItemIsEditable
Hello,
so I have to set Widget.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )?
Thats all?
Is there an overview over the flags?
RegardsDaniel
wrote on 12 Feb 2020, 10:44 last edited by@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 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)
wrote on 12 Feb 2020, 12:53 last edited by Daniel26 2 Dec 2020, 13:07@JonB
self.addip_tw.setFlags(self.addip_tw.flags() & ~QtCore.Qt.ItemIsEditable)
AttributeError: 'QTableWidget' object has no attribute 'setFlags'What did I wrong? Is setFlags Qt5 only?
Regards
Daniel
-
@JonB
self.addip_tw.setFlags(self.addip_tw.flags() & ~QtCore.Qt.ItemIsEditable)
AttributeError: 'QTableWidget' object has no attribute 'setFlags'What did I wrong? Is setFlags Qt5 only?
Regards
Daniel
wrote on 12 Feb 2020, 14:25 last edited by JonB 2 Dec 2020, 14:26@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. -
wrote on 12 Feb 2020, 14:40 last edited by
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? -
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.
-
wrote on 12 Feb 2020, 15:34 last edited by
Ok,
so I set every Cell with an empty QWidgetItem so I can set it to readOnly?
I'm totally confused....
-
Ok,
so I set every Cell with an empty QWidgetItem so I can set it to readOnly?
I'm totally confused....
wrote on 12 Feb 2020, 17:43 last edited by@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.
-
wrote on 18 Feb 2020, 10:23 last edited by
hello @ all,
thanks for the help. I've got it, did it with the QTableWidget.
But how can I check, if a column is empty?
A simple "if self.addip_tw.item(x,y)" won't work :)Regards
Daniel
-
hello @ all,
thanks for the help. I've got it, did it with the QTableWidget.
But how can I check, if a column is empty?
A simple "if self.addip_tw.item(x,y)" won't work :)Regards
Daniel
-
wrote on 18 Feb 2020, 10:51 last edited by
Sorry, I ment if a cell is empty.
I store the content of the cells into a dictionary, but only if the first cell of the line is not empty.
-
Sorry, I ment if a cell is empty.
I store the content of the cells into a dictionary, but only if the first cell of the line is not empty.
wrote on 18 Feb 2020, 11:22 last edited by@Daniel26
The I don't understand why you sayA simple "if self.addip_tw.item(x,y)" won't work :)
since https://doc.qt.io/qt-5/qtablewidget.html#item does return
nullptr
for no item. -
@Daniel26
The I don't understand why you sayA simple "if self.addip_tw.item(x,y)" won't work :)
since https://doc.qt.io/qt-5/qtablewidget.html#item does return
nullptr
for no item.
1/22