修复通过自定义控件编辑属性信息后tableview丢失焦点问题

This commit is contained in:
duanshengchao 2025-04-30 14:13:42 +08:00
parent 627646e31f
commit 23da60a235
1 changed files with 8 additions and 0 deletions

View File

@ -198,10 +198,14 @@ QWidget* AttributeTableDelegate::createEditor(QWidget *parent, const QStyleOptio
//判断放在setModelData函数中因为那里会先进行是否做内容改变的判断 //判断放在setModelData函数中因为那里会先进行是否做内容改变的判断
emit const_cast<AttributeTableDelegate*>(this)->commitData(textEditor); emit const_cast<AttributeTableDelegate*>(this)->commitData(textEditor);
emit const_cast<AttributeTableDelegate*>(this)->closeEditor(textEditor, QAbstractItemDelegate::NoHint); emit const_cast<AttributeTableDelegate*>(this)->closeEditor(textEditor, QAbstractItemDelegate::NoHint);
if(m_tableView)
m_tableView->setFocus();
}); });
connect(textEditor, &TextEditWidget::cancle, this, [=]{ connect(textEditor, &TextEditWidget::cancle, this, [=]{
//createEditor为const成员函数而closeEditor是QAbstractItemDelegate的非const信号在const成员函数中不能直接调用需要转换为非const指针 //createEditor为const成员函数而closeEditor是QAbstractItemDelegate的非const信号在const成员函数中不能直接调用需要转换为非const指针
emit const_cast<AttributeTableDelegate*>(this)->closeEditor(textEditor, QAbstractItemDelegate::NoHint); emit const_cast<AttributeTableDelegate*>(this)->closeEditor(textEditor, QAbstractItemDelegate::NoHint);
if(m_tableView)
m_tableView->setFocus();
}); });
//延迟获取焦点,防止可能因渲染未完成失效 //延迟获取焦点,防止可能因渲染未完成失效
QTimer::singleShot(10, this, [textEditor]() { QTimer::singleShot(10, this, [textEditor]() {
@ -215,9 +219,13 @@ QWidget* AttributeTableDelegate::createEditor(QWidget *parent, const QStyleOptio
connect(textEditor, &TextEditWidget::confirm, this, [=]{ connect(textEditor, &TextEditWidget::confirm, this, [=]{
emit const_cast<AttributeTableDelegate*>(this)->commitData(textEditor); emit const_cast<AttributeTableDelegate*>(this)->commitData(textEditor);
emit const_cast<AttributeTableDelegate*>(this)->closeEditor(textEditor, QAbstractItemDelegate::NoHint); emit const_cast<AttributeTableDelegate*>(this)->closeEditor(textEditor, QAbstractItemDelegate::NoHint);
if(m_tableView)
m_tableView->setFocus();
}); });
connect(textEditor, &TextEditWidget::cancle, this, [=]{ connect(textEditor, &TextEditWidget::cancle, this, [=]{
emit const_cast<AttributeTableDelegate*>(this)->closeEditor(textEditor, QAbstractItemDelegate::NoHint); emit const_cast<AttributeTableDelegate*>(this)->closeEditor(textEditor, QAbstractItemDelegate::NoHint);
if(m_tableView)
m_tableView->setFocus();
}); });
//延迟获取焦点,防止可能因渲染未完成失效 //延迟获取焦点,防止可能因渲染未完成失效
QTimer::singleShot(10, this, [textEditor]() { QTimer::singleShot(10, this, [textEditor]() {