• Qt视图/模型


    创建模型和单元格选择模型:

    1. QStandardItemModel* model = new QStandardItemModel(2,6,this);//模型行数、模型列数、父对象
    2. QItemSelectionModel* selection = new QItemSelectionModel(model);//创建单元格选择模型
    为tableView设置数据模型:
    1. ui.tableView->setModel(model);
    2. ui.tableView->setSelectionModel(selection);
    3. ui.tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    4. ui.tableView->setSelectionBehavior(QAbstractItemView::SelectItems);

    设置行数:

    model->setRowCount(rowCount - 1);

    设置表头:

    1. QString header = fileContents.at(0);
    2. QStringList headerList = header.split(QRegExp("\\s+"),QString::SkipEmptyParts);
    3. model->setHorizontalHeaderLabels(headerList);

    设置表格数据

    1. QStandardItem* item;
    2. for (int i = 1; i < rowCount; i++)
    3. {
    4. QString text = fileContents.at(i);
    5. QStringList lines = text.split(QRegExp("\\s"),QString::SkipEmptyParts);
    6. for (int j = 0; j < 5; j++)
    7. {
    8. item = new QStandardItem(lines.at(j));
    9. model->setItem(i - 1,j,item);
    10. }
    11. }

    设置项是否可选:

    1. item = new QStandardItem(headerList.at(j));
    2. item->setCheckable(true);
    3. if (lines.at(j) == "0")
    4. item->setCheckState(Qt::Unchecked);
    5. else
    6. item->setCheckState(Qt::Checked);
    7. model->setItem(i - 1,j,item);

    添加行:

    1. void port::on_add_triggered()
    2. {
    3. QList itemList;
    4. QStandardItem* item;
    5. //前5列
    6. for (int i = 0; i < 5; i++)
    7. {
    8. item = new QStandardItem("0");
    9. itemList << item;
    10. }
    11. //最后一列
    12. QString headerStr = model->headerData(model->columnCount() - 1,Qt::Horizontal,Qt::DisplayRole).toString();
    13. item = new QStandardItem(headerStr);
    14. item->setCheckable(true);
    15. itemList << item;
    16. model->insertRow(model->rowCount(),itemList);
    17. QModelIndex currentIndex = model->index(model->rowCount() - 1,0);
    18. selection->clearSelection();
    19. selection->setCurrentIndex(currentIndex,QItemSelectionModel::Select);
    20. }

    插入行:

    1. void port::on_insert_triggered()
    2. {
    3. QList itemList;
    4. QStandardItem* item;
    5. //前5列
    6. for (int i = 0; i < 5; i++)
    7. {
    8. item = new QStandardItem("0");
    9. itemList << item;
    10. }
    11. //最后一列
    12. QString headerStr = model->headerData(model->columnCount() - 1, Qt::Horizontal, Qt::DisplayRole).toString();
    13. item = new QStandardItem(headerStr);
    14. item->setCheckable(true);
    15. itemList << item;
    16. QModelIndex currentIndex = selection->currentIndex();
    17. model->insertRow(currentIndex.row(), itemList);
    18. selection->clearSelection();
    19. selection->setCurrentIndex(currentIndex, QItemSelectionModel::Select);
    20. }

    删除行

    1. void port::on_del_triggered()
    2. {
    3. QModelIndex curindex = selection->currentIndex();
    4. if (curindex.row() == model->rowCount() - 1)
    5. model->removeRow(curindex.row());
    6. else
    7. {
    8. model->removeRow(curindex.row());
    9. selection->setCurrentIndex(curindex,QItemSelectionModel::Select);
    10. }
    11. }

    设置表格内容格式居左:

    1. void port::on_left_triggered()
    2. {
    3. if (!selection->hasSelection()) //没有选择的项
    4. return;
    5. //获取选择的单元格的模型索引列表,可以是多选
    6. QModelIndexList selectedIndex = selection->selectedIndexes();
    7. for (int i = 0; i < selectedIndex.count(); i++)
    8. {
    9. QModelIndex aIndex = selectedIndex.at(i); //获取其中的一个模型索引
    10. QStandardItem* aItem = model->itemFromIndex(aIndex);//获取一个单元格的项数据对象
    11. aItem->setTextAlignment(Qt::AlignLeft);//设置文字对齐方式
    12. }
    13. }

    设置居中:

    1. void port::on_center_triggered()
    2. {
    3. if (!selection->hasSelection())
    4. return;
    5. QModelIndexList selectedIndex = selection->selectedIndexes();
    6. QModelIndex aIndex;
    7. QStandardItem *aItem;
    8. for (int i = 0; i < selectedIndex.count(); i++)
    9. {
    10. aIndex = selectedIndex.at(i);
    11. aItem = model->itemFromIndex(aIndex);
    12. aItem->setTextAlignment(Qt::AlignHCenter);
    13. }
    14. }

    获取表格选中的行数:

    1. // 在构造函数或初始化函数中连接信号与槽
    2. connect(tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MyClass::handleSelectionChanged);
    3. // 槽函数,处理选中行的行号
    4. void MyClass::handleSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
    5. QModelIndexList selectedIndexes = selected.indexes();
    6. if (!selectedIndexes.isEmpty()) {
    7. int row = selectedIndexes.first().row();
    8. qDebug() << "Selected row: " << row;
    9. }
    10. }

    获取QTabelView表格中指定行的内容:

    1. // 获取指定行的内容
    2. int row = 3; // 指定行号
    3. int column = 1; // 指定列号
    4. QModelIndex index = model->index(row, column); // 获取指定行的索引
    5. QVariant data = model->data(index); // 获取指定索引处的数据
    6. QString content = data.toString(); // 转换为字符串类型

  • 相关阅读:
    用户生命周期价值LTV
    企业该如何选择数字化转型工具?
    四轴飞控DIY Mark4 - 整理&参数优化
    【C++面向对象侯捷】5.操作符重载与临时对象
    元宇宙的八大典型应用场景
    1314. 矩阵区域和-矩阵前缀和算法
    精通Java事务编程(6)-可串行化隔离级别之真串行
    Windows 进程监视工具
    cvpr24写作模板pdfLaTex编译器注意点小结
    Go语言连不上 Mysql
  • 原文地址:https://blog.csdn.net/weixin_38241876/article/details/133946698