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. How to get this icon's name?
Forum Updated to NodeBB v4.3 + New Features

How to get this icon's name?

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 4 Posters 4.9k Views 2 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.
  • sierdzioS sierdzio

    I assume bIco is the button?

    qDebug() << bIco.icon().name();
    

    The documentation states, however, that the name might be unavailable. https://doc.qt.io/qt-5/qicon.html#name

    cloud21C Offline
    cloud21C Offline
    cloud21
    wrote on last edited by
    #3

    @sierdzio
    icon is the button's icon.
    QIcon bIco;
    I want get the icon's name to compare other string.
    I have no idea.

    Cobra91151C 1 Reply Last reply
    0
    • cloud21C cloud21

      @sierdzio
      icon is the button's icon.
      QIcon bIco;
      I want get the icon's name to compare other string.
      I have no idea.

      Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by Cobra91151
      #4

      @cloud21

      Hi! I think there is a bug with displaying the icon name from the button. For example:

      ui->pushButton->setIcon(QPixmap(":/Resource/some_icon.png"));
      QString myButtonIconName = ui->pushButton->icon().name();
      qDebug() << myButtonIconName; // returns ""
      

      By checking for isNull(), it returns false, so it has the icon, but it can't find the name.

      Also I tried the different approach to get the icon name:

      QIcon testIcon = ui->pushButton->icon();
      qDebug() << testIcon.name(); // returns ""
      

      It still returns empty string. Can anyone confirm it?

      cloud21C 1 Reply Last reply
      1
      • Cobra91151C Cobra91151

        @cloud21

        Hi! I think there is a bug with displaying the icon name from the button. For example:

        ui->pushButton->setIcon(QPixmap(":/Resource/some_icon.png"));
        QString myButtonIconName = ui->pushButton->icon().name();
        qDebug() << myButtonIconName; // returns ""
        

        By checking for isNull(), it returns false, so it has the icon, but it can't find the name.

        Also I tried the different approach to get the icon name:

        QIcon testIcon = ui->pushButton->icon();
        qDebug() << testIcon.name(); // returns ""
        

        It still returns empty string. Can anyone confirm it?

        cloud21C Offline
        cloud21C Offline
        cloud21
        wrote on last edited by
        #5

        @Cobra91151
        0_1541424364600_QQ.png

        1 Reply Last reply
        0
        • Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by Cobra91151
          #6

          Yes, it's a bug. But to get the icon name from the button, there are two options:

          1. Store the icon name before setting it to the button and get it later for comparison.
          2. Reimplement the QPushButton class to set/get the button's icon and then compare.
          cloud21C 1 Reply Last reply
          0
          • Cobra91151C Cobra91151

            Yes, it's a bug. But to get the icon name from the button, there are two options:

            1. Store the icon name before setting it to the button and get it later for comparison.
            2. Reimplement the QPushButton class to set/get the button's icon and then compare.
            cloud21C Offline
            cloud21C Offline
            cloud21
            wrote on last edited by
            #7

            @Cobra91151
            NO “y" only false...
            But Reimplement the QPushButton class to set/get the button's icon and then compare?
            0_1541426195378_sdf.png

            Cobra91151C 1 Reply Last reply
            0
            • cloud21C cloud21

              @Cobra91151
              NO “y" only false...
              But Reimplement the QPushButton class to set/get the button's icon and then compare?
              0_1541426195378_sdf.png

              Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on last edited by Cobra91151
              #8

              @cloud21

              To do such comparison you should reimplement the QPushButton class, because of the Qt icon bug. But reimplementing classes are not easy tasks. The easiest way would be storing your icons name and then checking for comparison. You have to decide what option to choose.

              cloud21C 1 Reply Last reply
              0
              • Cobra91151C Cobra91151

                @cloud21

                To do such comparison you should reimplement the QPushButton class, because of the Qt icon bug. But reimplementing classes are not easy tasks. The easiest way would be storing your icons name and then checking for comparison. You have to decide what option to choose.

                cloud21C Offline
                cloud21C Offline
                cloud21
                wrote on last edited by
                #9

                @Cobra91151
                OH!
                I will try...
                Thank you very much.

                Cobra91151C 2 Replies Last reply
                1
                • cloud21C cloud21

                  @Cobra91151
                  OH!
                  I will try...
                  Thank you very much.

                  Cobra91151C Offline
                  Cobra91151C Offline
                  Cobra91151
                  wrote on last edited by Cobra91151
                  #10

                  @cloud21

                  Your welcome. I'm also working on it (reimplementing the QPushButton). I will post it here when it will be ready.

                  1 Reply Last reply
                  0
                  • Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by
                    #11

                    @cloud21

                    I have done some checking, so you also need to reimplement the QIcon class to make it working.

                    mrjjM 1 Reply Last reply
                    0
                    • Cobra91151C Cobra91151

                      @cloud21

                      I have done some checking, so you also need to reimplement the QIcon class to make it working.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      @Cobra91151

                      Hi
                      Can i ask what you need icon (file) name for ?

                      Cobra91151C 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @Cobra91151

                        Hi
                        Can i ask what you need icon (file) name for ?

                        Cobra91151C Offline
                        Cobra91151C Offline
                        Cobra91151
                        wrote on last edited by Cobra91151
                        #13

                        @mrjj

                        Hi! I need the icon name to compare with desktop shortcut name.

                        mrjjM 1 Reply Last reply
                        0
                        • Cobra91151C Cobra91151

                          @mrjj

                          Hi! I need the icon name to compare with desktop shortcut name.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

                          @Cobra91151
                          Ok, im just asking as normally its easier to use a dynamic property to hold
                          such information for a widget.
                          http://doc.qt.io/qt-5/properties.html#dynamic-properties
                          Those can be set directly in Designer. ( the green big plus over list)
                          But it of cause depends on the full use case.

                          Cobra91151C 1 Reply Last reply
                          1
                          • mrjjM mrjj

                            @Cobra91151
                            Ok, im just asking as normally its easier to use a dynamic property to hold
                            such information for a widget.
                            http://doc.qt.io/qt-5/properties.html#dynamic-properties
                            Those can be set directly in Designer. ( the green big plus over list)
                            But it of cause depends on the full use case.

                            Cobra91151C Offline
                            Cobra91151C Offline
                            Cobra91151
                            wrote on last edited by
                            #15

                            @mrjj

                            Yes, you are right. This is another solution to use setProperty and then property to get it. But I'm still want to make it by reimplementing classes.

                            1 Reply Last reply
                            1
                            • cloud21C cloud21

                              @Cobra91151
                              OH!
                              I will try...
                              Thank you very much.

                              Cobra91151C Offline
                              Cobra91151C Offline
                              Cobra91151
                              wrote on last edited by Cobra91151
                              #16

                              @cloud21

                              So, here is my solution with reimplemented QPushButton:

                              AppPushButton.h

                              #ifndef APPPUSHBUTTON_H
                              #define APPPUSHBUTTON_H
                              
                              #include <QPushButton>
                              #include <QFileInfo>
                              
                              class AppPushButton : public QPushButton
                              {
                              public:
                                  explicit AppPushButton(QWidget *parent = Q_NULLPTR);
                                  explicit AppPushButton(const QString &text, QWidget *parent = Q_NULLPTR);
                                  void setButtonIcon(QString iconPath); // sets the icon by path
                                  QString iconFileName(); // gets the icon name with extension
                                  QString iconFileNameWithoutExtension(); // gets the icon name without extension
                                  virtual ~AppPushButton();
                              
                              private:
                                  QString myIconPath;
                                  QFileInfo iconFileInfo;
                              };
                              
                              #endif // APPPUSHBUTTON_H
                              
                              

                              AppPushButton.cpp

                              #include "apppushbutton.h"
                              
                              AppPushButton::AppPushButton(QWidget *parent) : QPushButton(parent)
                              {
                              
                              }
                              
                              AppPushButton::AppPushButton(const QString &text, QWidget *parent) : QPushButton(text, parent)
                              {
                              
                              }
                              
                              void AppPushButton::setButtonIcon(QString iconPath)
                              {
                                  myIconPath = iconPath;
                                  setIcon(QIcon(myIconPath));
                              }
                              
                              QString AppPushButton::iconFileName()
                              {
                                  iconFileInfo.setFile(myIconPath);
                                  return iconFileInfo.fileName();
                              }
                              
                              QString AppPushButton::iconFileNameWithoutExtension()
                              {
                                  iconFileInfo.setFile(myIconPath);
                                  return iconFileInfo.baseName();
                              }
                              
                              AppPushButton::~AppPushButton()
                              {
                              
                              }
                              

                              Usage:

                              AppPushButton *myButton = new AppPushButton("Click me!", this);
                              myButton->setButtonIcon(":Icons/qt-icon.png");
                              qDebug() << "Icon filename with extension: " << myButton->iconFileName();
                              qDebug() << "Icon filename without extension: " << myButton->iconFileNameWithoutExtension();
                              

                              Result:
                              Icon filename with extension: "qt-icon.png"
                              Icon filename without extension: "qt-icon"

                              Happy coding!

                              cloud21C 1 Reply Last reply
                              2
                              • Cobra91151C Cobra91151

                                @cloud21

                                So, here is my solution with reimplemented QPushButton:

                                AppPushButton.h

                                #ifndef APPPUSHBUTTON_H
                                #define APPPUSHBUTTON_H
                                
                                #include <QPushButton>
                                #include <QFileInfo>
                                
                                class AppPushButton : public QPushButton
                                {
                                public:
                                    explicit AppPushButton(QWidget *parent = Q_NULLPTR);
                                    explicit AppPushButton(const QString &text, QWidget *parent = Q_NULLPTR);
                                    void setButtonIcon(QString iconPath); // sets the icon by path
                                    QString iconFileName(); // gets the icon name with extension
                                    QString iconFileNameWithoutExtension(); // gets the icon name without extension
                                    virtual ~AppPushButton();
                                
                                private:
                                    QString myIconPath;
                                    QFileInfo iconFileInfo;
                                };
                                
                                #endif // APPPUSHBUTTON_H
                                
                                

                                AppPushButton.cpp

                                #include "apppushbutton.h"
                                
                                AppPushButton::AppPushButton(QWidget *parent) : QPushButton(parent)
                                {
                                
                                }
                                
                                AppPushButton::AppPushButton(const QString &text, QWidget *parent) : QPushButton(text, parent)
                                {
                                
                                }
                                
                                void AppPushButton::setButtonIcon(QString iconPath)
                                {
                                    myIconPath = iconPath;
                                    setIcon(QIcon(myIconPath));
                                }
                                
                                QString AppPushButton::iconFileName()
                                {
                                    iconFileInfo.setFile(myIconPath);
                                    return iconFileInfo.fileName();
                                }
                                
                                QString AppPushButton::iconFileNameWithoutExtension()
                                {
                                    iconFileInfo.setFile(myIconPath);
                                    return iconFileInfo.baseName();
                                }
                                
                                AppPushButton::~AppPushButton()
                                {
                                
                                }
                                

                                Usage:

                                AppPushButton *myButton = new AppPushButton("Click me!", this);
                                myButton->setButtonIcon(":Icons/qt-icon.png");
                                qDebug() << "Icon filename with extension: " << myButton->iconFileName();
                                qDebug() << "Icon filename without extension: " << myButton->iconFileNameWithoutExtension();
                                

                                Result:
                                Icon filename with extension: "qt-icon.png"
                                Icon filename without extension: "qt-icon"

                                Happy coding!

                                cloud21C Offline
                                cloud21C Offline
                                cloud21
                                wrote on last edited by
                                #17

                                @Cobra91151
                                Nice.

                                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