From 594e96fa501e8cffc8c280289db4f35d0e3f9c08 Mon Sep 17 00:00:00 2001 From: touwoyimuli Date: Fri, 27 Sep 2019 23:56:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E3=80=81=E5=88=A0?= =?UTF-8?q?=E9=99=A4=EF=BC=8C=E9=A2=84=E8=A7=88=EF=BC=8C=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E7=AD=89=E5=8A=9F=E8=83=BD=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 基本这里就是QStandardItemmodel的实际使用写完了;后面再写,就是在此基础上写代理部分功能 --- .../ExQStandardItemModel.cpp | 209 +++++++++++++++++- QtQStandardItemModelEx/ExQStandardItemModel.h | 25 ++- 2 files changed, 219 insertions(+), 15 deletions(-) diff --git a/QtQStandardItemModelEx/ExQStandardItemModel.cpp b/QtQStandardItemModelEx/ExQStandardItemModel.cpp index cfc542f..5eb7b7b 100644 --- a/QtQStandardItemModelEx/ExQStandardItemModel.cpp +++ b/QtQStandardItemModelEx/ExQStandardItemModel.cpp @@ -2,6 +2,10 @@ #include "ui_ExQStandardItemModel.h" #include #include +#include +#include + +#define COLUMN 6 //数据表的列数 ExQStandardItemModel::ExQStandardItemModel(QWidget *parent) : QMainWindow(parent), @@ -22,7 +26,7 @@ ExQStandardItemModel::ExQStandardItemModel(QWidget *parent) : ui->statusBar->addWidget(m_labCellPos); ui->statusBar->addWidget(m_labCellText); - m_model = new QStandardItemModel(2, 6, this); //设置数据模型,一开始设置为默认的2行6列表的一个表 + m_model = new QStandardItemModel(2, COLUMN, this); //设置数据模型,一开始设置为默认的2行6列表的一个表 m_selectModet = new QItemSelectionModel(m_model, this); //设置选择模型 ui->tableView->setModel(m_model); //设置数据模型 @@ -56,7 +60,7 @@ void ExQStandardItemModel::init(QStringList &list) QString aLineText = list.at(i); tempList = aLineText.split(QRegExp("\\s+"), QString::SkipEmptyParts);//正则表达式中\s匹配任何空白字符,包括空格、制表符、换页符等等, 等价于[ \f\n\r\t\v] - for ( j = 0; j < 6 - 1; j++) { //设置前5列的item + for ( j = 0; j < COLUMN - 1; j++) { //设置前5列的item item = new QStandardItem(tempList.at(j)); m_model->setItem(i - 1, j, item); } @@ -69,23 +73,29 @@ void ExQStandardItemModel::init(QStringList &list) else item->setCheckState(Qt::Checked); - m_model->setItem(i - 1, 5, item); + m_model->setItem(i - 1, COLUMN -1, item); } } //当前单元格发生变化时 void ExQStandardItemModel::onCurrentChanged(const QModelIndex ¤t, const QModelIndex &previous) { + if (current.isValid()) { //当前模型索性有效 + m_labCellPos->setText(QString("当前单元格:%1行, %2列").arg(current.row()).arg(current.column())); + QStandardItem *item = m_model->itemFromIndex(current); //从模型索引获得Item + m_labCellText->setText(QString("当前文件:%1").arg(item->text())); //显示item的文字内容 + QFont font = item->font(); + ui->actBold->setChecked(font.bold()); //更新actFontBold的check状态 + } } - //action+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //打开和导入文件,并且在plainTextEdit里面显示 void ExQStandardItemModel::on_actOpen_triggered() { QString currPath = QCoreApplication::applicationDirPath(); - QString fileName = QFileDialog::getOpenFileName(this, "打开一个文件", currPath, "导入数据文件(*txt); 所有文件(*.*)"); + QString fileName = QFileDialog::getOpenFileName(this, "打开一个文件", currPath, "导入数据文件(*txt);;所有文件(*.*)"); if (fileName.isEmpty()) return; @@ -111,12 +121,197 @@ void ExQStandardItemModel::on_actOpen_triggered() ui->actSave->setEnabled(true); } - init(list); //初始化.txt的数据 + init(list); //初始化.txt的数据 } - //在表格的最后一行添加一行 void ExQStandardItemModel::on_actAppend_triggered() { + QList list; + QStandardItem *item; + //获取表头前五列 + for (int i = 0; i < COLUMN - 1; i++) { + item = new QStandardItem("添加一行"); + list<headerData(m_model->columnCount() - 1, Qt::Horizontal, Qt::DisplayRole).toString(); + item = new QStandardItem(str + "添加一行"); + item->setCheckable(true); + list<insertRow(m_model->rowCount(), list); + QModelIndex currIndex = m_model->index(m_model->rowCount() - 1, 0); //创建最后一行的ModelIndex + m_selectModet->clearSelection(); + m_selectModet->setCurrentIndex(currIndex, QItemSelectionModel::Select); +} + +//保存文件 +void ExQStandardItemModel::on_actSave_triggered() +{ + QString path = QCoreApplication::applicationDirPath(); + QString fileName = QFileDialog::getSaveFileName(this, tr("选择一个文件"), path, "另存数据(*.txt);;所有数据(*.*)"); + if ( fileName.isEmpty()) + return; + + QFile file(fileName); + + if (!(file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate))) //读写,文本,覆盖原有内容打开 + return; + + QTextStream stream(&file); //用文本流读取文件 + QStandardItem *item; + int i = 0 ; + int j = 0; + QString str = ""; + ui->plainTextEdit->clear(); + + //获取表头文字 + for (i = 0; i < m_model->columnCount(); i++) { + item = m_model->horizontalHeaderItem(i); + str = str + item->text() + "\t\t"; //以TAB按键隔开 + } + stream<plainTextEdit->appendPlainText(str); + + //获取数据区文字 + for (i = 1; i < m_model->rowCount(); i++) { + str = ""; + + for (j = 0; j < m_model->columnCount(); j++) { + item = m_model->item(i, j); + str = str + item->text() + "\t\t"; + } + stream<plainTextEdit->appendPlainText(str); + } +} + +//在当前选中的一行,其前面插入一行 +void ExQStandardItemModel::on_actInsert_triggered() +{ + QList list; + QStandardItem *item; + + for (int i = 0; i < COLUMN - 1; i++) { + item = new QStandardItem("插入一行"); + list<headerData(m_model->columnCount() - 1, Qt::Horizontal, Qt::DisplayRole).toString(); + item = new QStandardItem(str + "插入一行"); + item->setCheckable(true); + list<currentIndex(); + m_model->insertRow(currIndex.row(), list); //在当前行的前面插入一行 + m_selectModet->clearSelection(); + m_selectModet->setCurrentIndex(currIndex, QItemSelectionModel::Select); +} + + +//删除一行 +void ExQStandardItemModel::on_actDelete_triggered() +{ + QModelIndex currIndex = m_selectModet->currentIndex(); + m_model->removeRow(currIndex.row()); +} + +//关闭退出 +void ExQStandardItemModel::on_actExit_triggered() +{ + close(); +} + +//预览数据模型:模型数据导出到PlainTextEdit显示 +void ExQStandardItemModel::on_actModelData_triggered() +{ //用文本流读取文件 + QStandardItem *item; + int i = 0 ; + int j = 0; + QString str = ""; + ui->plainTextEdit->clear(); + + //获取表头文字 + for (i = 0; i < m_model->columnCount(); i++) { + item = m_model->horizontalHeaderItem(i); + str = str + item->text() + "\t\t"; //以TAB按键隔开 + } + + ui->plainTextEdit->appendPlainText(str); + + //获取数据区文字 + for (i = 1; i < m_model->rowCount(); i++) { + str = ""; + + for (j = 0; j < m_model->columnCount(); j++) { + item = m_model->item(i, j); + str = str + item->text() + "\t\t"; + } + ui->plainTextEdit->appendPlainText(str); + } +} + +//左对齐 +void ExQStandardItemModel::on_actAlignLeft_triggered() +{ + if (!m_selectModet->hasSelection()) + return; + + QModelIndexList list = m_selectModet->selectedIndexes(); //获得选中的List + + for (int i = 0; i < list.count(); i++) { + QModelIndex index = list.at(i); + QStandardItem *item = m_model->itemFromIndex(index); + item->setTextAlignment(Qt::AlignLeft); //设置文本左对齐 + } +} + +//文本居中 +void ExQStandardItemModel::on_actAlignCenter_triggered() +{ + if (!m_selectModet->hasSelection()) + return; + + QModelIndexList list = m_selectModet->selectedIndexes(); //获得选中的List + + for (int i = 0; i < list.count(); i++) { + QModelIndex index = list.at(i); //获取一个模型索引 + QStandardItem *item = m_model->itemFromIndex(index); + item->setTextAlignment(Qt::AlignCenter); //设置文本居中 + } +} + +//文本右对齐 +void ExQStandardItemModel::on_actAlingRight_triggered() +{ + if (!m_selectModet->hasSelection()) + return; + + QModelIndexList list = m_selectModet->selectedIndexes(); + + for (int i = 0; i < list.count(); i++) { + QModelIndex index = list.at(i); + QStandardItem *item = m_model->itemFromIndex(index); + item->setTextAlignment(Qt::AlignRight); + } +} + +//文本加粗 +void ExQStandardItemModel::on_actBold_triggered(bool checked) +{ + if (!m_selectModet->hasSelection()) + return; + + QModelIndexList list = m_selectModet->selectedIndexes(); + + for (int i = 0; i < list.count(); i++) { + QModelIndex index = list.at(i); + QStandardItem *item = m_model->itemFromIndex(index); + QFont font = item->font(); + font.setBold(checked); + item->setFont(font); + } } diff --git a/QtQStandardItemModelEx/ExQStandardItemModel.h b/QtQStandardItemModelEx/ExQStandardItemModel.h index 15fd0e6..2c119e7 100644 --- a/QtQStandardItemModelEx/ExQStandardItemModel.h +++ b/QtQStandardItemModelEx/ExQStandardItemModel.h @@ -19,21 +19,30 @@ public: ~ExQStandardItemModel(); private: - void init(QStringList& list); //从list初始化数据模型 + void init(QStringList& list); //从list初始化数据模型 private slots: void onCurrentChanged(const QModelIndex& current, const QModelIndex& previous); //当前单元格发生变化时 - void on_actOpen_triggered(); //打开和导入文件,并且在plainTextEdit里面显示 - void on_actAppend_triggered(); //在表格的最后一行添加一行 + void on_actOpen_triggered(); //打开和导入文件,并且在plainTextEdit里面显示 + void on_actAppend_triggered(); //在表格的最后一行添加一行 + void on_actSave_triggered(); //保存文件 + void on_actInsert_triggered(); //在当前选中的一行,其前面插入一行 + void on_actDelete_triggered(); //删除一行 + void on_actExit_triggered(); //关闭退出 + void on_actModelData_triggered(); //预览模型 + void on_actAlignLeft_triggered(); //左对齐 + void on_actAlignCenter_triggered(); //文本居中 + void on_actAlingRight_triggered(); //文本右对齐 + void on_actBold_triggered(bool checked);//文本加粗 private: Ui::ExQStandardItemModel *ui; - QLabel *m_labCurrFile; //当前文件 - QLabel *m_labCellPos; //当前单元格行列号 - QLabel *m_labCellText; //当前单元格数据内容 - QStandardItemModel *m_model; //数据模型 - QItemSelectionModel *m_selectModet; //选择模型 + QLabel *m_labCurrFile; //当前文件 + QLabel *m_labCellPos; //当前单元格行列号 + QLabel *m_labCellText; //当前单元格数据内容 + QStandardItemModel *m_model; //数据模型 + QItemSelectionModel *m_selectModet; //选择模型 };