Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Use of Variable or Constant



  • Hi guys,

    I wanted to know use of variables or constants not to directly use "mDir.AllDirs" and "mDir.count()" within qDebug as what is in the code below.

    I mean to put "mDir.AllDirs" and "mDir.count()" in different variables, then I may use them through qDebug.

    @
    QDir mDir("C:/Windows/System32");

    foreach(QFileInfo mitm, mDir.entryList())
    {
        qDebug() << mitm.absoluteFilePath();
    }
    
    qDebug () <<"\nNumber of directories :" <<mDir.AllDirs <<"items";
    
    qDebug () <<"\nNumber of files :" <<mDir.count() <<"items";
    

    @


  • Moderators

    AllDirs is "an enum value in QDir":http://qt-project.org/doc/qt-5/qdir.html#Filter-enum. It is not an access function or anything.

    If you want to display all dirs inside a certain QDir, you need to iterate over QDir::entryList() or QDir::entryInfoList(), combined with a filter. Please see the documentation.



  • thanks.

    if I understand you, you meant there's no other way to do,
    and it always must be like I did in the above code?


  • Moderators

    There are always numerous ways to achieve a goal. I do not really understand what you are trying to do in your code, so I can't answer that question.



  • I wanted a code like this :

    @QMyVariable FirstVariable = mDir.count();

    QMyVariable SecondVariable = mDir.AllDirs;

    QDir mDir("C:/Windows/System32");
    
    foreach(QFileInfo mitm, mDir.entryList())
    {
        qDebug() << mitm.absoluteFilePath();
    }
    
    
    qDebug () <<"\nNumber of directories :" <<FirstVariable <<"items";
    
    qDebug () <<"\nNumber of files :" <<SecondVariable <<"items";
    

    @


  • Moderators

    Mate, posting the same (wrong) code again does not help me understand your problem. So, I will be guessing what you mean.

    You want to print all files and directories in a folder, and then count them separately. Correct? This should work:

    @
    QDir mDir("C:/Windows/System32");
    qDebug() << mDir.entryList(QDir::Dirs | QDir::Files | QDir:NoDotAndDotDot);
    qDebug() << "Number of directories:" << mDir.entryList(QDir::Dirs).count();
    qDebug() << "Number of files:" << mDir.entryList(QDir::Files).count();
    @



  • okay, let me be explicit.

    The first code I posted on my first post is working GREAT, without any problem, you may also test it from your PC if you want; and my concern is not there !

    I might also use it as it's working fine, [ it doesn't a matter ] but I just want to use variable/constant in that code precisely in qDebug while displaying instead of directly using these enum value, like you called them.

    I'm going to reformulate my question :

    In accordance with the code that is on my first post, I need the result of "mDir.AllDirs" & "mDir.count()" to be saved in variables before displaying them through qDebug, and after using these variables through qDebug.


  • Moderators

    @
    QDir::Filter myValue1 = mDir.AllDirs;
    int myValue2 = mDir.count();
    @

    But I want to point it out again: AllDir value is an enum. It will not change to anything else, regardless of what you do to mDir variable. You can use QDir::AllDirs and it will have exactly the same effect, without the need to define an object (mDir).


Log in to reply