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 16.4k 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.
  • mrjjM mrjj

    @Eddy
    Nice find :)

    EddyE Offline
    EddyE Offline
    Eddy
    wrote on 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

    mrjjM 1 Reply Last reply
    3
    • EddyE Eddy

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

      @Eddy
      Nice find :)

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

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #6

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

      1 Reply Last reply
      2
      • Pradeep P NP Pradeep P N

        Hi All,

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

        Thank You.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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

        Pradeep P NP 1 Reply Last reply
        6
        • EddyE Eddy

          this plugin looks promising

          Pradeep P NP Offline
          Pradeep P NP Offline
          Pradeep P N
          wrote on 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
          • jsulmJ jsulm

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

            Pradeep P NP Offline
            Pradeep P NP Offline
            Pradeep P N
            wrote on 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.

            jsulmJ mrjjM 2 Replies Last reply
            0
            • Pradeep P NP Pradeep P N

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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 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
                • Pradeep P NP Pradeep P N

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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 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.

                    jsulmJ 1 Reply Last reply
                    6
                    • J.HilkJ J.Hilk

                      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();
                      }
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 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
                      2
                      • jsulmJ jsulm

                        @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 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
                        • Pradeep P NP Offline
                          Pradeep P NP Offline
                          Pradeep P N
                          wrote on 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 last edited by
                            #17

                            find and grep...find and grep

                            aha_1980A 1 Reply Last reply
                            0
                            • Kent-DorfmanK Kent-Dorfman

                              find and grep...find and grep

                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on 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 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.

                                Pradeep P NP 1 Reply Last reply
                                1
                                • fcarneyF Offline
                                  fcarneyF Offline
                                  fcarney
                                  wrote on 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
                                  • W wrosecrans
                                    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.

                                    Pradeep P NP Offline
                                    Pradeep P NP Offline
                                    Pradeep P N
                                    wrote on last edited by
                                    #21

                                    @wrosecrans
                                    This don't do the folder search recursive right. ?

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

                                    aha_1980A 1 Reply Last reply
                                    1
                                    • Pradeep P NP Pradeep P N

                                      @wrosecrans
                                      This don't do the folder search recursive right. ?

                                      aha_1980A Offline
                                      aha_1980A Offline
                                      aha_1980
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #22

                                      @Pradeep-P-N

                                      I have not tested this code, but find is recursive so it should work.

                                      Regards

                                      Qt has to stay free or it will die.

                                      1 Reply Last reply
                                      1
                                      • Kent-DorfmanK Offline
                                        Kent-DorfmanK Offline
                                        Kent-Dorfman
                                        wrote on last edited by Kent-Dorfman
                                        #23

                                        yes, the default behaviour of find "in unix" is recursive. grep is a pattern matcher. I use

                                        find . -type f -iname \*.h -o -iname \*.hpp -o \
                                        -iname \*.c -o -iname \*.cc -o -iname \*.cxx \
                                        -o -iname \*.cpp | \
                                        xargs -n 1 egrep -v "^[ \t]*$" | wc -l
                                        

                                        prints total of all target file lines (found recursively). ignores blank lines

                                        Pradeep P NP 1 Reply Last reply
                                        0
                                        • Kent-DorfmanK Kent-Dorfman

                                          yes, the default behaviour of find "in unix" is recursive. grep is a pattern matcher. I use

                                          find . -type f -iname \*.h -o -iname \*.hpp -o \
                                          -iname \*.c -o -iname \*.cc -o -iname \*.cxx \
                                          -o -iname \*.cpp | \
                                          xargs -n 1 egrep -v "^[ \t]*$" | wc -l
                                          

                                          prints total of all target file lines (found recursively). ignores blank lines

                                          Pradeep P NP Offline
                                          Pradeep P NP Offline
                                          Pradeep P N
                                          wrote on last edited by
                                          #24

                                          Hi @Kent-Dorfman
                                          Cool, Linux commands are always awesome.
                                          I just Added *.qml to the command so i can include qml also for the line count.

                                          find . -type f -iname \*.h -o -iname \*.hpp -o -iname \*.qml -o \
                                          -iname \*.c -o -iname \*.cc -o -iname \*.cxx \
                                          -o -iname \*.cpp | \
                                          xargs -n 1 egrep -v "^[ \t]*$" | wc -l
                                          

                                          Thanks a lot again
                                          @mrjj @Eddy @jsulm @J-Hilk @Kent-Dorfman @aha_1980 @wrosecrans .

                                          Have a great day.

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

                                          1 Reply Last reply
                                          5

                                          • Login

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