QTableView 单行选择模式,行选择事件

1.要点

1.1 单行模型

tableView.setSelectionMode(QAbstractItemView::SingleSelection); // 设置单行选择模式  
    tableView.setSelectionBehavior(QAbstractItemView::SelectRows); // 设置选择行为为选择整行 

1.2 选择行

QModelIndexList indexes = selected.indexes();  
            if (!indexes.isEmpty()) {  
                QModelIndex firstIndex = indexes.first();  
                qDebug() << "Row" << firstIndex.row() << "selected.";  
            }  

1.3 行选择事件

// 处理行选择事件  
    QObject::connect(tableView.selectionModel(), &QItemSelectionModel::selectionChanged,  
        [&](const QItemSelection &selected, const QItemSelection &deselected){  
            QModelIndexList indexes = selected.indexes();  
            if (!indexes.isEmpty()) {  
                QModelIndex firstIndex = indexes.first();  
                qDebug() << "Row" << firstIndex.row() << "selected.";  
            }  
        }); 

2.正文

在 Qt 中,要实现 QTableView 的单行选择模式以及处理行选择事件,你需要做两件事:

  1. 设置单行选择模式:你可以通过设置 QTableView 的选择模式来确保用户只能选择整行。这可以通过调用 setSelectionMode() 和 setSelectionBehavior() 方法来实现。

  2. 处理行选择事件:如之前所述,你可以通过连接 QItemSelectionModel 的 selectionChanged 信号来响应选择事件。

下面是一个示例代码,展示了如何设置单行选择模式并处理行选择事件:

 
#include <QApplication>  
#include <QTableView>  
#include <QStandardItemModel>  
#include <QHeaderView>  
  
int main(int argc, char *argv[]) {  
    QApplication app(argc, argv);  
  
    // 创建一个标准的表格模型并填充数据  
    QStandardItemModel model(5, 3); // 5行3列  
    for (int row = 0; row < 5; ++row) {  
        for (int column = 0; column < 3; ++column) {  
            QModelIndex index = model.index(row, column);  
            model.setData(index, QString("Item %1-%2").arg(row + 1).arg(column + 1));  
        }  
    }  
  
    // 创建表格视图并设置模型  
    QTableView tableView;  
    tableView.setModel(&model);  
    tableView.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);  
    tableView.verticalHeader()->setVisible(false);  
    tableView.setSelectionMode(QAbstractItemView::SingleSelection); // 设置单行选择模式  
    tableView.setSelectionBehavior(QAbstractItemView::SelectRows); // 设置选择行为为选择整行  
  
    // 处理行选择事件  
    QObject::connect(tableView.selectionModel(), &QItemSelectionModel::selectionChanged,  
        [&](const QItemSelection &selected, const QItemSelection &deselected){  
            QModelIndexList indexes = selected.indexes();  
            if (!indexes.isEmpty()) {  
                QModelIndex firstIndex = indexes.first();  
                qDebug() << "Row" << firstIndex.row() << "selected.";  
            }  
        });  
  
    tableView.show();  
    return app.exec();  
}

在这个示例中,setSelectionMode(QAbstractItemView::SingleSelection) 确保了用户一次只能选择一行,而 setSelectionBehavior(QAbstractItemView::SelectRows) 使得当用户点击某个单元格时,整行都会被选中。

通过连接 selectionChanged 信号,我们可以在用户选择行时执行自定义的操作,比如在这个例子中,我们简单地打印了被选中行的行号。

请注意,这个示例假设你已经设置了 Qt 环境和必要的头文件。如果你在实际应用中遇到问题,确保你的项目配置正确,并且已经包含了所有必要的 Qt 模块。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值