[PyQt4] QTableWidget Readonly?
-
@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
@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)
-
@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
@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? -
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.
-
Ok,
so I set every Cell with an empty QWidgetItem so I can set it to readOnly?
I'm totally confused....
@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.
-
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
-
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.
-
@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. -
@JonB
if self.addip_tw.item[row,0]:
TypeError: 'builtin_function_or_method' object has no attribute 'getitem'