Unsolved How to get this icon's name?
-
-
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
-
@sierdzio
icon is the button's icon.
QIcon bIco;
I want get the icon's name to compare other string.
I have no idea. -
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?
-
-
Yes, it's a bug. But to get the icon name from the button, there are two options:
- Store the icon name before setting it to the button and get it later for comparison.
- Reimplement the
QPushButton
class to set/get the button's icon and then compare.
-
@Cobra91151
NO “y" only false...
But Reimplement the QPushButton class to set/get the button's icon and then compare?
-
To do such comparison you should reimplement the
QPushButton
class, because of theQt
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. -
@Cobra91151
OH!
I will try...
Thank you very much. -
Your welcome. I'm also working on it (reimplementing the
QPushButton
). I will post it here when it will be ready. -
I have done some checking, so you also need to reimplement the
QIcon
class to make it working. -
Hi
Can i ask what you need icon (file) name for ? -
Hi! I need the icon name to compare with desktop shortcut name.
-
@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. -
Yes, you are right. This is another solution to use
setProperty
and thenproperty
to get it. But I'm still want to make it by reimplementing classes. -
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!
-
@Cobra91151
Nice.