• 一个统计多文件单行字符串出现次数QT实现


    只保证能用,细节内存没考虑,清空功能也没调好

    count.pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2020-05-08T01:22:21
    #
    #-------------------------------------------------
    
    QT       += core gui
    
    TARGET = sitool
    TEMPLATE = app
    
    
    SOURCES += main.cpp
            sitool.cpp
    
    HEADERS  += sitool.h
    
    FORMS    += sitool.ui
    
    

    mainwindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="windowModality">
       <enum>Qt::WindowModal</enum>
      </property>
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>631</width>
        <height>565</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <property name="sizePolicy">
        <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
         <horstretch>0</horstretch>
         <verstretch>0</verstretch>
        </sizepolicy>
       </property>
       <layout class="QHBoxLayout" name="horizontalLayout_3">
        <item>
         <layout class="QVBoxLayout" name="verticalLayout">
          <property name="sizeConstraint">
           <enum>QLayout::SetMinAndMaxSize</enum>
          </property>
          <item>
           <layout class="QHBoxLayout" name="horizontalLayout">
            <item>
             <widget class="QLabel" name="dirpath">
              <property name="text">
               <string>查找文件夹:</string>
              </property>
             </widget>
            </item>
            <item>
             <widget class="QLineEdit" name="dirname">
              <property name="text">
               <string/>
              </property>
             </widget>
            </item>
            <item>
             <widget class="QPushButton" name="dirbtn">
              <property name="text">
               <string>打开</string>
              </property>
             </widget>
            </item>
            <item>
             <widget class="QPushButton" name="clearbtn">
              <property name="text">
               <string>清空</string>
              </property>
             </widget>
            </item>
           </layout>
          </item>
          <item>
           <layout class="QHBoxLayout" name="horizontalLayout_2">
            <item>
             <widget class="QListWidget" name="filelist"/>
            </item>
            <item>
             <widget class="QTableView" name="restable"/>
            </item>
           </layout>
          </item>
         </layout>
        </item>
       </layout>
      </widget>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QStringList>
    #include <QStandardItemModel>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    private slots:
        void getDirSlots();
        void clearbtnSlots();
    private:
        Ui::MainWindow *ui;
    
        QStandardItemModel* mmodel;
        QStringList mImgNames;
        uint32_t mCnt;
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFileDialog>
    #include <QDir>
    #include <QDebug>
    #include <map>
    #include <QString>
    #include <QListWidget>
    #include <QListWidgetItem>
    #include <QStandardItemModel>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        setWindowState(Qt::WindowMaximized); //ok
        connect(ui->dirbtn, SIGNAL(clicked()), this, SLOT(getDirSlots()));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::getDirSlots()
    {
        mCnt = 0;
    
        QStringList mImgNames;
        QString mFolderPath;
        if(!ui->dirname->text().isNull())
        {
            mFolderPath  = QFileDialog::getExistingDirectory(this, "Open Folder", ui->dirname->text());
        }
        else
        {
            mFolderPath = QFileDialog::getExistingDirectory(this, "Open Folder");
        }
        if (mFolderPath.isEmpty())
        {
            return;
        }
        ui->dirname->setText(mFolderPath);
    
        QDir dir(mFolderPath);
        mFolderPath = dir.fromNativeSeparators(mFolderPath);//  "\"转为"/"
        if (!dir.exists())
        {
            mImgNames = QStringList("");
        }
    
        dir.setFilter(QDir::Files);
        dir.setSorting(QDir::Name);
        dir.setNameFilters(QString("*.EBK").split(";"));
        mImgNames = dir.entryList();
        for (int i = 0; i < mImgNames.size(); ++i)
        {
            QListWidgetItem*item=new QListWidgetItem(ui->filelist);
            item->setText(mImgNames[i]);
    
            QString temp;
            mImgNames[i] = temp.append(mFolderPath).append("/").append(mImgNames[i]);
    
            ui->filelist->addItem(item);
    
            qDebug() << "entryList: " << i << "-" << mImgNames[i];
        }
    
        typedef std::map<QString, int> MapList;
        MapList map;
        qDebug() <<  mImgNames.size();
        for(int k = 0; k < mImgNames.size(); k++)
        {
            QStringList stocks;
            QFile file(mImgNames[k]);
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            {
                return;
            }
            QTextStream in(&file);
            QString line = in.readLine();
            while(!line.isNull())
            {
                line.remove(QRegExp("\s"));
                qDebug() << line;
                if(map.end() == map.find(line))
                {
                    map[line]= 1;
                }
                else
                {
                    map[line] = map[line]++;
                }
                line = in.readLine();
            }
        }
    
        /* 创建表格视图 */
        QTableView *tableView =  ui->restable;
        tableView->verticalHeader()->hide();
        /* 设置表格视图大小 */
        //tableView->resize(850, 400);
    
        /* 创建数据模型 */
        mmodel = new QStandardItemModel();
        QStandardItemModel* model = mmodel;
    
        /* 设置表格标题行(输入数据为QStringList类型) */
        model->setHorizontalHeaderLabels({"次数", "代码"});
    
        /* 自适应所有列,让它布满空间 */
        tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        int index = 0;
        for(MapList::iterator itr = map.begin(); itr != map.end(); itr++)
        {
            if(itr->second > 1)
            {
                /* 加载第一列(ID)数据 */
                model->setItem(index, 0, new QStandardItem(QString::number(itr->second, 10)));
                /* 加载第二列(User Name)数据 */
                model->setItem(index, 1, new QStandardItem(QString(itr->first.data() + 1)));
                index++;
            }
        }
        /* 设置表格视图数据 */
        tableView->setModel(model);
    }
    
    void MainWindow::clearbtnSlots()
    {
        mmodel->removeRows(0, mmodel->rowCount());
    }
    
    

    main.cpp

    #include "mainwindow.h"
    
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    
    
  • 相关阅读:
    Robot Framework(一)安装笔记
    Postman之获得登录的token,并设置为全局变量
    Postman之简单使用
    Postman之前言
    LoadRunner之使用JSEESIONID访问网站
    Jmeter之逻辑控制器/定时器
    android 解决小米手机上选择照片路径为null情况
    spring MVC学习之二
    Spring MVC学习一
    Struts学习总结(一)
  • 原文地址:https://www.cnblogs.com/kuikuitage/p/13605009.html
Copyright © 2020-2023  润新知