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. problem in getting folder icon from folder-path via QMime
Forum Updated to NodeBB v4.3 + New Features

problem in getting folder icon from folder-path via QMime

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

    i found a code that gets icon of a file or folder by it's path .
    here is the function .

    QIcon geticon(const QString &filename){
    
        QIcon icon;
        QMimeDatabase mime_database;
    
        QList<QMimeType> mime_types = mime_database.mimeTypesForFileName(filename);
    
        for (int i=0; i < mime_types.count() && icon.isNull(); i++){
            qDebug()<< mime_types[i].iconName();
            icon = QIcon::fromTheme(mime_types[i].iconName());
        }
    
        if (icon.isNull())
          return QApplication::style()->standardIcon(QStyle::SP_FileIcon);
        else
          return icon;
    }
    

    there is some problems in this function and i want to fix that

    1. it dose not detect if the given path is a folder path ,so it dose not return a folder icon.
    2. as i will give it a single file path , i don't want the "QList<QMimeType> mime_types " and the "for loop" in my code.

    i tried different way to fix those but failed.
    please help me.

    jsulmJ 1 Reply Last reply
    0
    • S saber

      i found a code that gets icon of a file or folder by it's path .
      here is the function .

      QIcon geticon(const QString &filename){
      
          QIcon icon;
          QMimeDatabase mime_database;
      
          QList<QMimeType> mime_types = mime_database.mimeTypesForFileName(filename);
      
          for (int i=0; i < mime_types.count() && icon.isNull(); i++){
              qDebug()<< mime_types[i].iconName();
              icon = QIcon::fromTheme(mime_types[i].iconName());
          }
      
          if (icon.isNull())
            return QApplication::style()->standardIcon(QStyle::SP_FileIcon);
          else
            return icon;
      }
      

      there is some problems in this function and i want to fix that

      1. it dose not detect if the given path is a folder path ,so it dose not return a folder icon.
      2. as i will give it a single file path , i don't want the "QList<QMimeType> mime_types " and the "for loop" in my code.

      i tried different way to fix those but failed.
      please help me.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @saber If you don't want mime_types and the loop then use http://doc.qt.io/qt-5/qmimedatabase.html#mimeTypeForFile-1

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        saber
        wrote on last edited by saber
        #3

        @jsulm
        yes i want that mime_types to get it's icon and return the icon .

        but problem is i don't know how to do that.
        i could not declare the "mime_types" to pass the filepath.

        jsulmJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          @saber said in problem in getting folder icon from folder-path via QMime:

          mime_database.mimeTypesForFileName

          From the looks of it, you should rather use QMimeDatabase::mimeTypeForFile.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S saber

            @jsulm
            yes i want that mime_types to get it's icon and return the icon .

            but problem is i don't know how to do that.
            i could not declare the "mime_types" to pass the filepath.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #5

            @saber Please read again what I wrote.
            Using http://doc.qt.io/qt-5/qmimedatabase.html#mimeTypeForFile-1 you don't need mime_types ...

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              saber
              wrote on last edited by
              #6

              here is my solution

              give it the file path it will return the icon.

              QIcon geticon(const QString &filePath) {
              
                  QIcon icon;
                  QMimeDatabase mime;
                  QMimeType mType;
              
                  mType = mime.mimeTypeForFile(filePath);
                  icon = QIcon::fromTheme(mType.iconName());
              
                  if (icon.isNull())
                    return QApplication::style()->standardIcon(QStyle::SP_FileIcon);
                  else
                    return icon;
              }
              
              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