在QTableView中使用选项卡一次移动两行

时间:2016-11-21 15:36:04

标签: c++ qt

我正在为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;
    }
};

1 个答案:

答案 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.