QTreeWidget 编辑项失败并显示“编辑:编辑失败"; [英] QTreeWidget editItem fails with "edit: editing failed"

查看:142
本文介绍了QTreeWidget 编辑项失败并显示“编辑:编辑失败";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QTreeWidgetItem 添加到一个 QTreeWidget:

I have a QTreeWidgetItem added to a QTreeWidget:

QTreeWidgetItem* item = new QTreeWidgetItem(ui->trwPairs);
item->setFlags(item->flags() | Qt::ItemIsEditable);

如果项目被编辑,我想对新值做一些检查:

If the item is edited, I want to do a few checks on the new value:

Pairs::Pairs(QWidget *parent) :
QWidget(parent),
  ui(new Ui::Pairs)
{
  ui->setupUi(this);
  connect(this->ui->trwPairs, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(Validate(QTreeWidgetItem*,int)));
}

void Pairs::Validate(QTreeWidgetItem* item, int column)
{
  if (item->text(column).toInt() < 1)
  {
    QMessageBox::critical(this, "Error", QString("Node ID ") + item->text(column) +  " is invalid.");
    ui->trwPairs->editItem(item, column);
  }
}

当然,如果它小于 1,它会捕获它,并给我消息框.但是,打印到 cerr 的是 edit:editing failed 并且该项目未处于编辑模式.我错过了什么?

Naturally, if it's less than 1, it catches it, and gives me the message box. However, printed to cerr is edit: editing failed and the item is not in edit mode. What am I missing?

推荐答案

在调试器中逐步执行会显示以下内容:

Stepping through it in the debugger reveals the following:

在 quaabstractitemview.cpp 行中,第 3953 行返回 false.不知何故,您的项目似乎仍处于编辑状态,您正在尝试再次编辑它或其他什么.

In quabstractitemview.cpp line false is returned on line 3953. Somehow it looks like your item is still in editing state and you are trying to edit it again or something.

bool QAbstractItemViewPrivate::shouldEdit(QAbstractItemView::EditTrigger trigger,
                                          const QModelIndex &index) const
{
// ..
    if (state == QAbstractItemView::EditingState)
      return false;
}

IIRC 我对每个单元格有多行的表格有类似的问题.查看类 QAbstractItemDelegate 视图具有项目委托,允许您控制使用哪个编辑器及其行为方式.我相信默认情况下使用 QLineEdit.像 QLineEdit 这样的编辑器可以有验证器来控制数据的验证方式,在您的情况下,如果数值小于0. 但我认为你必须使用模型/视图类并为此实现你自己的模型.QTreeWidget::setItemWidget(..) 的 Qt 文档说:

IIRC I had a similar problem with tables with multiple lines per cell. Check out the classes QAbstractItemDelegate views have item delegates which allow you to control which editor is used and how it behaves. I believe by default the QLineEdit is used. Editors like QLineEdit can have validators which control how the data is validated, in your case reject it if the numerical value is < 0. But I think you have to use the model / view classes and implement your own model for that. The Qt documentation for QTreeWidget::setItemWidget(..) says:

此功能只应用于在树小部件项目的位置显示静态内容.如果要显示自定义动态内容或实现自定义编辑器小部件,请改用 QTreeView 和子类 QItemDelegate.

This function should only be used to display static content in the place of a tree widget item. If you want to display custom dynamic content or implement a custom editor widget, use QTreeView and subclass QItemDelegate instead.

但是我不确定是否有使用小部件类的更简单的方法来做到这一点.

I am not sure however if there is a simpler way to do this using the widget classes.

这篇关于QTreeWidget 编辑项失败并显示“编辑:编辑失败";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆