Solved Can't give button a QIcon!
-
I've tried both png and ico versions but they don't work. and this is one of the first examples of the getting started with qt5
so i didn't write it. maybe it wasn't tested with windows 10 and mingw32 ? does anyone know how to make the button have an Icon?#include <QApplication> #include <QPushButton> int main(int argc, char *argv[]) { QApplication app(argc, argv); QPushButton myButton(QIcon("Save.ico"), "Push me"); myButton.setToolTip("Click this to turn back the hands of time"); myButton.show(); return app.exec(); }
-
@nullbuil7
QIcon("..\\Save.ico")
orQIcon("../Save.ico")
. Either of those would work but don't do that. Relative paths are relative to current working directory, which can be different depending on how you start your app (via exe click, shortcut, command line etc.).
If your path is relative to your exe location then retrieve a path of your exe and build an absolute path at runtime.For example, if your exe is in
C:\MyApp\bin\MyApp.exe
and your icon is inC:\MyApp\data\icon.ico
you can get a path to it like this:QString path = qApp->applicationDirPath() + "/../data/icon.ico";
also, take a look at Qt Resources System. You can just embed your icon inside the data segent of your exe and use a resource path like this
QIcon(":/icon.ico")
. -
Are you sure your app can find that icon, i.e.
QIcon("Save.ico").isNull()
doesn't return true? With a path like that the ico file needs to be in the same directory as the exe of your app. -
Passing a reference to a temporary (stack) object? Unless you can be certain that the framework copies what's needed from the QIcon it will not exist after the call to the constructor, right?
-
@Kent-Dorfman
Correct me if I'm wrong: if you don't thinkQPushButton
keeps a copy/increments the count of anyQIcon
passed to it, then everywhere you pass an icon to a button you'd have to keep the icon in scope for as long as you use the button, which doesn't sound right? -
@Kent-Dorfman said:
Unless you can be certain that the framework copies what's needed
Good APIs don't store pointers to parameters passed by
const &
and Qt doesn't here. That would be a disaster waiting to happen. The icon is copied and it's a shallow copy sinceQIcon
is one of the implicitly shared classes.The code is fine. It's either a missing resource, wrong path or missing image format plugin.
-
i found that the problem is path and can be resolved using absolute path but that's where the problem starts.
as i said i just started and i can't use c-string and Qstring together to create a relative path under windows. ( i can't use experimental::filesystem either) so i don't know how to pass a relative path under windows 10 / migw32 with QString.
any suggestions?
i tried : //icon.ico, \icon.ico, ..//icon.ico, ..\icon.co etc. -
@nullbuil7 said in Can't give button a QIcon!:
as i said i just started and i can't use c-string and Qstring together to create a relative path under windows.
Sorry, but this doesn't make sense.
It sounds like you would be best making an absolute path at run-time, so that you don't have to rely on what the current directory might or might not be if you use a relative path. You can build the path using whatever Qt call/variable it is which gives you, say, the path to the executable, if that's where your icon file lives. Or, maybe you should embed the icon as a resource?
-
What @JonB is suggesting is e.g. to use QCoreApplication::applicationDirPath()
-
@nullbuil7
QIcon("..\\Save.ico")
orQIcon("../Save.ico")
. Either of those would work but don't do that. Relative paths are relative to current working directory, which can be different depending on how you start your app (via exe click, shortcut, command line etc.).
If your path is relative to your exe location then retrieve a path of your exe and build an absolute path at runtime.For example, if your exe is in
C:\MyApp\bin\MyApp.exe
and your icon is inC:\MyApp\data\icon.ico
you can get a path to it like this:QString path = qApp->applicationDirPath() + "/../data/icon.ico";
also, take a look at Qt Resources System. You can just embed your icon inside the data segent of your exe and use a resource path like this
QIcon(":/icon.ico")
.