cpp
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QModelIndex>
#include <QTextCodec>
#include <QRegularExpression>
#include <QStandardItemModel>
#include <QItemSelectionModel>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
void initModelData(QStringList & aDataList);
private slots:
void do_currentChange(const QModelIndex & current,const QModelIndex & privous);
void on_openAction_triggered();
private:
QLabel * m_labelCurrFile;
QLabel * m_labelPos;
QLabel * m_labelCellText;
const int FixedColumnCount = 6;
QStandardItemModel * m_model;
QItemSelectionModel * m_selectModel;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setCentralWidget(ui->splitter);
m_labelCurrFile = new QLabel("当前文件: " ,this);
m_labelCurrFile->setMinimumWidth(200);
m_labelPos = new QLabel(" 当前单元格: ",this);
m_labelPos->setMinimumWidth(200);
m_labelCellText = new QLabel(" 单元格内容: ",this);
m_labelCellText->setMinimumWidth(200);
ui->statusbar->addWidget(m_labelCurrFile);
ui->statusbar->addWidget(m_labelPos);
ui->statusbar->addWidget(m_labelCellText);
m_model = new QStandardItemModel(2,FixedColumnCount,this);
m_selectModel = new QItemSelectionModel(m_model,this);
ui->tableView->setModel(m_model);
ui->tableView->setSelectionModel(m_selectModel);
ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
connect(m_selectModel,&QItemSelectionModel::currentChanged,this,&MainWindow::do_currentChange);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initModelData(QStringList &aDataList)
{
int rowCount = aDataList.size();//获取文件行数
m_model->setRowCount(rowCount -1);//第一行是标题
QString header = aDataList.at(0); //获取文件的标题
QStringList headList = header.split(QRegularExpression(R"(\s+)"),QString::SkipEmptyParts);//把一行数据,用空格隔开
m_model->setHorizontalHeaderLabels(headList);//设置表头
//显示文件中用空格隔开的字段信息,最后一列中插入复选框
QStandardItem *aItem;
int j = 0;
for(int i = 1; i<rowCount; ++i){
QString aLineText = aDataList.at(i);//获取一行数据
QStringList tempList = aLineText.split(QRegularExpression(R"(\s+)"),QString::SkipEmptyParts);//把一行数据,用空格隔开
for(j=0; j<FixedColumnCount; j++){
aItem = new QStandardItem(tempList.at(j));
m_model->setItem(i-1,j,aItem);
}
aItem = new QStandardItem(" ");
aItem->setCheckable(true); //为最后一列添加复选框
//判断最后一列,如果是"10",设置不选中复选框;否则选中
if(tempList.at((j-1)) == "10"){
aItem->setText("是");
aItem->setBackground(QBrush(Qt::red));//把最后一列背景调为红色
aItem->setCheckState(Qt::Unchecked);
}else{
aItem->setText("否");
aItem->setCheckState(Qt::Checked);
aItem->setBackground(QBrush(Qt::green));//把最后一列背景调为红色
}
m_model->setItem(i-1,j,aItem);
}
}
void MainWindow::do_currentChange(const QModelIndex ¤t, const QModelIndex &privous)
{
if(current.isValid()){
m_labelPos->setText(QString::asprintf(" 当前单元格:%d行,%d列",current.row(),current.column()));
QStandardItem * aItem = m_model->itemFromIndex(current);
m_labelCellText->setText(" 单元格内容: "+aItem->text());
ui->SetAction->setChecked(aItem->font().bold());
}
}
void MainWindow::on_openAction_triggered()
{
QString currPath = QCoreApplication::applicationDirPath();
QString aFileName = QFileDialog::getOpenFileName(this,"打开文件",currPath,"数据文件(*.txt);;二进制文件(*.dat)");//筛选打开的文本格式
if(aFileName.isEmpty()){
return;
}
QFile afile(aFileName);
if(!afile.open(QIODevice::ReadOnly|QIODevice::Text)){
return;
}
//以文本流的方式,读取每一行数据,并刷新到文本框中
QStringList aFileCount;
ui->textBrowser->clear();
QTextStream aStream(&afile);
aStream.setCodec("UTF-8");//防止中文乱码
//将读取每一行的数据,放在列表中,并在组件中显示
while (!aStream.atEnd()) {
QString aLine = aStream.readLine();
ui->textBrowser->append(aLine);
aFileCount.append(aLine);
}
afile.close();
m_labelCurrFile->setText(" 当前文件: "+aFileName);
initModelData(aFileCount);
}
下载源码地址:添加链接描述