如何使 QTableWidget 中的单元格只读?
我有以下代码定义我的应用程序的 gui
class Ui (object):
def setupUi():
self.tableName = QtGui.QTableWidget(self.layoutWidget_20)
self.tableName.setObjectName(_fromUtf8("twHistoricoDisciplinas"))
self.tableName.setColumnCount(4)
self.tableName.setRowCount(3)
以及我的应用程序中的以下代码
class MainWindow(QtGui.QMainWindow):
def __init__(self):
self.ui = Ui()
self.ui.setupUi(self)
self.createtable()
#creating a tw cell
def cell(self,var=""):
item = QtGui.QTableWidgetItem()
item.setText(var)
return item
def createtable(self):
rows = self.tableName.rowCount()
columns = self.tableName.columnCount()
for i in range(rows):
for j in range(columns):
item = self.cell("text")
self.ui.tableName.setItem(i, j, item)
我希望能够添加新的行和列并编辑它们,但我想锁定一些单元格。 (我已经有扩展表格的代码) 如何使某些单元格只读,同时保持其他单元格读写? 我找到了这个链接 How to make a columns in QTableWidget read only? 以及 C++ 中问题的解决方案,python 解决方案是否类似?
编辑:从帖子中删除答案并粘贴为答案
i have the following code defining the gui of my app
class Ui (object):
def setupUi():
self.tableName = QtGui.QTableWidget(self.layoutWidget_20)
self.tableName.setObjectName(_fromUtf8("twHistoricoDisciplinas"))
self.tableName.setColumnCount(4)
self.tableName.setRowCount(3)
and the following code in my app
class MainWindow(QtGui.QMainWindow):
def __init__(self):
self.ui = Ui()
self.ui.setupUi(self)
self.createtable()
#creating a tw cell
def cell(self,var=""):
item = QtGui.QTableWidgetItem()
item.setText(var)
return item
def createtable(self):
rows = self.tableName.rowCount()
columns = self.tableName.columnCount()
for i in range(rows):
for j in range(columns):
item = self.cell("text")
self.ui.tableName.setItem(i, j, item)
I want to be able to add new rows and columns and edit them but i want to lock some of the cells. ( i already have code that expand the table )
how can i make some cells read only while keeping the others read write?
i found this link How to make a column in QTableWidget read only? with a solution for the problem in C++, is python solution similar ?
EDIT: Removed the answer from the post and pasted as an answer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我玩了一下代码并阅读了更多文档
问题的答案是
解决方案是“item.setFlags(QtCore.Qt.ItemIsEnabled)”行,您使用它来将单元格属性QtCore.Qt.ItemIsEnabled设置为禁用,因此您无法选择或编辑单元格
您可以按照 http://doc.qt.io/archives/qt-4.8/qt.html 在 Qt::ItemFlag 部分下,
如 Sven 对此问题的第二个答案的评论中所述,如果 QTableWidgetItem 中有静态数量的行和列,则可以使用 Qtdesigner 选择单元格的属性(如果您使用 Qtdesigner 为应用程序创建屏幕)
I played a little with the code and read some more documentation
the answer to the problem is
The solution is the line "item.setFlags(QtCore.Qt.ItemIsEnabled)", you use it to set the cell property QtCore.Qt.ItemIsEnabled to disabled, so you can't select or edit the cell
You can change a number of other properties this way at runtime as per documentarion on http://doc.qt.io/archives/qt-4.8/qt.html under the section Qt::ItemFlag
as mentioned in a comment by Sven on the second answer to this question, if you have a static number of rows and columns in your QTableWidgetItem you can select the properties of the cells with Qtdesigner if you use it to create the screens for your application
当没有编辑触发器时,永远不会进入 QTableWidgetItem 的编辑状态:
self.tableName.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
The editing status of a QTableWidgetItem is never entered when there are no Edit Triggers:
self.tableName.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
就像Sven Krüger的答案一样,您也可以将此方法用于PyQt5 :
Like Sven Krüger's answer, you can also use this methods for PyQt5:
如果您希望 UI 看起来相同(仍然可以选择,并变成蓝色,但不可编辑),我发现 QtCore.Qt.ItemIsEditable 给出了很好的结果。
If you want the UI to look the same (have it still selectable, and turn blue, but just not editable) I found
QtCore.Qt.ItemIsEditable
gave good results.对于 PyQt6,它与 @ozcanyarimdunya 相同,但具有枚举
EditTrigger
:For PyQt6, it's the same as @ozcanyarimdunya but with the enum
EditTrigger
:如果你想让整个表格不可编辑,请尝试以下操作:
if you want to make the whole table is not editable try this: