Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Line Count in my Qt Project.
Forum Updated to NodeBB v4.3 + New Features

Line Count in my Qt Project.

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 10 Posters 15.9k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pradeep P N
    wrote on 18 Jun 2017, 15:39 last edited by
    #1

    Hi All,

    Is there any way to find out the total number lines count in my Qt Project.

    Thank You.

    Pradeep Nimbalkar.
    Upvote the answer(s) that helped you to solve the issue...
    Keep code clean.

    J 1 Reply Last reply 19 Jun 2017, 06:10
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 18 Jun 2017, 15:45 last edited by
      #2

      Hi
      You mean lines in all cpp and h files?

      I dont think Creator can show you.

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Eddy
        wrote on 18 Jun 2017, 16:07 last edited by
        #3

        this plugin looks promising

        Qt Certified Specialist
        www.edalsolutions.be

        M P 2 Replies Last reply 18 Jun 2017, 16:12
        5
        • E Eddy
          18 Jun 2017, 16:07

          this plugin looks promising

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 18 Jun 2017, 16:12 last edited by
          #4

          @Eddy
          Nice find :)

          E 1 Reply Last reply 18 Jun 2017, 16:57
          2
          • M mrjj
            18 Jun 2017, 16:12

            @Eddy
            Nice find :)

            E Offline
            E Offline
            Eddy
            wrote on 18 Jun 2017, 16:57 last edited by
            #5

            @mrjj said in Line Count in my Qt Project.:

            @Eddy
            Nice find :)

            I may have no glasses, but I can google ;-)

            Qt Certified Specialist
            www.edalsolutions.be

            M 1 Reply Last reply 18 Jun 2017, 18:07
            3
            • E Eddy
              18 Jun 2017, 16:57

              @mrjj said in Line Count in my Qt Project.:

              @Eddy
              Nice find :)

              I may have no glasses, but I can google ;-)

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 18 Jun 2017, 18:07 last edited by
              #6

              @Eddy
              Its also very good example of Creator plugin \o/

              1 Reply Last reply
              2
              • P Pradeep P N
                18 Jun 2017, 15:39

                Hi All,

                Is there any way to find out the total number lines count in my Qt Project.

                Thank You.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 19 Jun 2017, 06:10 last edited by
                #7

                @Pradeep-P-N There is well known command line tool for that: sloccount

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                P 1 Reply Last reply 20 Jun 2017, 02:13
                6
                • E Eddy
                  18 Jun 2017, 16:07

                  this plugin looks promising

                  P Offline
                  P Offline
                  Pradeep P N
                  wrote on 20 Jun 2017, 02:11 last edited by
                  #8

                  Hi @Eddy i did try it but i couldn't get the solution.
                  Hi @mrjj Yes i want to count all the lines in CPP and H and also in my QML files

                  Pradeep Nimbalkar.
                  Upvote the answer(s) that helped you to solve the issue...
                  Keep code clean.

                  1 Reply Last reply
                  1
                  • J jsulm
                    19 Jun 2017, 06:10

                    @Pradeep-P-N There is well known command line tool for that: sloccount

                    P Offline
                    P Offline
                    Pradeep P N
                    wrote on 20 Jun 2017, 02:13 last edited by Pradeep P N
                    #9

                    @jsulm Can you please give me more details on sloccount and how can i use it with Qt

                    Pradeep Nimbalkar.
                    Upvote the answer(s) that helped you to solve the issue...
                    Keep code clean.

                    J M 2 Replies Last reply 20 Jun 2017, 04:16
                    0
                    • P Pradeep P N
                      20 Jun 2017, 02:13

                      @jsulm Can you please give me more details on sloccount and how can i use it with Qt

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 20 Jun 2017, 04:16 last edited by
                      #10

                      @Pradeep-P-N Why not just use Google?
                      Then you will find https://www.dwheeler.com/sloccount/
                      "how can i use it with Qt" - you don't use it with Qt (as Qt is not a programming language), you use it with your C++ source code. It does not support QML though.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on 20 Jun 2017, 04:28 last edited by
                        #11

                        This is as good of a reason as it gets to write a small program, that does the work for you.

                        Recursively opening your source file folder and counting how often QIODevice::readline can be called.

                        Shouldn't take more than a few minutes to do.


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        1
                        • P Pradeep P N
                          20 Jun 2017, 02:13

                          @jsulm Can you please give me more details on sloccount and how can i use it with Qt

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 20 Jun 2017, 05:08 last edited by
                          #12

                          @Pradeep-P-N

                          sloccount topmost-source-code-directory
                          so like
                          sloccount c:\projects\testproject

                          https://www.dwheeler.com/sloccount/sloccount.html

                          1 Reply Last reply
                          1
                          • J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on 20 Jun 2017, 06:04 last edited by
                            #13

                            I had some time during breakfast :-)

                            #include <QCoreApplication>
                            #include <QDir>
                            #include <QFile>
                            #include <QTextStream>
                            #include <QVector>
                            
                            int countLines(QString path){
                                QFile f(path);
                                int cnt = 0;
                            
                                if(f.open(QIODevice::ReadOnly | QIODevice::Text)){
                                    QTextStream read(&f);
                                    while(!read.atEnd()){
                                        read.readLine();
                                        cnt++;
                                    }
                                }
                                f.close();
                                return cnt;
                            }
                            
                            int parseDir(QString path){
                                int cnt = 0;
                                QDir dir(path);
                                QStringList dirs = dir.entryList(QDir::AllDirs |QDir::NoDotAndDotDot);
                                QStringList file = dir.entryList(QDir::Files);
                                for(QString dir : dirs){
                                        cnt += parseDir(path + "/"+dir);
                                }
                            
                                for(QString s : file){
                                    if(s.splitRef('.').last() == "h" || s.splitRef('.').last() == "cpp")
                                        cnt += countLines(path + "/"+s);
                                }
                            
                                return cnt;
                            }
                            
                            int main(int argc, char *argv[])
                            {
                                QCoreApplication a(argc, argv);
                            
                                int count = 0;
                            
                                count += PathDir(a.arguments().last());
                            
                                QTextStream out(stdout);
                                out << "Lines in project: " << a.arguments().last() << ": "<< endl <<count << endl;
                            
                                return a.exec();
                            }
                            

                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            J 1 Reply Last reply 20 Jun 2017, 07:39
                            6
                            • J.HilkJ J.Hilk
                              20 Jun 2017, 06:04

                              I had some time during breakfast :-)

                              #include <QCoreApplication>
                              #include <QDir>
                              #include <QFile>
                              #include <QTextStream>
                              #include <QVector>
                              
                              int countLines(QString path){
                                  QFile f(path);
                                  int cnt = 0;
                              
                                  if(f.open(QIODevice::ReadOnly | QIODevice::Text)){
                                      QTextStream read(&f);
                                      while(!read.atEnd()){
                                          read.readLine();
                                          cnt++;
                                      }
                                  }
                                  f.close();
                                  return cnt;
                              }
                              
                              int parseDir(QString path){
                                  int cnt = 0;
                                  QDir dir(path);
                                  QStringList dirs = dir.entryList(QDir::AllDirs |QDir::NoDotAndDotDot);
                                  QStringList file = dir.entryList(QDir::Files);
                                  for(QString dir : dirs){
                                          cnt += parseDir(path + "/"+dir);
                                  }
                              
                                  for(QString s : file){
                                      if(s.splitRef('.').last() == "h" || s.splitRef('.').last() == "cpp")
                                          cnt += countLines(path + "/"+s);
                                  }
                              
                                  return cnt;
                              }
                              
                              int main(int argc, char *argv[])
                              {
                                  QCoreApplication a(argc, argv);
                              
                                  int count = 0;
                              
                                  count += PathDir(a.arguments().last());
                              
                                  QTextStream out(stdout);
                                  out << "Lines in project: " << a.arguments().last() << ": "<< endl <<count << endl;
                              
                                  return a.exec();
                              }
                              
                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 20 Jun 2017, 07:39 last edited by
                              #14

                              @J.Hilk Well, it is a bit more complex :-) For example your version counts empty lines and comment lines.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              J.HilkJ 1 Reply Last reply 20 Jun 2017, 08:22
                              2
                              • J jsulm
                                20 Jun 2017, 07:39

                                @J.Hilk Well, it is a bit more complex :-) For example your version counts empty lines and comment lines.

                                J.HilkJ Offline
                                J.HilkJ Offline
                                J.Hilk
                                Moderators
                                wrote on 20 Jun 2017, 08:22 last edited by
                                #15

                                @jsulm well obviously :-P
                                but that could be addressed e.g:

                                int countLines(QString path){
                                    QFile f(path);
                                    int cnt = 0;
                                
                                    if(f.open(QIODevice::ReadOnly | QIODevice::Text)){
                                        QTextStream read(&f);
                                        QString line;
                                        bool comment = false;
                                        while(!read.atEnd()){
                                            line = read.readLine();
                                            line = line.simplified();
                                            line.replace(" ","");
                                            if(line.size() >0){
                                                if(line.leftRef(2) != "//"){
                                                    if(line.contains("/*"))
                                                        comment = true;
                                                    if(line.contains("*/"))
                                                        comment = false;
                                                    if(!comment)
                                                        cnt++;
                                                }
                                            }
                                        }
                                    }
                                    f.close();
                                    return cnt;
                                }
                                

                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                1 Reply Last reply
                                2
                                • P Offline
                                  P Offline
                                  Pradeep P N
                                  wrote on 4 Jun 2019, 15:36 last edited by
                                  #16

                                  Thanks All for the help.
                                  Moving to solved.

                                  Pradeep Nimbalkar.
                                  Upvote the answer(s) that helped you to solve the issue...
                                  Keep code clean.

                                  1 Reply Last reply
                                  2
                                  • Kent-DorfmanK Offline
                                    Kent-DorfmanK Offline
                                    Kent-Dorfman
                                    wrote on 4 Jun 2019, 17:04 last edited by
                                    #17

                                    find and grep...find and grep

                                    aha_1980A 1 Reply Last reply 4 Jun 2019, 19:24
                                    0
                                    • Kent-DorfmanK Kent-Dorfman
                                      4 Jun 2019, 17:04

                                      find and grep...find and grep

                                      aha_1980A Offline
                                      aha_1980A Offline
                                      aha_1980
                                      Lifetime Qt Champion
                                      wrote on 4 Jun 2019, 19:24 last edited by
                                      #18

                                      @Kent-Dorfman would you like to elaborate a bit more?

                                      Qt has to stay free or it will die.

                                      1 Reply Last reply
                                      2
                                      • W Offline
                                        W Offline
                                        wrosecrans
                                        wrote on 4 Jun 2019, 20:11 last edited by
                                        #19
                                        find . -iname \*.h -o -iname \*.cpp -exec grep \\\; {} \; | wc -l
                                        

                                        Would find all the .h and .cpp find in the current directory, and count the number of lines with a semicolon. For the most things, that's probably a pretty useful count of code LOC, and requires no extra tools to be installed.

                                        P 1 Reply Last reply 5 Jun 2019, 02:44
                                        1
                                        • fcarneyF Offline
                                          fcarneyF Offline
                                          fcarney
                                          wrote on 4 Jun 2019, 20:15 last edited by
                                          #20

                                          Is this for some kind of line-counting match?

                                          C++ is a perfectly valid school of magic.

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved