仅使 QTreeWidgetItem 的一列可编辑
我有一个包含两列数据的 QTreeWidgetItem,有什么方法可以使第二列可编辑吗?当我执行以下操作时:
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setFlags(item->flags() | Qt::ItemIsEditable);
所有列都变为可编辑。
I have a QTreeWidgetItem
with two columns of data, is there any way to make only the second column editable? When I do the following:
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setFlags(item->flags() | Qt::ItemIsEditable);
all columns become editable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以使用解决方法仅使 QTreeWidget 中的某些列可编辑:
1) 将 QTreeWidget 的 editTriggers 属性设置为 NoEditTriggers
2) 在插入项目时,设置 QTreeWidgetItem 对象的 Qt:ItemIsEditable 标志
3) 将以下插槽连接到“ QTreeWidget 对象的 itemDoubleClicked” 信号:
其中“isEditable”是您编写的函数,它对于可编辑列返回 true,对于不可编辑列返回 false。
You can make only certain columns in a QTreeWidget editable using a workaround:
1) Set the editTriggers property of the QTreeWidget to NoEditTriggers
2) On inserting items, set the Qt:ItemIsEditable flag of the QTreeWidgetItem object
3) Connect the following slot to the "itemDoubleClicked" signal of the QTreeWidget object:
where "isEditable" is a function you wrote that returns true for editable columns and false for non-editable columns.
我最近遇到了同样的问题,发现了一种适用于所有 EditTriggers 的解决方案,而不仅仅是 DoubleClicked 触发器(以及与双击信号的连接)
创建一个委托,为编辑器返回一个 NULL 指针:
然后将其用作为您的专栏自定义代表
I had the same problem recently and discovered a solution which works with all EditTriggers, not only the DoubleClicked one (and the connection to the double clicked signal)
Create a Delegate, that returns a NULL Pointer for the editor:
And later use it as a custom delegate for your column
看起来您将不得不放弃使用
QTreeWidget
和QTreeWidgetItem
并使用QTreeView
和QAbstractItemModel
。 “Widget”类是方便的类,它们是更抽象但更灵活的版本的具体实现。QAbstractItemModel
有一个调用flags(QModelIndex index)
,您可以在其中返回列的适当值。Looks like you will have to forgo using
QTreeWidget
andQTreeWidgetItem
and go withQTreeView
andQAbstractItemModel
. The "Widget" classes are convenience classes that are concrete implementations of the more abstract but more flexible versions.QAbstractItemModel
has a callflags(QModelIndex index)
where you would return the appropriate value for your column.似乎标准 QTreeWidget 不允许这样做。我认为有两种方法可以做到这一点:
将 QTreeView 与您自己的从 QAbstractItemModel 派生的类一起使用,并覆盖标志函数
将 QTreeView 与 QStandardItemModel 一起使用。然后,当您添加项目时,只需设置适当的列以允许编辑:
这是第二个选项的一些代码:
我发现第二种方法更简单,但这取决于您想要模型的灵活性。
Seem like the standard QTreeWidget doesn't allow this. I think there are two ways to do this:
Use a QTreeView with your own class derived from QAbstractItemModel and override the flags function
Use a QTreeView with a QStandardItemModel. Then when you add the item just set the appropriate column to allow edits:
Here's some code for the second option:
I find the second approach simpler but that depends on how much flexibility you want with your model.
我发现的最简单的方法是使用 Qt::ItemFlags
if
的顶部通过OR
添加编辑功能,底部检查它是否存在 < code>AND,然后使用XOR
将其删除。这样,编辑功能就会在您需要时添加,在您不需要时删除。
然后将此函数连接到树小部件的
itemDoubleClicked()
信号,并在isEditable()
内写入您的“编辑或不编辑”决策The simplest way that I found was to use Qt::ItemFlags
The top of the
if
adds the editing functionality through anOR
, and the bottom checks if it is there withAND
, then removes it with aXOR
.This way the editing functionality is added when you want it, and removed when you don't.
Then connect this function to the tree widget's
itemDoubleClicked()
signal, and write your 'to edit or not to edit' decision inside ofisEditable()
在
QTreeWidget
中:In the
QTreeWidget
:也许有点晚了,但可能会有所帮助:
这里 0 是您想要只读的列的索引。
将 ItemIsEditable 位置设置为 0,无论项目之前的标志是什么。
无论之前的标志如何,都将其设置为 1。
Maybe a little late, but may help :
Here 0 is the index of the column you want to make readonly.
Sets the ItemIsEditable position to 0 regardless the previous flag of your item.
Sets it to 1 regardless the previous flag.
我发现下面的代码可以很好地满足我的需求,并且“有点”停止
用户编辑列的某些部分:
我基本上检查角色,然后检查列。我只允许在第 0 列中进行编辑。因此,如果用户在任何其他列中对其进行编辑,那么我将停止 setData 编辑,并且不会进行任何更改。
I found out that the code below works well for my needs and does "kinda" stop the
user from editing certain parts of columns:
I basically check for role and then column. I only allow for editing in column 0. So if user edit it in any other column, then I stop the setData edit and no change is being made.
根据行和列设置树小部件的子项可编辑或不可编辑(树的项)。
Set the child of the tree-widget editable or not(itmes of tree), based on the row and column.
一般来说,我对 PySide 和 Python 很陌生,但我可以通过向 QTreeWidget 注册 itemClicked 回调来使其工作。在回调中,检查列,并且仅当您想要允许编辑的列时才调用“editItem”。
通过不调用第 0 列的 editItem,该事件基本上被丢弃。
I'm new to PySide and Python in general, but I was able to get this to work by registering with the QTreeWidget for itemClicked callbacks. Within the callback, check the column and only call 'editItem' if it's for a column you want to allow editing.
By not invoking editItem for column 0, the event is basically discarded.
对于那些寻找 Olaf Schumann 的答案 的 PyQt 实现的人,可以使用类似的方法。
首先,我们对自定义项委托进行子类化,返回
None
(无编辑器小部件):然后,我们将自定义委托应用于所需的列
For those looking for a PyQt implementation of Olaf Schumann's answer, a similar approach is possible.
First, we sub-class our custom item delegate, returning
None
(no editor widget):Then, we apply the custom delegate to the desired column