#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
}