创建模型和单元格选择模型:
- QStandardItemModel* model = new QStandardItemModel(2,6,this);//模型行数、模型列数、父对象
- QItemSelectionModel* selection = new QItemSelectionModel(model);//创建单元格选择模型
为tableView设置数据模型:
- ui.tableView->setModel(model);
- ui.tableView->setSelectionModel(selection);
- ui.tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
- ui.tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
设置行数:
model->setRowCount(rowCount - 1);
设置表头:
- QString header = fileContents.at(0);
- QStringList headerList = header.split(QRegExp("\\s+"),QString::SkipEmptyParts);
- model->setHorizontalHeaderLabels(headerList);
设置表格数据:
- QStandardItem* item;
- for (int i = 1; i < rowCount; i++)
- {
- QString text = fileContents.at(i);
- QStringList lines = text.split(QRegExp("\\s"),QString::SkipEmptyParts);
- for (int j = 0; j < 5; j++)
- {
- item = new QStandardItem(lines.at(j));
- model->setItem(i - 1,j,item);
- }
- }
设置项是否可选:
- item = new QStandardItem(headerList.at(j));
- item->setCheckable(true);
- if (lines.at(j) == "0")
- item->setCheckState(Qt::Unchecked);
- else
- item->setCheckState(Qt::Checked);
- model->setItem(i - 1,j,item);
添加行:
- void port::on_add_triggered()
- {
- QList
itemList; - QStandardItem* item;
- //前5列
- for (int i = 0; i < 5; i++)
- {
- item = new QStandardItem("0");
- itemList << item;
- }
- //最后一列
- QString headerStr = model->headerData(model->columnCount() - 1,Qt::Horizontal,Qt::DisplayRole).toString();
- item = new QStandardItem(headerStr);
- item->setCheckable(true);
- itemList << item;
- model->insertRow(model->rowCount(),itemList);
- QModelIndex currentIndex = model->index(model->rowCount() - 1,0);
- selection->clearSelection();
- selection->setCurrentIndex(currentIndex,QItemSelectionModel::Select);
- }
插入行:
- void port::on_insert_triggered()
- {
- QList
itemList; - QStandardItem* item;
- //前5列
- for (int i = 0; i < 5; i++)
- {
- item = new QStandardItem("0");
- itemList << item;
- }
- //最后一列
- QString headerStr = model->headerData(model->columnCount() - 1, Qt::Horizontal, Qt::DisplayRole).toString();
- item = new QStandardItem(headerStr);
- item->setCheckable(true);
- itemList << item;
- QModelIndex currentIndex = selection->currentIndex();
- model->insertRow(currentIndex.row(), itemList);
- selection->clearSelection();
- selection->setCurrentIndex(currentIndex, QItemSelectionModel::Select);
- }
删除行:
- void port::on_del_triggered()
- {
- QModelIndex curindex = selection->currentIndex();
- if (curindex.row() == model->rowCount() - 1)
- model->removeRow(curindex.row());
- else
- {
- model->removeRow(curindex.row());
- selection->setCurrentIndex(curindex,QItemSelectionModel::Select);
- }
- }
设置表格内容格式居左:
- void port::on_left_triggered()
- {
- if (!selection->hasSelection()) //没有选择的项
- return;
-
- //获取选择的单元格的模型索引列表,可以是多选
- QModelIndexList selectedIndex = selection->selectedIndexes();
-
- for (int i = 0; i < selectedIndex.count(); i++)
- {
- QModelIndex aIndex = selectedIndex.at(i); //获取其中的一个模型索引
- QStandardItem* aItem = model->itemFromIndex(aIndex);//获取一个单元格的项数据对象
- aItem->setTextAlignment(Qt::AlignLeft);//设置文字对齐方式
- }
- }
设置居中:
- void port::on_center_triggered()
- {
- if (!selection->hasSelection())
- return;
-
- QModelIndexList selectedIndex = selection->selectedIndexes();
-
- QModelIndex aIndex;
- QStandardItem *aItem;
-
- for (int i = 0; i < selectedIndex.count(); i++)
- {
- aIndex = selectedIndex.at(i);
- aItem = model->itemFromIndex(aIndex);
- aItem->setTextAlignment(Qt::AlignHCenter);
- }
- }
获取表格选中的行数:
- // 在构造函数或初始化函数中连接信号与槽
- connect(tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MyClass::handleSelectionChanged);
-
- // 槽函数,处理选中行的行号
- void MyClass::handleSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
- QModelIndexList selectedIndexes = selected.indexes();
- if (!selectedIndexes.isEmpty()) {
- int row = selectedIndexes.first().row();
- qDebug() << "Selected row: " << row;
- }
- }
获取QTabelView表格中指定行的内容:
- // 获取指定行的内容
- int row = 3; // 指定行号
- int column = 1; // 指定列号
- QModelIndex index = model->index(row, column); // 获取指定行的索引
- QVariant data = model->data(index); // 获取指定索引处的数据
- QString content = data.toString(); // 转换为字符串类型