Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting custom Icon

Tags:

c++

icons

qt

I am struggling with adding a custom icon to a QToolButton. I have a resource file(properly under RESOURCES in the .pro file): myResourceFile.qrc containing

/images
ICON_TEST.png

and code

dragButton = new QToolButton(this);
QString resourcePath = ":/images/ICON_TEST.png";
QPixmap pixIcon(resourcePath); //THE LINE THAT GIVES THE ERROR!
dragButton->setIcon(QIcon(pixIcon));

I get the error:

no match for call to QPixmap(QString &)

How do I pass the proper path to the QPixmap object and then the object to setIcon()?

edit: Just confirmed that the .png file exists and is recognized by Qt:

File exists - true   ":/images/ICON_TEST.png" 

with:

 qDebug()<<"File exists -"<<QFileInfo(":/images/ICON_TEST.png").exists()<<" "<<
              QFileInfo(":/images/ICON_TEST.png").absoluteFilePath();

EDIT WITH SOLUTION: below as answer

Question remains, why this happened? The variable pixIcon is declared in the .h file of the class as:

QPixmap pixIcon;

and assigned a value in the constructor:

 pixIcon(resourcePath);

Which to me seems to be almost equivalent to doing it in one line.

This was on Windows Vista with Qt Creator 2.0.

like image 719
Stanislaw T Avatar asked May 24 '26 06:05

Stanislaw T


1 Answers

For some reason, when doing everything in one line it worked as it should have:

 dragButton->setIcon(QIcon(QPixmap(resourcePath)));
like image 186
Stanislaw T Avatar answered May 25 '26 21:05

Stanislaw T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!