判断QTableView是否有打开的编辑器
有没有办法确定 QTableView 在当前单元格中是否有打开的编辑器?我需要处理以下情况:
- 用户双击单元格并编辑数据,但使单元格处于“编辑”状态。
- 在 UI 的另一部分,将执行更改底层模型的选定行的操作。
- 回到我的观点,我想确定新选择的行是否与打开的行相同。如果没有,我需要采取行动。 (提示用户?自动提交?恢复?)
我了解了如何获取当前项目,并且可以获取该项目的委托,但我没有看到任何我希望的 isEditMode()
属性去寻找。
有人能指出我正确的方向吗?
Is there any way to determine if a QTableView
has an open editor in the current cell? I need to handle the following situation:
- A user double-clicks a cell and edits the data, but leaves the cell in the "edit" state.
- On another part of the UI, an action is taken that changes the selected row of the underlying model.
- Back on my view, I want to determine if the newly selected row is the same as the open row. If not, I need to take an action. (Prompt the user? Commit automatically? Revert?)
I see how to get the current item, and can get the delegate on that item, but I don't see any isEditMode()
property I was hoping to find.
Can someone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只需检查返回值是否
为
Just check whether the return value of
is
您可以子类化
QTableView
,以便能够访问state()
函数,不幸的是,该函数受到保护。但是,我没有尝试这样做。如果您已有 QStyledItemDelegate 子类,则可以使用它来跟踪编辑器当前是否打开。但是,您不能只使用
setEditorData
/setModelData
,因为当用户取消编辑时,不会调用setModelData
。相反,您可以跟踪编辑器本身的创建和销毁。实现:
在某些情况下,例如当使用光标键移动到树中的下一项时,Qt 将首先创建新编辑器,然后销毁旧编辑器。因此,
m_editorCount
必须是整数而不是布尔值。不幸的是,createEditor() 是一个 const 函数。因此,您无法创建
int
成员。相反,创建一个指向int
的指针并使用它。You can subclass
QTableView
in order to be able to access thestate()
function, which is unfortunately protected. However, I did not try that.If you already have an
QStyledItemDelegate
subclass, you can use it to track whether an editor is currently open. However, you can't just usesetEditorData
/setModelData
, becausesetModelData
won't be called, when the user cancels editing. Instead, you can track the creation and destruction of the editor itself.Implementation:
On some occasions, e.g. when moving to the next item in the tree using the cursor keys, Qt will create the new editor first and then destroy the old one. Hence,
m_editorCount
must be an integer instead of a bool.Unfortunately,
createEditor()
is aconst
function. Therefore, you cannot create anint
-member. Instead, create a pointer to anint
and use that.连接到底层模型 dataChanged 信号
您可以检查数据已更改的单元格是否与 currentIndex 相同
您无法知道当前单元格是否直接打开编辑器,但可以检查视图是否在 QAbstractItemView::EditingState 中
它应该是足够做你想做的事。
Connect to underlying model dataChanged signal
You can check if the cell where data has changed is the same than the currentIndex
You cannot know if the current cell had an open editor straight, but can check if the view is in QAbstractItemView::EditingState
It should be enough to do what you want.
对您的委托进行子类化,使其包含一个访问器来告诉您何时进行编辑:
然后您只需检查委托即可了解发生了什么。或者和/或如果您不喜欢可变的,您可以发出信号,以便您知道委托处于什么状态。
Subclass your delegate so that it includes an accessor that tells you when it's editing:
Then you can just check the delegate to see what's going on. Alternatively and/or if you don't like the
mutable
, you can emit signals so you know what state the delegate is in.如果您知道正在编辑的项目的索引,则可以调用
indexWidget()
并尝试强制转换它。如果它有效,您不仅知道您正在编辑,而且还可以方便地使用编辑器小部件。If you know the index of the item being edited, you can call
indexWidget()
and attempt to cast it. If it's valid, you not only know you're editing, but you also have your editor widget handy.这是一个想法,在编辑开始之前获取编辑/组合小部件甚至很有帮助...
只需发出一个信号并在主窗口中使用它...这就是我在编辑之前在 QTableWidget 中获取组合框的方法。 ..
首先在 ComoBoxItemDelegate 中创建一个信号...
然后在 createEditor 方法中发出信号...
并在 MainWindow 中声明一个函数来接收信号...
然后最后在 MainWindow 的构造函数中连接它...
Here is an idea, its even helpful to get the edit/combo widget before the edit begins...
just emit a signal and consume it in the mainwindow... this is what I used one to get combo box in QTableWidget before editing...
first create a signal in ComoBoxItemDelegate...
then emit the signal in the createEditor method...
and in the MainWindow declare a function to receive the singal...
Then finally in the constructor of MainWindow connect it...