qt 设置QTableView 中某一列无法选中
时间: 2024-03-27 16:40:09 浏览: 18
您可以通过设置QTableView的selectionBehavior属性来禁用对某一列的选中。具体地,将selectionBehavior属性设置为QAbstractItemView::SelectRows,然后重写QStyledItemDelegate的editorEvent()方法,当用户点击该列时,将其忽略即可。以下是一个示例代码:
```
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
class CustomDelegate : public QStyledItemDelegate
{
public:
explicit CustomDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
{
if (index.column() == YOUR_COLUMN_INDEX)
{
return true; // Ignore the event and do not edit the item
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
};
tableView->setItemDelegate(new CustomDelegate(this));
```
请将YOUR_COLUMN_INDEX替换为您想要禁用选中的列的索引。