我正在为QTableView
使用自定义委托。我希望QLineEdit
旁边有一个组合框,而不是默认的QLineEdit
编辑器小部件。自定义编辑器正确显示,当我在文本框中键入并按Tab键移动到表格中的下一行时,它会跳过下一行,而是开始编辑下面两行的单元格。如果我使用输入键而不是使用制表符,则会按预期移动到下一行。有人可以解释Tab键行为在这里发生了什么吗?
BTW,我的表有两列,只有第二列是可编辑的,因此编辑器应该移动到下面的行。
我通过继承QStyledItemDelegate
这样做了我的自定义委托:
class ParameterDelegate: public QStyledItemDelegate {
public:
QWidget* createEditor(QWidget* parent, const QStyledItemDelegate& option, const QModelIndex& index) {
QWidget* widget = new QWidget(parent);
widget->setLayout(new QHBoxLayout());
widget->layout()->addWidget(new QLineEdit());
widget->layout()->addWidget(new QComboBox());
widget->layout()->setContentsMargins(0, 0, 0, 0);
widget->layout()->setSpacing(0);
return widget;
}
};
答案 0 :(得分:0)
解决方案结果非常简单;子类QWidget
并重新实现QWidget::focusNextPrevChild
以返回false
,如下所示:
class MyCustomEditorWidget: public QWidget {
public:
bool focusNextPrevChild(bool next) {return false;}
};
然后更改QStyledItemDelegate::createEditor
重新实现中的代码以创建/返回MyCustomEditorWidget
的新实例:
QWidget* widget = new MyCustomEditorWidget();
//etc.