feat: 创建UI和实现init()函数,文件内容值到QTableView和QStandardItemModel里面

This commit is contained in:
touwoyimuli 2019-09-23 00:00:16 +08:00
parent 5bbd3229ef
commit a251d988fb
20 changed files with 507 additions and 0 deletions

View File

@ -0,0 +1,122 @@
#include "ExQStandardItemModel.h"
#include "ui_ExQStandardItemModel.h"
#include <QDebug>
#include <QFileDialog>
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 &current, 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()
{
}

View File

@ -0,0 +1,40 @@
#ifndef EXQSTANDARDITEMMODEL_H
#define EXQSTANDARDITEMMODEL_H
#include <QMainWindow>
#include <QLabel>
#include <QStandardItemModel>
#include <QItemSelectionModel>
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

View File

@ -0,0 +1,259 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ExQStandardItemModel</class>
<widget class="QMainWindow" name="ExQStandardItemModel">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>739</width>
<height>403</height>
</rect>
</property>
<property name="windowTitle">
<string>ExQStandardItemModel</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QWidget" name="">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Table View:</string>
</property>
</widget>
</item>
<item>
<widget class="QTableView" name="tableView"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>PlainTextEdit:</string>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="plainTextEdit"/>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>739</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actOpen"/>
<addaction name="actSave"/>
<addaction name="actModelData"/>
<addaction name="separator"/>
<addaction name="actAppend"/>
<addaction name="actInsert"/>
<addaction name="actDelete"/>
<addaction name="separator"/>
<addaction name="actAlignLeft"/>
<addaction name="actAlignCenter"/>
<addaction name="actAlingRight"/>
<addaction name="actBold"/>
<addaction name="separator"/>
<addaction name="actExit"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actOpen">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image011.jpg</normaloff>:/images/Image011.jpg</iconset>
</property>
<property name="text">
<string>打开</string>
</property>
<property name="toolTip">
<string>打开文件夹</string>
</property>
<property name="shortcut">
<string>Ctrl+1</string>
</property>
</action>
<action name="actSave">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image001.png</normaloff>:/images/Image001.png</iconset>
</property>
<property name="text">
<string>保存</string>
</property>
<property name="toolTip">
<string>保存文件</string>
</property>
<property name="shortcut">
<string>Ctrl+S</string>
</property>
</action>
<action name="actAppend">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image012.png</normaloff>:/images/Image012.png</iconset>
</property>
<property name="text">
<string>添加</string>
</property>
<property name="toolTip">
<string>添加一行</string>
</property>
</action>
<action name="actInsert">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image008.jpg</normaloff>:/images/Image008.jpg</iconset>
</property>
<property name="text">
<string>插入</string>
</property>
<property name="toolTip">
<string>插入一行</string>
</property>
<property name="shortcut">
<string>Ins</string>
</property>
</action>
<action name="actDelete">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image009.jpg</normaloff>:/images/Image009.jpg</iconset>
</property>
<property name="text">
<string>删除</string>
</property>
<property name="toolTip">
<string>删除一行</string>
</property>
<property name="shortcut">
<string>Del</string>
</property>
</action>
<action name="actExit">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image007.jpg</normaloff>:/images/Image007.jpg</iconset>
</property>
<property name="text">
<string>退出</string>
</property>
<property name="toolTip">
<string>退出程序</string>
</property>
<property name="shortcut">
<string>Ctrl+0</string>
</property>
</action>
<action name="actModelData">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image010.jpg</normaloff>:/images/Image010.jpg</iconset>
</property>
<property name="text">
<string>预览</string>
</property>
<property name="toolTip">
<string>预览数据模型到文本框</string>
</property>
<property name="shortcut">
<string>Ctrl+2</string>
</property>
</action>
<action name="actAlignLeft">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image004.png</normaloff>:/images/Image004.png</iconset>
</property>
<property name="text">
<string>居左</string>
</property>
<property name="toolTip">
<string>文字左对齐</string>
</property>
<property name="shortcut">
<string>Ctrl+3</string>
</property>
</action>
<action name="actAlignCenter">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image006.png</normaloff>:/images/Image006.png</iconset>
</property>
<property name="text">
<string>居中</string>
</property>
<property name="toolTip">
<string>文本居中</string>
</property>
<property name="shortcut">
<string>Ctrl+4</string>
</property>
</action>
<action name="actAlingRight">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image005.png</normaloff>:/images/Image005.png</iconset>
</property>
<property name="text">
<string>居右</string>
</property>
<property name="toolTip">
<string>文本右对齐</string>
</property>
<property name="shortcut">
<string>Ctrl+5</string>
</property>
</action>
<action name="actBold">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image003.png</normaloff>:/images/Image003.png</iconset>
</property>
<property name="text">
<string>粗体</string>
</property>
<property name="toolTip">
<string>文字设置粗体</string>
</property>
<property name="shortcut">
<string>Ctrl+B</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
<include location="resources.qrc"/>
</resources>
<connections/>
</ui>

View File

@ -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

Binary file not shown.

After

Width:  |  Height:  |  Size: 797 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 865 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 287 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 957 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 827 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 826 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 906 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 607 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -0,0 +1,11 @@
#include "ExQStandardItemModel.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ExQStandardItemModel w;
w.show();
return a.exec();
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -0,0 +1,16 @@
<RCC>
<qresource prefix="/">
<file>images/Image001.png</file>
<file>images/Image002.png</file>
<file>images/Image003.png</file>
<file>images/Image004.png</file>
<file>images/Image005.png</file>
<file>images/Image006.png</file>
<file>images/Image007.jpg</file>
<file>images/Image008.jpg</file>
<file>images/Image009.jpg</file>
<file>images/Image010.jpg</file>
<file>images/Image011.jpg</file>
<file>images/Image012.png</file>
</qresource>
</RCC>

View File

@ -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