diff --git a/QtQStandardItemModelEx/ExQStandardItemModel.cpp b/QtQStandardItemModelEx/ExQStandardItemModel.cpp new file mode 100644 index 0000000..cfc542f --- /dev/null +++ b/QtQStandardItemModelEx/ExQStandardItemModel.cpp @@ -0,0 +1,122 @@ +#include "ExQStandardItemModel.h" +#include "ui_ExQStandardItemModel.h" +#include +#include + +ExQStandardItemModel::ExQStandardItemModel(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::ExQStandardItemModel) +{ + ui->setupUi(this); + setWindowTitle(QObject::tr("QTableView和QStandardItemModel的用法")); + + ui->mainToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); //设置主工具栏的图标样式风格 + + m_labCurrFile = new QLabel("当前文件:", this); //设置状态栏 + m_labCellPos = new QLabel("当前单元格:", this); + m_labCellText = new QLabel("单元格内容:", this); + m_labCurrFile->setMinimumWidth(200); + m_labCellPos->setMinimumWidth(200); + m_labCellText->setMinimumWidth(200); + ui->statusBar->addWidget(m_labCurrFile); + ui->statusBar->addWidget(m_labCellPos); + ui->statusBar->addWidget(m_labCellText); + + m_model = new QStandardItemModel(2, 6, this); //设置数据模型,一开始设置为默认的2行6列表的一个表 + m_selectModet = new QItemSelectionModel(m_model, this); //设置选择模型 + + ui->tableView->setModel(m_model); //设置数据模型 + ui->tableView->setSelectionModel(m_selectModet); //设置选择模型 + ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection); //设置选择模式 + ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems); //设置选择行为 + + connect(m_selectModet, SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(onCurrentChanged(QModelIndex, QModelIndex))); //选择当前单元格变化时的信号与槽 +} + +ExQStandardItemModel::~ExQStandardItemModel() +{ + delete ui; +} + +//从list初始化数据模型QTableView里面 +void ExQStandardItemModel::init(QStringList &list) +{ + int rowCount = list.count(); //文本行数,第一行为表头 + m_model->setRowCount(rowCount - 1); + + QString header = list.at(0); + QStringList headerList = header.split(QRegExp("\\s+"), QString::SkipEmptyParts); //通过一个或者多个空格或者tab按键切割 + m_model->setHorizontalHeaderLabels(headerList); //设置表头 + + QStandardItem *item = nullptr; //此处开始,设置表格数据 + QStringList tempList; + int j = 0; + + for (int i = 1; i < rowCount; i++) { + 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 + item = new QStandardItem(tempList.at(j)); + m_model->setItem(i - 1, j, item); + } + + item = new QStandardItem(tempList.at(j)); //最后一列的item + item->setCheckable(true); //设置有检查框 + + if ( tempList.at(j) == "https://www.google.com") + item->setCheckState(Qt::Unchecked); + else + item->setCheckState(Qt::Checked); + + m_model->setItem(i - 1, 5, item); + } +} + +//当前单元格发生变化时 +void ExQStandardItemModel::onCurrentChanged(const QModelIndex ¤t, const QModelIndex &previous) +{ + +} + + +//action+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +//打开和导入文件,并且在plainTextEdit里面显示 +void ExQStandardItemModel::on_actOpen_triggered() +{ + QString currPath = QCoreApplication::applicationDirPath(); + QString fileName = QFileDialog::getOpenFileName(this, "打开一个文件", currPath, "导入数据文件(*txt); 所有文件(*.*)"); + + if (fileName.isEmpty()) + return; + + QStringList list; + QFile file(fileName); + if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { //只读形式打开文本文件 + QTextStream stream(&file); //用文本流读取文件 + ui->plainTextEdit->clear(); + + while (!stream.atEnd()) { //读取文本中文本的内容 + QString str = stream.readLine(); + ui->plainTextEdit->appendPlainText(str); + list.append(str); + } + + file.close(); //关闭 + m_labCurrFile->setText("当前文件:" + fileName); //设置状态栏 + + ui->actAppend->setEnabled(true); //设置action的Enabled的属性 + ui->actInsert->setEnabled(true); + ui->actDelete->setEnabled(true); + ui->actSave->setEnabled(true); + } + + init(list); //初始化.txt的数据 +} + + +//在表格的最后一行添加一行 +void ExQStandardItemModel::on_actAppend_triggered() +{ + +} diff --git a/QtQStandardItemModelEx/ExQStandardItemModel.h b/QtQStandardItemModelEx/ExQStandardItemModel.h new file mode 100644 index 0000000..15fd0e6 --- /dev/null +++ b/QtQStandardItemModelEx/ExQStandardItemModel.h @@ -0,0 +1,40 @@ +#ifndef EXQSTANDARDITEMMODEL_H +#define EXQSTANDARDITEMMODEL_H + +#include +#include +#include +#include + +namespace Ui { +class ExQStandardItemModel; +} + +class ExQStandardItemModel : public QMainWindow +{ + Q_OBJECT + +public: + explicit ExQStandardItemModel(QWidget *parent = nullptr); + ~ExQStandardItemModel(); + +private: + void init(QStringList& list); //从list初始化数据模型 + +private slots: + void onCurrentChanged(const QModelIndex& current, const QModelIndex& previous); //当前单元格发生变化时 + void on_actOpen_triggered(); //打开和导入文件,并且在plainTextEdit里面显示 + void on_actAppend_triggered(); //在表格的最后一行添加一行 + +private: + Ui::ExQStandardItemModel *ui; + + QLabel *m_labCurrFile; //当前文件 + QLabel *m_labCellPos; //当前单元格行列号 + QLabel *m_labCellText; //当前单元格数据内容 + QStandardItemModel *m_model; //数据模型 + QItemSelectionModel *m_selectModet; //选择模型 + +}; + +#endif // EXQSTANDARDITEMMODEL_H diff --git a/QtQStandardItemModelEx/ExQStandardItemModel.ui b/QtQStandardItemModelEx/ExQStandardItemModel.ui new file mode 100644 index 0000000..68e7b70 --- /dev/null +++ b/QtQStandardItemModelEx/ExQStandardItemModel.ui @@ -0,0 +1,259 @@ + + + ExQStandardItemModel + + + + 0 + 0 + 739 + 403 + + + + ExQStandardItemModel + + + + + + + Qt::Horizontal + + + + + + + Table View: + + + + + + + + + + + + + + PlainTextEdit: + + + + + + + + + + + + + + + + 0 + 0 + 739 + 23 + + + + + + TopToolBarArea + + + false + + + + + + + + + + + + + + + + + + + + + :/images/Image011.jpg:/images/Image011.jpg + + + 打开 + + + 打开文件夹 + + + Ctrl+1 + + + + + + :/images/Image001.png:/images/Image001.png + + + 保存 + + + 保存文件 + + + Ctrl+S + + + + + + :/images/Image012.png:/images/Image012.png + + + 添加 + + + 添加一行 + + + + + + :/images/Image008.jpg:/images/Image008.jpg + + + 插入 + + + 插入一行 + + + Ins + + + + + + :/images/Image009.jpg:/images/Image009.jpg + + + 删除 + + + 删除一行 + + + Del + + + + + + :/images/Image007.jpg:/images/Image007.jpg + + + 退出 + + + 退出程序 + + + Ctrl+0 + + + + + + :/images/Image010.jpg:/images/Image010.jpg + + + 预览 + + + 预览数据模型到文本框 + + + Ctrl+2 + + + + + + :/images/Image004.png:/images/Image004.png + + + 居左 + + + 文字左对齐 + + + Ctrl+3 + + + + + + :/images/Image006.png:/images/Image006.png + + + 居中 + + + 文本居中 + + + Ctrl+4 + + + + + + :/images/Image005.png:/images/Image005.png + + + 居右 + + + 文本右对齐 + + + Ctrl+5 + + + + + true + + + + :/images/Image003.png:/images/Image003.png + + + 粗体 + + + 文字设置粗体 + + + Ctrl+B + + + + + + + + + diff --git a/QtQStandardItemModelEx/QtQStandardItemModelEx.pro b/QtQStandardItemModelEx/QtQStandardItemModelEx.pro new file mode 100644 index 0000000..f9056fb --- /dev/null +++ b/QtQStandardItemModelEx/QtQStandardItemModelEx.pro @@ -0,0 +1,45 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-09-21T21:51:12 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QtQStandardItemModelEx +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + ExQStandardItemModel.cpp + +HEADERS += \ + ExQStandardItemModel.h + +FORMS += \ + ExQStandardItemModel.ui + +RC_ICONS += qt.ico + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +RESOURCES += \ + resources.qrc diff --git a/QtQStandardItemModelEx/images/Image001.png b/QtQStandardItemModelEx/images/Image001.png new file mode 100644 index 0000000..6fdf4fc Binary files /dev/null and b/QtQStandardItemModelEx/images/Image001.png differ diff --git a/QtQStandardItemModelEx/images/Image002.png b/QtQStandardItemModelEx/images/Image002.png new file mode 100644 index 0000000..d2dce04 Binary files /dev/null and b/QtQStandardItemModelEx/images/Image002.png differ diff --git a/QtQStandardItemModelEx/images/Image003.png b/QtQStandardItemModelEx/images/Image003.png new file mode 100644 index 0000000..4010109 Binary files /dev/null and b/QtQStandardItemModelEx/images/Image003.png differ diff --git a/QtQStandardItemModelEx/images/Image004.png b/QtQStandardItemModelEx/images/Image004.png new file mode 100644 index 0000000..89e496a Binary files /dev/null and b/QtQStandardItemModelEx/images/Image004.png differ diff --git a/QtQStandardItemModelEx/images/Image005.png b/QtQStandardItemModelEx/images/Image005.png new file mode 100644 index 0000000..9b5a39c Binary files /dev/null and b/QtQStandardItemModelEx/images/Image005.png differ diff --git a/QtQStandardItemModelEx/images/Image006.png b/QtQStandardItemModelEx/images/Image006.png new file mode 100644 index 0000000..01d01db Binary files /dev/null and b/QtQStandardItemModelEx/images/Image006.png differ diff --git a/QtQStandardItemModelEx/images/Image007.jpg b/QtQStandardItemModelEx/images/Image007.jpg new file mode 100644 index 0000000..db6317d Binary files /dev/null and b/QtQStandardItemModelEx/images/Image007.jpg differ diff --git a/QtQStandardItemModelEx/images/Image008.jpg b/QtQStandardItemModelEx/images/Image008.jpg new file mode 100644 index 0000000..f963db2 Binary files /dev/null and b/QtQStandardItemModelEx/images/Image008.jpg differ diff --git a/QtQStandardItemModelEx/images/Image009.jpg b/QtQStandardItemModelEx/images/Image009.jpg new file mode 100644 index 0000000..10e5c42 Binary files /dev/null and b/QtQStandardItemModelEx/images/Image009.jpg differ diff --git a/QtQStandardItemModelEx/images/Image010.jpg b/QtQStandardItemModelEx/images/Image010.jpg new file mode 100644 index 0000000..b7cbb9a Binary files /dev/null and b/QtQStandardItemModelEx/images/Image010.jpg differ diff --git a/QtQStandardItemModelEx/images/Image011.jpg b/QtQStandardItemModelEx/images/Image011.jpg new file mode 100644 index 0000000..995f150 Binary files /dev/null and b/QtQStandardItemModelEx/images/Image011.jpg differ diff --git a/QtQStandardItemModelEx/images/Image012.png b/QtQStandardItemModelEx/images/Image012.png new file mode 100644 index 0000000..7c62593 Binary files /dev/null and b/QtQStandardItemModelEx/images/Image012.png differ diff --git a/QtQStandardItemModelEx/main.cpp b/QtQStandardItemModelEx/main.cpp new file mode 100644 index 0000000..40530df --- /dev/null +++ b/QtQStandardItemModelEx/main.cpp @@ -0,0 +1,11 @@ +#include "ExQStandardItemModel.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + ExQStandardItemModel w; + w.show(); + + return a.exec(); +} diff --git a/QtQStandardItemModelEx/qt.ico b/QtQStandardItemModelEx/qt.ico new file mode 100644 index 0000000..0ee0568 Binary files /dev/null and b/QtQStandardItemModelEx/qt.ico differ diff --git a/QtQStandardItemModelEx/resources.qrc b/QtQStandardItemModelEx/resources.qrc new file mode 100644 index 0000000..9a367d9 --- /dev/null +++ b/QtQStandardItemModelEx/resources.qrc @@ -0,0 +1,16 @@ + + + images/Image001.png + images/Image002.png + images/Image003.png + images/Image004.png + images/Image005.png + images/Image006.png + images/Image007.jpg + images/Image008.jpg + images/Image009.jpg + images/Image010.jpg + images/Image011.jpg + images/Image012.png + + diff --git a/QtQStandardItemModelEx/录入的人员信息.txt b/QtQStandardItemModelEx/录入的人员信息.txt new file mode 100644 index 0000000..74f3cc5 --- /dev/null +++ b/QtQStandardItemModelEx/录入的人员信息.txt @@ -0,0 +1,14 @@ +(name) Ա(sex) (age) (cm) (kg) ϵ(blog) +Ͷľ Ů 18 175 57 https://touwoyimuli.github.io +֮ Ů 18 174 58 https://github.com/touwoyimuli +˱Ҳ Ů 18 173 59 https://www.google.com +ΪҲ Ů 18 172 60 https://www.google.com +Ͷľ Ů 18 171 56 https://www.google.com +֮ Ů 18 170 55 https://www.google.com +˱Ҳ Ů 18 176 54 https://www.google.com +ΪҲ Ů 18 177 53 https://www.google.com +Ͷľ Ů 18 178 52 https://www.google.com +֮ Ů 18 179 51 https://www.google.com +˱Ҳ Ů 18 180 50 https://www.google.com +ΪҲ Ů 18 181 49 https://www.google.com +Ը԰쳯Ϧ 18 182 48 https://blog.csdn.net/qq_33154343