Results 1 to 13 of 13

Thread: How to use? And where to put QDir?

  1. #1
    Join Date
    Sep 2015
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default How to use? And where to put QDir?

    Hi,

    I am trying to have a total count of the .png file of a folder and its subfolders.

    Seems like QDir is the way to go but I have trouble placing it and using it. Damn am I such a noob (.

    What QDir attributes/properties should I use? Even with the doc and some example I can't wrap my head around it...

    Once I have this count, I want to put it in a variable that I can use/read in my other .qml files...



    If anyone would be so kind as to help me code this...



    Yours,
    --Jay

  2. #2
    Join Date
    Sep 2015
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use? And where to put QDir?

    You have to calculate the total count of .png files in some directory structure....First You can use function void QDir::setNameFilters(const QStringList) for setting the filters in the directory struture.eg.
    QStringList filters;
    filters << "*.png";
    dir.setNameFilters(filters);
    Then you can use function QStringList QDir::entryList(const QStringList &filters) which will return the StringList for all .png files. Then you should use function uint QDir:: count() which is equivalent to entryList().count()...Try it..and you can also check these functions on Qt Help for the details.

  3. The following user says thank you to bartarya.parul for this useful post:

    JaySDC (21st September 2015)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use? And where to put QDir?

    There is no direct way to use this in QML, you will have to go through C++.

    There a re different ways of doing this, some more complex (but also more flexible) then others.

    Do you have any object already exposed from C++ via setContextProperty()?

    Cheers,
    _

  5. #4
    Join Date
    Sep 2015
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to use? And where to put QDir?

    Yes anda_skoa I gathered from research that I'd have to go with C++ and QDir.

    Thank you bartarya.parul I'll see what I can do with that : )

    Yours,
    --Jay

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use? And where to put QDir?

    Since you want to do recursive, you will be better of using QDirIterator with the respective flag.

    If you already have an object that is exposed to QML via setContextProperty() then the easiest way is to add a Q_INVOKABLE marked method to that class.

    Out of curiosity: how do you intend to use this count?

    Cheers,
    _

  7. #6
    Join Date
    Sep 2015
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to use? And where to put QDir?

    Here is my project:

    I (try to) develop a FrontEnd to run on my arcade cabinet.

    Qt is a nice way to display the images of the different games in a caroussel-way.


    I have several counters on my FrontEnd design that are animated.
    Each game have a unique property that is either HORIZONTAL or VERTICAL.

    I have one counter (A) that display the # of HORIZONTAL games in the CURRENT category (working : D)
    I have one counter (B) that display the # of VERTICAL games in the CURRENT category (working : D)

    I have one counter (C) that display the # of HORIZONTAL games in TOTAL (non-working yet )
    I have one counter (D) that display the # of VERTICAL games in TOTAL (non-working yet )

    Right now my code only knows how many games there are WHEN switching to a category.
    Then it displays the number of items in that category.


    But as we only know the # in the current category, I also want to have an additional check/count when the application is INITIALLY ran.
    This number will only be checked once. I don't even need to change that variable afterwards. So C & D should always display the same, but I still need to know C and D values : /.

    Yours,
    --Jay

  8. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use? And where to put QDir?

    Ah, see, that makes it even simplier

    While you still need to gather that information in C++, you can now just export the final values via setContextProperty().

    At some place in your C++ code you are loading the main QML file, probably using a QQuickWindow or a QQuickView.
    Both have a "root context" which basically holds "global variables".
    You can add values to that by calling setContextProperty(), providing a name (to be used in QML) and the value (in your case the two numbers you've just gathered).

    Something like this:
    Qt Code:
    1. int totalHorizontalGames = ....;
    2.  
    3. QQuickView view;
    4. view.rootContext()->setContextProperty("_totalHorizontalGamesCount", totalHorizontalGames);
    5. view.setSource(...);
    To copy to clipboard, switch view to plain text mode 
    and anywhere in QML
    Qt Code:
    1. Text {
    2. text: "Total number of Horizontal games: " + _totalHorizontalGamesCount
    3. }
    To copy to clipboard, switch view to plain text mode 


    Cheers,
    _

  9. #8
    Join Date
    Sep 2015
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to use? And where to put QDir?

    Will give it a go.

    Thanks again : )

    Yours,
    --Jay

  10. #9
    Join Date
    Sep 2015
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to use? And where to put QDir?

    Hey everyone,

    The code almost works :P

    Right now I succeeded in (obv. with both your help):

    - Count the number of png files in any folder I choose
    - Stock that variable
    - Pass that variable to my qml component to use it
    - Use it in qml



    There is only one thing missing, I'd like to include subfolders relative to the path I gave initially, and also a filter for the beginning of the file name.

    **short version**
    Basically I want to browse 9 subfolders in order to count:
    All the files HORI_*.png of these 9 subfolders
    All the files VERTI_*.png of these 9 subfolders
    **/end short version**




    **detailed version**
    a)
    Each category is in a different folder, all of them are located here -> c:/temp/

    b)
    I have 9 of them -> c:/temp/category1, c:/temp/category2, c:/temp/category3, ...

    c)
    So I want the code to scan these 9 subfolders for all the .png
    Right now it only scans the current path that I give it. It does not check the subfolders.

    d)
    Finally I want a last filter depending on the START of the name of the .png file:
    it either starts with HORI_ *or* VERTI_

    So I am pretty sure there is some filter I could use to achieve that, but can't find which : /
    what code should I add as a filter?
    **/end detailed version**


    Qt Code:
    1. #pragma once
    2. #include <QDir>
    3. #include <QObject>
    4.  
    5. class FolderInfo : public QObject {
    6. Q_OBJECT
    7. Q_PROPERTY(int numFiles READ GetNumFiles)
    8. Q_PROPERTY(QString currentPath READ GetCurrentPath)
    9. QStringList filters;
    10.  
    11. public:
    12. FolderInfo() {
    13. SetFromPath("c:/temp/");
    14. }
    15.  
    16. Q_INVOKABLE void SetFromPath(const QString& path) {
    17. QDir dir(path);
    18. filters<<"*.png";
    19. m_currentPath = dir.absolutePath();
    20. dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
    21. dir.setNameFilters(filters);
    22. m_numFiles = dir.count();
    23. }
    24.  
    25. int GetNumFiles() const {
    26. return m_numFiles;
    27. }
    28.  
    29.  
    30. QString GetCurrentPath() const {
    31. return m_currentPath;
    32. }
    33.  
    34. private:
    35. int m_numFiles;
    36. QString m_currentPath;
    37.  
    38. };
    To copy to clipboard, switch view to plain text mode 



    Yours,
    --Jay

  11. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use? And where to put QDir?

    Quote Originally Posted by JaySDC View Post
    So I want the code to scan these 9 subfolders for all the .png
    Right now it only scans the current path that I give it. It does not check the subfolders.
    QDirIterator with the iterator flag for subdirectories.

    Quote Originally Posted by JaySDC View Post
    Finally I want a last filter depending on the START of the name of the .png file:
    it either starts with HORI_ *or* VERTI_
    Just include the prefix in the name filters (two entries, one for each pattern).

    Cheers,
    _

  12. #11
    Join Date
    Sep 2015
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to use? And where to put QDir?

    Wow the HORI_/VERTI_ filter was as simple as that...
    I did not even think about trying that haha.

    As for the subfolders, I'll see what I can do, thank you!

    --Jay


    Added after 26 minutes:


    Darn, I am such a noob, because of my lack of knowledge and experience, I can't even use doc to add the QDirIterator part .

    I tried to add it here:
    dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDirIterator::Subdirectories);

    Also some other places. But in the end a piece of the puzzle is missing.


    Would you be so kind as to be more specific with QDirIterator usage?

    Qt Code:
    1. #pragma once
    2. #include <QDir>
    3. #include <QObject>
    4.  
    5. class FolderInfo : public QObject {
    6. Q_OBJECT
    7. Q_PROPERTY(int numFiles READ GetNumFiles)
    8. Q_PROPERTY(QString currentPath READ GetCurrentPath)
    9. QStringList filters;
    10.  
    11. public:
    12. FolderInfo() {
    13. SetFromPath("c:/temp/Bat/YOKO_SP_");
    14. }
    15.  
    16. Q_INVOKABLE void SetFromPath(const QString& path) {
    17. QDir dir(path);
    18.  
    19. filters<<"YOKO_*.png";
    20. m_currentPath = dir.absolutePath();
    21. dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
    22. dir.setNameFilters(filters);
    23. m_numFiles = dir.count();
    24. }
    25.  
    26. int GetNumFiles() const {
    27. return m_numFiles;
    28. }
    29.  
    30.  
    31.  
    32. QString GetCurrentPath() const {
    33. return m_currentPath;
    34. }
    35.  
    36. private:
    37. int m_numFiles;
    38. QString m_currentPath;
    39.  
    40. };
    To copy to clipboard, switch view to plain text mode 


    Yours,
    --Jay
    Last edited by JaySDC; 17th September 2015 at 11:54.

  13. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use? And where to put QDir?

    You will be using QDirIterator instead of the QDir.

    Create the QDirIterator and pass in the path, filters and the recursion flag to the constructor.
    Then you iterate like in the docs, but instead of doing anything with the return value of next(), you simply increment m_numFiles (which you reset to 0 before starting the loop).

    Cheers,
    _

  14. The following user says thank you to anda_skoa for this useful post:

    JaySDC (21st September 2015)

  15. #13
    Join Date
    Sep 2015
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to use? And where to put QDir?

    Working. Cheers : D.

    --Jay

Similar Threads

  1. QDir
    By smemamian in forum Newbie
    Replies: 3
    Last Post: 2nd April 2013, 07:55
  2. QListView and QDir
    By Yayati.Ekbote in forum Qt Programming
    Replies: 5
    Last Post: 26th March 2010, 11:45
  3. QDir::mkpath
    By drescherjm in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2009, 04:35
  4. qdir and entrylist
    By mattia in forum Newbie
    Replies: 1
    Last Post: 28th November 2007, 10:13
  5. Is it me or QDir?
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2007, 23:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.