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. Sort function in Qdir
QtWS25 Last Chance

Sort function in Qdir

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 6 Posters 1.1k Views
  • 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.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1

    I set the directory to come first by using the setSorting function.
    I'm getting the path information with QDirIterator, but it doesn't get it in the way I want it to be sorted. Import files in the middle.

    Is there any way to get the directory first?

    void wMethodInfo::UpdateCurrentPathInfo(QString strPath)
    {
    	QDir rootPath(strPath);
    	rootPath.setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs);
    	rootPath.setSorting(QDir::SortFlag::DirsFirst);
    
    	QDirIterator it(rootPath, QDirIterator::FollowSymlinks);
    
    	while (it.hasNext())
    	{
    		it.next();
    		const QFileInfo& fInfo = it.fileInfo();
    
    		//qDebug() << "File Name(): " << it.fileName();
    		//qDebug() << "PopulateFileToSizeHash(): " << it.fileInfo();
    		//qDebug() << "PopulateFileToSizeHash(): " << it.filePath();
    
    
    		
    		//if (fInfo.isFile())
    			//qDebug() << "PopulateFileToSizeHash(): " << it.filePath();
    
    
    		AddItem(fInfo.fileName());		
    	}
    
    }
    
    KroMignonK 1 Reply Last reply
    0
    • I IknowQT

      I set the directory to come first by using the setSorting function.
      I'm getting the path information with QDirIterator, but it doesn't get it in the way I want it to be sorted. Import files in the middle.

      Is there any way to get the directory first?

      void wMethodInfo::UpdateCurrentPathInfo(QString strPath)
      {
      	QDir rootPath(strPath);
      	rootPath.setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs);
      	rootPath.setSorting(QDir::SortFlag::DirsFirst);
      
      	QDirIterator it(rootPath, QDirIterator::FollowSymlinks);
      
      	while (it.hasNext())
      	{
      		it.next();
      		const QFileInfo& fInfo = it.fileInfo();
      
      		//qDebug() << "File Name(): " << it.fileName();
      		//qDebug() << "PopulateFileToSizeHash(): " << it.fileInfo();
      		//qDebug() << "PopulateFileToSizeHash(): " << it.filePath();
      
      
      		
      		//if (fInfo.isFile())
      			//qDebug() << "PopulateFileToSizeHash(): " << it.filePath();
      
      
      		AddItem(fInfo.fileName());		
      	}
      
      }
      
      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #3

      @IknowQT said in Sort function in Qdir:

      I'm getting the path information with QDirIterator, but it doesn't get it in the way I want it to be sorted. Import files in the middle

      As you can see in documentation, QDirIterator is not able to sort (cf. https://doc.qt.io/qt-5/qdiriterator.html#QDirIterator).

      So if sorting is something important for you, than you should better use QDir::entryList() or QDir::entryInfoList()

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      2
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #2

        Why not just call QDir::entryInfoList() twice, once for directories, once for files, then append the second list to the first.

        1 Reply Last reply
        5
        • I IknowQT

          I set the directory to come first by using the setSorting function.
          I'm getting the path information with QDirIterator, but it doesn't get it in the way I want it to be sorted. Import files in the middle.

          Is there any way to get the directory first?

          void wMethodInfo::UpdateCurrentPathInfo(QString strPath)
          {
          	QDir rootPath(strPath);
          	rootPath.setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs);
          	rootPath.setSorting(QDir::SortFlag::DirsFirst);
          
          	QDirIterator it(rootPath, QDirIterator::FollowSymlinks);
          
          	while (it.hasNext())
          	{
          		it.next();
          		const QFileInfo& fInfo = it.fileInfo();
          
          		//qDebug() << "File Name(): " << it.fileName();
          		//qDebug() << "PopulateFileToSizeHash(): " << it.fileInfo();
          		//qDebug() << "PopulateFileToSizeHash(): " << it.filePath();
          
          
          		
          		//if (fInfo.isFile())
          			//qDebug() << "PopulateFileToSizeHash(): " << it.filePath();
          
          
          		AddItem(fInfo.fileName());		
          	}
          
          }
          
          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #3

          @IknowQT said in Sort function in Qdir:

          I'm getting the path information with QDirIterator, but it doesn't get it in the way I want it to be sorted. Import files in the middle

          As you can see in documentation, QDirIterator is not able to sort (cf. https://doc.qt.io/qt-5/qdiriterator.html#QDirIterator).

          So if sorting is something important for you, than you should better use QDir::entryList() or QDir::entryInfoList()

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          2
          • C Offline
            C Offline
            CleoWillemsen
            Banned
            wrote on last edited by
            #4
            This post is deleted!
            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              zalteyitro
              wrote on last edited by JKSH
              #5

              [EDIT: Post moved from other thread --JKSH]

              I have one additional inquiries if it’s not too much trouble πŸ˜“πŸ˜“, guide me about this. These inquiries are displayed underneath here:

              https://forum.qt.io/topic/135772/sort-function-in-qdir

              Sitting tight for most appropriate response straightaway. I’m excessively confounded here 😨😨.

              JonBJ 1 Reply Last reply
              0
              • Z zalteyitro

                [EDIT: Post moved from other thread --JKSH]

                I have one additional inquiries if it’s not too much trouble πŸ˜“πŸ˜“, guide me about this. These inquiries are displayed underneath here:

                https://forum.qt.io/topic/135772/sort-function-in-qdir

                Sitting tight for most appropriate response straightaway. I’m excessively confounded here 😨😨.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JKSH
                #6

                [EDIT: Post moved from other thread --JKSH]

                @zalteyitro
                HI. This thread has no connection to the one you link to. If you want to ask a question in that thread please go there and do so, or open your own thread and reference that one from it.

                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