Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Code Statistics component in Qt

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces Qt how to achieve code statistics components, the article introduces in great detail, has a certain reference value, interested friends must read it!

I. Preface

The number of lines of code statistics is mainly used to count the number of lines of code of all files in the project, including blank lines, comment lines, and lines of code. You can specify a filter extension, such as files that only want to count .cpp, or you can specify files or directories for statistics. The first thing I did when I finished writing this tool was to count the number of lines of code for the largest project I had ever written, to see if it was a legendary line of code for one yuan. This largest project started in 2010 and has been almost 10 years since now. It is the largest project I have ever written in the company. I have been upgrading and updating it all the time. I have reconstructed twice along the way and made major structural changes. According to the statistics, there seem to be about 15W lines of code, the pure code is about 10W, and the rest are blank lines and comment lines. I really startled myself, and it was still a medium-sized project, and then I counted all the code of the custom control. Oh, my God. 23W lines of total code and 17W lines of pure code. Oh, I'll go!

Main functions:

You can count code lines / blank lines / comment lines respectively

Support for specifying filter extension

Support to specify files or directories for statistics

Display the statistical results step by step without sticking to the main interface

Show the size of each counted file / total number of lines / lines of code, etc.

2. Code ideas void frmCountCode::countCode (const QString & filePath) {QDir dir (filePath); foreach (QFileInfo fileInfo, dir.entryInfoList ()) {if (fileInfo.isFile ()) {QString strFileName = fileInfo.fileName (); if (checkFile (strFileName)) {listFile tableWidget- > setRowCount (count); quint32 totalLines = 0; quint32 totalBytes = 0; quint32 totalCodes = 0; quint32 totalNotes = 0 Quint32 totalBlanks = 0; for (int I = 0; I

< count; i++) { QFileInfo fileInfo(files.at(i)); countCode(fileInfo.filePath(), lineCode, lineBlank, lineNotes); int lineAll = lineCode + lineBlank + lineNotes; QTableWidgetItem *itemName = new QTableWidgetItem; itemName->

SetText (fileInfo.fileName ()); QTableWidgetItem * itemSuffix = new QTableWidgetItem; itemSuffix- > setText (fileInfo.suffix ()); QTableWidgetItem * itemSize = new QTableWidgetItem; itemSize- > setText (QString::number (fileInfo.size (); QTableWidgetItem * itemLine = new QTableWidgetItem; itemLine- > setText (QString::number (lineAll)); QTableWidgetItem * itemCode = new QTableWidgetItem; itemCode- > setText (QString::number (lineCode)) QTableWidgetItem * itemNote = new QTableWidgetItem; itemNote- > setText (QString::number (lineNotes)); QTableWidgetItem * itemBlank = new QTableWidgetItem; itemBlank- > setText (QString::number (lineBlank)); QTableWidgetItem * itemPath = new QTableWidgetItem; itemPath- > setText (fileInfo.filePath ()); itemSuffix- > setTextAlignment (Qt::AlignCenter); itemSize- > setTextAlignment (Qt::AlignCenter); itemLine- > setTextAlignment (Qt::AlignCenter) ItemCode- > setTextAlignment (Qt::AlignCenter); itemNote- > setTextAlignment (Qt::AlignCenter); itemBlank- > setTextAlignment (Qt::AlignCenter); ui- > tableWidget- > setItem (I, 0, itemName); ui- > tableWidget- > setItem (I, 1, itemSuffix); ui- > tableWidget- > setItem (I, 2, itemSize); ui- > tableWidget- > setItem (I, 3, itemLine); ui- > tableWidget- > setItem (I, 4, itemCode) Ui- > tableWidget- > setItem (I, 5, itemNote); ui- > tableWidget- > setItem (I, 6, itemBlank); ui- > tableWidget- > setItem (I, 7, itemPath); totalBytes + = fileInfo.size (); totalLines + = lineAll; totalCodes + = lineNotes; totalBlanks + = lineBlank; if (I% 100 = 0) {qApp- > processEvents () }} / / display statistical results listFile.clear (); ui- > txtCount- > setText (QString::number (count)); ui- > txtSize- > setText (QString::number (totalBytes)); ui- > txtRow- > setText (QString::number (totalLines)); ui- > txtCode- > setText (QString::number (totalCodes)); ui- > txtNote- > setText (QString::number (totalNotes); ui- > txtBlank- > setText (QString::number (totalBlanks)) / / calculated percentage double percent = 0; / / percentage of lines of code percent = ((double) totalCodes / totalLines) * 100; ui- > labPercentCode- > setText (QString ("1%") .Arg (percent, 5, 'fags, 2, QChar (')); / / percentage of comment lines percent = ((double) totalNotes / totalLines) * 100 Ui- > labPercentNote- > setText (QString ("1%") .arg (percent, 5, 'fallow, 2, QChar (')); / / percentage of blank lines percent = ((double) totalBlanks / totalLines) * 100; ui- > labPercentBlank- > setText (QString ("1%") .Arg (percent, 5, 'fallow, 2, QChar (') } void frmCountCode::countCode (const QString & fileName, int & lineCode, int & lineBlank, int & lineNotes) {lineCode = lineBlank = lineNotes = 0; QFile file (fileName); if (file.open (QFile::ReadOnly)) {QTextStream out (& file); QString line; bool isNote = false; while (! out.atEnd ()) {line = out.readLine () / / remove the preceding blank line if (line.startsWith (")) {line.remove (");} / / determine whether the current line is comment if (line.startsWith ("/ *")) {isNote = true } / / comment part if (isNote) {lineNotes++;} else {if (line.startsWith ("/ /")) {/ / comment line lineNotes++;} else if (line.isEmpty ()) {/ / blank line lineBlank++ } else {/ / Line of Code lineCode++;} / / end of comment if (line.endsWith ("* /")) {isNote = false;} 3. Effect diagram

These are all the contents of the article "how to implement Code Statistics components in Qt". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report