【QT系列】tableView

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QStandardItemModel>
#include <unordered_set>

QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();


public slots:
    void dataChangedSlot(const QModelIndex &topLeft, const QModelIndex &bottomRight,
            const QVector<int> &roles = QVector<int> ());

private slots:
    void ShowSchoolMenu(const QPoint &pos);
    void ShowClassMenu(const QPoint &pos);
    void ClickCell(const QModelIndex & index);

private:
    Ui::Dialog *ui;

    QStandardItemModel *m_pSchoolModel;
    QStandardItemModel *m_pClassModel;
    std::unordered_set<QString> m_usSchool;

};
#endif // DIALOG_H


先写一点 后续完善

#include "dialog.h"
#include "ui_dialog.h"
#include <QMenu>
#include <QMessageBox>
#include <QDebug>
#include <QKeyEvent>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
   
    ui->setupUi(this);
    m_pSchoolModel = new QStandardItemModel(this);
    m_pClassModel = new QStandardItemModel(this);

    m_pSchoolModel->setHorizontalHeaderLabels(QStringList()<< "班级名称");
    m_pClassModel->setHorizontalHeaderLabels(QStringList()<< "学号"<<"姓名"<<"性别"<<"年龄"<<"身高");
    ui->tableViewSchool->setModel(m_pSchoolModel);
    ui->tableViewSchool->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableViewClass->setModel(m_pClassModel);
    ui->tableViewClass->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->tableViewSchool->setContextMenuPolicy(Qt::CustomContextMenu);
    ui->tableViewClass->setContextMenuPolicy(Qt::CustomContextMenu);

    connect(ui->tableViewSchool, SIGNAL(customContextMenuRequested(const QPoint &)), this ,SLOT(ShowSchoolMenu(const QPoint&)));
    connect(ui->tableViewClass, SIGNAL(customContextMenuRequested(const QPoint &)), this ,SLOT(ShowClassMenu(const QPoint&)));
    connect(ui->tableViewSchool, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(ClickCell(const QModelIndex &)));
    connect(m_pSchoolModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),
            this, SLOT(dataChangedSlot(QModelIndex,QModelIndex,QVector<int>)));
}

Dialog::~Dialog()
{
   
    delete ui;
}
// const QModelIndex &bottomRight, const QVector<int> &roles
void Dialog::dataChangedSlot(const QModelIndex &topLeft, const QModelIndex &, const QVector<int> &)
{
   
     qDebug() << "dataChangedSlot" << m_pSchoolModel->data(topLeft).toString() << endl;
     QString qstrClassName = m_pSchoolModel->data(topLeft).toString();
     if(qstrClassName.size() > 0  && m_usSchool.count(qstrClassName) == 0)
     {
   
         m_usSchool.insert(qstrClassName);
     }else
     {
   
         QMessageBox::warning(this, "提示", "请勿重复创建班级");
         int nIndex = ui->tableViewSchool->currentIndex().row();
         m_pSchoolModel->removeRow(nIndex);
     }
}

void Dialog::ShowSchoolMenu(const QPoint &)
{
   
    QMenu *menu = new QMenu(this);
    QAction* pAdd = new QAction(this);
    pAdd->setText("新增一行");
    connect(pAdd, &QAction::triggered, this, [&]()
    {
   
        int nIndex = ui->tableViewSchool->currentIndex().row();
        QAbstractItemModel *pModel = ui->tableViewSchool->model();
        pModel->insertRow(nIndex + 1);
        ui->tableViewSchool->update();
    });
    menu->addAction(pAdd);
    menu->exec(QCursor::pos());
}

void Dialog::ShowClassMenu(const QPoint&)
{
   
    QMenu *menu = new QMenu(this);
    QAction* pAdd = new QAction(this);
    pAdd->setText("新增一行");
    connect(pAdd, &QAction::triggered, this, [&]()
    {
   
        if(ui->tableViewSchool->currentIndex().row() < 0)
        {
   
            QMessageBox::warning(this, "提示", "请先创建班级");
            return;
        }
        int nIndex = ui->tableViewClass->currentIndex().row();
        QAbstractItemModel *pModel = ui->tableViewClass->model();
        pModel->insertRow(nIndex + 1);
        ui->tableViewClass->update();
    });
    menu->addAction(pAdd);
    menu->exec(QCursor::pos());
}

void Dialog::ClickCell(const QModelIndex & index)
{
   
    QVariant data = index.data();
    qDebug() << "ClickCell" << data.toString();   // 获取旧的值l
}

相关推荐

  1. QT系列tableView

    2024-02-04 06:18:01       44 阅读
  2. Swift中TableView的原理

    2024-02-04 06:18:01       28 阅读
  3. Swift中TableView的使用

    2024-02-04 06:18:01       35 阅读
  4. 1.Swift基础控件:TableView列表

    2024-02-04 06:18:01       36 阅读
  5. Swift中TableView的编辑模式

    2024-02-04 06:18:01       31 阅读
  6. pyqt5 tableView实现excel拖曳填充

    2024-02-04 06:18:01       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-04 06:18:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-04 06:18:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-04 06:18:01       82 阅读
  4. Python语言-面向对象

    2024-02-04 06:18:01       91 阅读

热门阅读

  1. docker 搭建 Seafile 集成 onlyoffice

    2024-02-04 06:18:01       60 阅读
  2. 如何确定子网地址(范例)?

    2024-02-04 06:18:01       48 阅读
  3. 用linux文件系统的链接功能实现文件缓存LRU

    2024-02-04 06:18:01       54 阅读
  4. kafka排除zookeeper使用kraft的最新部署方案

    2024-02-04 06:18:01       45 阅读
  5. kafka自定义分区策略详解

    2024-02-04 06:18:01       43 阅读
  6. Xlua分析:C#调用Lua

    2024-02-04 06:18:01       40 阅读
  7. 数组和List之间的相互转换

    2024-02-04 06:18:01       49 阅读