使用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);
}

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

相关推荐
侃侃_天下3 天前
最终的信号类
开发语言·c++·算法
echoarts3 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix3 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题3 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说3 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔4 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号4 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_4 天前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty4 天前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
百锦再4 天前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame