icon for QT app - c++

I'm using QTCreator to build an application on Mac. At this time the application icon is the default one set by Qt Creator.
I would like to set one custom specific to my app.
What I have in in the .qrc file, I have added
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>images/logo.icns</file>
</qresource>
</RCC>
I have made a try on my main.cpp by adding
QIcon icon(":images/logo.icns");
QApplication app(argc, argv);
app.setWindowIcon(icon);
it doesn't work... I have tried to do it in another class in which I define how the window of the app is done
QIcon icon(":images/logo.icns");
QMainWindow *window = new QMainWindow();
window->setWindowTitle(QString::fromUtf8("PULS"));
window->resize(600, 600);
QWidget *centralWidget = new QWidget(window);
centralWidget->setWindowIcon(icon);
It's also not working.

The icon's path may be incorrect.
try to change
QIcon icon(":images/logo.icns");
to
QIcon icon(":/images/logo.icns");
cause of your .qrc file define the prefix:
<qresource prefix="/">

To set the icon for the executable file you need:
ICON = logo.icns
In your .pro file.
To set the window icon you need to call
QIcon icon(":/images/logo.icns");
centralWidget->setWindowIcon(icon);
Or try using a .png

Related

How to use FontAwesome in Qt

FontAwesome makes adding editable icons into HTML fairly easy:
<link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
<i class="fa fa-camera-retro"></i>
I have tried adding a FontAwesome icon to a Qt widget, but it doesn't show:
QPushButton *qB = new QPushButton("TXT");
qB -> setProperty("class", "fa fa-camera-retro");
How can I go about it to make it work with Qt widgets?
Qt doesn't work like that.
You need to create a qrc file and bundle FontAwesome to your project, like this:
<RCC>
<qresource prefix="/">
<file alias="FontAwesome.otf">FontAwesome.otf</file>
</qresource>
</RCC>
Then include it in the .pro file:
RESOURCES += resources.qrc
Then load it and use it, like this, providing the unicode character of the icon you intend to display:
if (QFontDatabase::addApplicationFont(":/FontAwesome.otf") < 0)
qWarning() << "FontAwesome cannot be loaded !";
QFont font;
font.setFamily("FontAwesome");
font.setPixelSize(32);
ui->pushButton->setFont(font);
ui->pushButton->setText("\uf083");
In your case, the camera icon code is indicated here
Result:
I'm looking to do the same with Qt5 and the last version of FontAwesome 5.x. After some research, I found a very simple class in GitHub for using Font Icon in Qt 5 and Qt 6 projects with QWidget. This class provides helpers to use Font awesome (v4.7, v5.15 and v6.0) and Google Material Icon (v4).
The code is very small and provides all the functions needed to display icons in QPushButton, QToolButton, etc.
Here, the GitHub link : https://github.com/philvl/ZFontIcon.

How to include an icon in the title bar of window in Qt?

I want to be able to replace the existing standard icon with my own but I am unsure how to do so. I only started Qt one week ago so I don't know much about the application. In addition to this I looked at the Qt website but their method for adding an icon did not work on my computer.
Besides using a RC file (Windows) you may specify the icon directly using qmake.
win32: RC_ICONS = icon.ico
Make sure it detects the path correctly. In the provided case the icon resides in the project root directory right beside the project file.
For window icons you typically, if application wide applicable, use some method of QApplication.
...
QApplication app(argc, argv);
...
app.setWindowIcon(QIcon(":/icon");
...
which is consuming the resource file using the alias icon. Here is an example (res.qrc):
<RCC>
<qresource prefix="/">
<file alias="icon">icon.png</file>
</qresource>
</RCC>
More information on resources are here.
Perhaps try the function setWindowIcon function on the QWidget or QApplication.

Using a resource file in QMediaPlayer

How do I load a .mp3 file to use in a QMediaPlayer from a .qrc resource file?
This is what I have so far
QMediaPlayer *player = new QMediaPlayer;
player->setMedia(QUrl::fromLocalFile(":/audio/theme.mp3"));
player->play();
resources.qrc:
<RCC>
<qresource prefix="/audio">
<file>theme.mp3</file>
</qresource>
</RCC>
theme.mp3 is located in the project directory.
Use m_player->setMedia(QUrl("qrc:/audio/theme.mp3"));
If you are using Qt Creator, you can copy this url to clipboard by pressing right button on the audio file in the side bar and choosing Copy url "...".

qtcreator tray icon under window does not show up when deployed

i'am trying to deploy a QT application made in QT Creator which goes back to system tray when closed.
I have made a svg tray icon, when i run it from QT Creator, either in debug and in release mode under Window 7, the tray icon shows up, but when i copy everything to another directory to make a distributable archive from it, the tray icon does not show up anymore.
Of course i search already for solutions, but everything i have found yet, i have made.
So what i have:
trayicon.svg file in project root
qrc file created, adding trayicon.svg into the resource files root
in project .pro file: RESOURCES += resources.qrc
copied binary + necessary dll's to target directory
copied QT plugins imageformats/* to target dir imageformats
added QApplication a(argc, argv); a.addLibraryPath(a.applicationDirPath()); to main.cpp
that is everything i found so far, but still the system tray icon is not showing up
What am i missing?
(current qt 4.8 + current qtcreator by the way)
#netrom
code in MainWindow : QMainWindow constructor:
trayIcon = new QSystemTrayIcon(this);
showAction = new QAction(tr("&Show"), this);
connect(showAction, SIGNAL(triggered()), this, SLOT(show()));
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(showAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon->setContextMenu(trayIconMenu);
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
trayIcon->setIcon(QIcon(":trayicon.svg"));
trayIcon->show();
Create iconegines directory in your app directory (for example: c:\MyApp\iconengines).
Copy qsvgicon.dll to this new directory (for example: c:\MyApp\iconengines\qsvgicon.dll) from qt plugins directory (in my case it's c:\qt\5.4\mingw491_32\plugins\iconengines\qsvgicon.dll).
Copy QtSvg.dll to your app directory (for example: c:\MyApp\Qt5Svg.dll) from Qt bin directory (in my case it's c:\qt\5.4\mingw491_32\bin\Qt5Svg.dll).
P.S. I know I'm late, this answer is for those who will google same problem.
Again, way later for DuckDuckGo-ers:
If your icon is not in .svg you might need another solution. For my .ico system tray icon I had to copy <path-to-qt>\plugins\imageformats into the application folder. Actually only qico.dll was needed in that folder in my case, but you get my drift.

Does Meego Nokia N950 support C++ QML binding?

I want to communicate between C++ and QML on Meego. It happens using C++ QML binding as described in this link.
When I run an app on the Symbian platform it works, and data from C++ is available to manipulate in some QML file.
But on Meego it always says myItem.qml file not found.
See the following code snippet:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QDeclarativeView view;
MyClass myClass;
view.rootContext()->setContextProperty("myObject", &myClass);
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
return app.exec();
}
Does Meego does not support QML C++ binding?
I also tried to give the exact path of the QML file but never found that file, even myItem is in project structure.
Is there some thing associated with path settings in .pro file etc
Please help on this as I am stuck here with this and cannot move forward in my app using Qt Quick.
Yes, but your problem isn't related to C++ and QML bindings. Your application (specifically the view) can't find the .qml file to load.
By default, I think on meego the default path to look in is ../qml. So by default you should be packaging into /opt/YOURNAME with the binary in /opt/YOURNAME/bin and the qml files in /opt/YOURNAME/qml.
However, you can also put the qml files into a resource and use the :/ paths to access a resource instead.
IE, in your resource file:
<RCC>
<qresource prefix="/">
<file>qml/foo.qml</file>
</qresource>
</RCC>
And in C++:
viewer.setSource(QUrl("qrc:/qml/foo.qml"));