使用QTableView显示本地字段信息

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 &current, 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);
}

下载源码地址:添加链接描述

相关推荐
benchmark_cc3 小时前
如何用 Python + QuantDash 快速构建高胜率“配对交易(Pairs Trading)”策略?
开发语言·人工智能·python·pandas·量化交易·quantdash
程序员无隅5 小时前
Coding Agent 为什么压缩上下文后还能继续工作?上下文模块设计拆解
java·开发语言·数据库
Python+995 小时前
Java 枚举类(Enum)详解:从基础到高级应用
java·开发语言·python
二炮手亮子5 小时前
浅记java线程池
java·开发语言
zmzb01036 小时前
C++课后习题训练记录Day157
开发语言·c++
dunge20266 小时前
2026年7月最新ChatGPT Plus / Pro 与 Codex:当 AI Agent 最新5.6版本来袭,必须理解事务、幂等与补偿
开发语言·人工智能·python
爱吃牛肉的大老虎7 小时前
rust基础之环境搭建
java·开发语言·rust
openKylin7 小时前
与全球技术演进同频,openKylin 3.0从C迈向Rust
c语言·开发语言·rust·开源·开放原子·openkylin
疯狂打码的少年8 小时前
【软件工程】结构化设计:模块独立性与耦合内聚
java·开发语言·笔记·软件工程
程高兴8 小时前
PMSM基于在线转动惯量辩识的滑模负载转矩观测器MATLAB-SIMULINK仿真模型
开发语言·matlab