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. Get size of icon constructed via QIcon(QString &)
Forum Updated to NodeBB v4.3 + New Features

Get size of icon constructed via QIcon(QString &)

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 12.1k 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.
  • M Offline
    M Offline
    mcosta
    wrote on last edited by
    #2

    Hi,

    a QIcon is a object that store many Pixmap depending the mode, size and state.
    For example you could use different "unscaled" pixmap for Size; so there isn't single Image.

    Regards

    Once your problem is solved don't forget to:

    • Mark the thread as SOLVED using the Topic Tool menu
    • Vote up the answer(s) that helped you to solve the issue

    You can embed images using (http://imgur.com/) or (http://postimage.org/)

    1 Reply Last reply
    0
    • T Offline
      T Offline
      toralf
      wrote on last edited by
      #3

      [quote author="mcosta" date="1363959373"]Hi,

      a QIcon is a object that store many Pixmap depending the mode, size and state.
      For example you could use different "unscaled" pixmap for Size; so there isn't single Image.

      Regards[/quote]
      Maybe I wasn't very clear in my orignal post, but the question wasn't supposed to be how to get one single image size, but rather, how to find any size at all with certain setups.

      Yes, there may be different pixmaps depending on mode, size and state. If the icon is created via the theme engine, I can get size information on each and every one of those via availableSizes(). However, if I create an icon from a single PNG file, this method simply doesn't return anything - although surely, there is at least one well-known "unscaled" size - i.e. the dimensions of the input image.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mcosta
        wrote on last edited by
        #4

        Sorry,

        can you post your code?

        I tried this

        @void Widget::on_loadImage_clicked()
        {
        QString fileName = QFileDialog::getOpenFileName(this,
        tr ("open Image"),
        ".",
        tr ("PNG Files (*.png)"));

        if (!fileName.isEmpty()) {
        QIcon icon (fileName);

        qDebug() << "Available sizes for" << fileName << ":" << icon.availableSizes();
        

        }
        }
        @

        and it works fine

        OUTPUT
        @Available sizes for "C:/Users/xxxxxx/Pictures/pdc.png" : (QSize(542, 306) )@

        I'm working with Qt5.0.1 on Windows7

        Regards

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        1 Reply Last reply
        0
        • T Offline
          T Offline
          toralf
          wrote on last edited by
          #5

          I'm using Qt 4.8 under Linux. Simplified version of the actual code used, wrapped up as a stand-alone application:

          @#include <QApplication>
          #include <QIcon>
          #include <QPushButton>

          static void checkIcon(const QIcon &i1)
          {
          qDebug("Size lists for icon "%s":", qPrintable(i1.name()));

          for(int modeNo=0; modeNo<=QIcon::Selected; modeNo++) {
          QIcon::Mode mode=(QIcon::Mode )modeNo;

          for(int stateNo=0; stateNo<=QIcon::On; stateNo++) {
            QIcon::State state=(QIcon::State )stateNo;
          
            QList<QSize> sizes=i1.availableSizes(mode, state);
          
            qDebug("%d sizes for %d %d", sizes.size(), mode, state);
          }
          

          }
          }

          int main(int argc, char **argv)
          {
          QApplication app(argc, argv);
          QIcon fileIcon("/usr/share/icons/gnome/32x32/actions/document-open.png");
          QIcon themeIcon(QIcon::fromTheme("document-open"));
          QPushButton b1(fileIcon, "file"), b2(themeIcon, "theme");

          checkIcon(fileIcon);
          checkIcon(themeIcon);

          b1.show();
          b2.show();

          app.exec();

          return 0;
          }
          @
          Notice how a "themed" icon is also included for comparison purposes. The buttons are just a way to verify that the icons are valid. Output:
          @Size lists for icon "":
          0 sizes for 0 0
          0 sizes for 1 0
          0 sizes for 2 0
          0 sizes for 3 0
          Size lists for icon "document-open":
          5 sizes for 0 0
          5 sizes for 1 0
          5 sizes for 2 0
          5 sizes for 3 0
          @

          Both icons are displayed correctly.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #6

            Sounds like a bug in Qt 4.8 to me that got fixed in 5.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              toralf
              wrote on last edited by
              #7

              Yeah, I suppose it looks like that. Maybe the functionality just was never properly implemented in the past?

              BTW, a minor error somehow managed to slip into the code I posted; line 12 should actually read

              @for(int stateNo=0; stateNo<=QIcon::Off; stateNo++)@

              This doesn't make a whole lot of difference, though. The original code wouldn't really check all mode/state combinations, but the size lists are also empty for the ones that were left out (in the "file" case.)

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mcosta
                wrote on last edited by
                #8

                Hi,

                from your post I read that, when you call checkIcon() for PNG file also the name is Invalid.
                It's correct or is a copy error?

                Once your problem is solved don't forget to:

                • Mark the thread as SOLVED using the Topic Tool menu
                • Vote up the answer(s) that helped you to solve the issue

                You can embed images using (http://imgur.com/) or (http://postimage.org/)

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  toralf
                  wrote on last edited by
                  #9

                  It's correct. The name is always empty.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mcosta
                    wrote on last edited by
                    #10

                    As -Anrdee- André wrote, maybe a Qt 4.8 bug.

                    Can anyone verify that? At the moment I have only Qt5 installed on my PC's

                    Once your problem is solved don't forget to:

                    • Mark the thread as SOLVED using the Topic Tool menu
                    • Vote up the answer(s) that helped you to solve the issue

                    You can embed images using (http://imgur.com/) or (http://postimage.org/)

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      toralf
                      wrote on last edited by
                      #11

                      Well, I feel a bit embarrased now, because when testing again, I find that there is in fact one valid image size. Which I'm assuming was the case all along, although I've done a minor Qt update since posting the question, so there is a small chance that real problems existed in the past.

                      The situation is this:

                      When using the filename based constructor, a valid image and size is set up for exactly one mode/state combination - QIcon::Normal/QIcon::Off.

                      My original application as well as the first version of my simplified example would really only check state QIcon::On, due to an incorrect loop setup - as mentioned earlier. Now, I don't think a loop on enum values is very neat anyhow, but can't think of a better alternative.

                      I somehow managed to overlook the fact that one size was in fact returned for one of the 8 different combinations listed, after correcting the loop in the test program.

                      Sorry, folks.

                      It still looks like there is no icon name, though...

                      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