Select 在 QAbstractTableModel 项的 QTableView 中

Select in QTableView from QAbstractTableModel item

我创建了 TableView(继承自 QTableView)和 Model(继承自 QAbstractTableModel)并实现了我需要的所有功能,但现在我必须添加奇怪的功能 - 那些对象(存储在模型中)必须能够导致 table 视图到 select "their" 行。

它来自这样一个事实,即总是有与之相关的图形对象,每当我单击某个对象上的场景时,我希望以它在 table 视图中的表示为中心。我可以这样做吗?

您的模型可以实现每次您想要更改选择时发出的信号。像这样:

void CMyModel::sigUpdateSelection(const QItemSelection & selection, QItemSelectionModel::SelectionFlags flags);

然后您可以将此信号连接到 table 视图的 QItemSelectionModel。这是您获得选择模型的方式:

QTableView* view = new QTableView(parent);
QItemSelectionModel* selectionModel = view->selectionModel();

QItemSelectionModel 有一个插槽 select()。这是您连接信号的地方。

这就是你发射的方式:

// Add to current selection
emit sigUpdateSelection(QItemSelection(indexLeft, indexRight), QItemSelectionModel::Select);

// Clear current selection and select new one
emit sigUpdateSelection(QItemSelection(indexLeft, indexRight), QItemSelectionModel::ClearAndSelect);