为什么QTableView单元在点击它时不会聚焦?

时间:2016-02-22 01:22:03

标签: qt focus qtableview qstyleditemdelegate

我创建了QTableView并设置了一个委托,该委托使用滑块编辑第2列中的单元格。我在第2列的所有单元格上调用openPersistentEditor

MinMaxSliderDelegate *minMaxSliderDelegate = new MinMaxSliderDelegate(this);
table = new QTableView();
table->setModel(new ServoConfiguration(this));
table->setItemDelegate(minMaxSliderDelegate);
for(int r=0; r<table->model()->rowCount(); r++) {
    table->openPersistentEditor(table->model()->index(r,2));
}

所以现在我在第2列中有一张永久显示滑块的表格。

我用来在QStyledItemDelegate的子类中创建滑块的代码是:

QWidget *MinMaxSliderDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {

if (index.column()==2) {

    MinMaxSlider *editor = new MinMaxSlider(Qt::Horizontal, index.row(), parent);
    editor->setAutoFillBackground(true);
    editor->setFocusPolicy(Qt::StrongFocus);
    editor->setMinimum(750);
    editor->setMaximum(2000);
    editor->setMinValue(1000);
    editor->setMaxValue(1500);
    connect(uiObj, SIGNAL(setMinToCurrentValue(int)), editor, SLOT(setMinValueToCurrentValue(int)));
    connect(uiObj, SIGNAL(setMaxToCurrentValue()), editor, SLOT(setMaxValueToCurrentValue()));

    return editor;
}

return QStyledItemDelegate::createEditor(parent, option, index);

我的问题是我需要知道用户何时使用滑块编辑值(它应该具有焦点)。我发现,如果我从表格单元格向左滑动到滑块,则滑块将成为table->selectionModel()->currentIndex();返回的当前项目。但是,如果单击滑块,则不会更改当前项目。我还发现虽然我可以从单元格向左滑动到滑块,但我无法从滑块中跳出来。

我确信我错过了一些简单的事情,但我会感激一些帮助。

如果重要,我在其构造函数中为滑块设置StrongFocus,滑块列的QModelView标志为:Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;

1 个答案:

答案 0 :(得分:0)

对不起,我不能告诉确切的代码来回答你的问题。我只是想到它可能与QRect有关。

void QTableView::setSelection(const QRect & rect, QItemSelectionModel::SelectionFlags flags)

http://doc.qt.io/qt-4.8/qtableview.html#setSelection