qt中,要獲取獲取QTableview中選中行的第一列內容,只需要用到一個mode和選中行觸發信號。
一、步驟如下
1.1先綁定選中行信號事件
//綁定表格選中行摁下觸發事件
connect(addedDeviceTable,SIGNAL(pressed(QModelIndex)),this,SLOT(slot_pressedItem_tableView(QModelIndex)));
1.2槽函數中,通過函數QStandItemMode::data(QModeIndex::(rew,column)).toString()獲取選中行第column列內容
//槽函數
void QLocalDriveWidget::slot_pressedItem_tableView(QModelIndex index)
{
QString deviceName=addedDeviceItemMode->data(addedDeviceItemMode->index(index.row(),0)).toString();
}
二、具體核心代碼如下
#include <QStandardItemModel>
#include <QHeaderView>
#include <QTableView>
#include <QDebug>
//QStandardItemModel模型
QStandardItemModel *addedDeviceItemMode=new QStandardItemModel;
//綁定表格選中行摁下觸發事件
connect(addedDeviceTable,SIGNAL(pressed(QModelIndex)),this,SLOT(slot_pressedItem_tableView(QModelIndex)));
//槽函數
void QLocalDriveWidget::slot_pressedItem_tableView(QModelIndex index)
{
QString deviceName=addedDeviceItemMode->data(addedDeviceItemMode->index(index.row(),0)).toString();
qDebug()<<"deviceName=="<<deviceName;
}
參考內容:
https://blog.csdn.net/mao834099514/article/details/50955368(參考:獲取QTableView選擇行所有列內容)
https://blog.csdn.net/naibozhuan3744/article/details/92573309(參考:獲取QTreeView選中行內容)