How to use FontAwesome in Qt - c++

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.

Related

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.

Qt - QFile not opening a .qss file

I'm trying to use an external stylesheet for my project and I'm having trouble opening it using the QFile class. I've imported it into the .qrc file, a portion of it looks like this:
<qresource prefix="stylesheets">
<file>Resources/Stylesheet.qss</file>
</qresource>
This is how I am opening and using the file:
QFile stylesheet(":/stylesheets/Resources/Stylesheet.qss");
if (stylesheet.open(QIODevice::ReadOnly | QIODevice::Text))
{
newGameDialog.setStyleSheet(stylesheet.readAll());
stylesheet.close();
}
What could be wrong with this? I am using Visual Studio 2013 with the latest Qt and the VS Qt Add-in. I've also set the project to support QML in "Qt Project Settings".
The .qss file looks like this (it works if set directly as a QString). I'm not sure if the "import" line is needed:
import Qt 5.3.1
QDialog
{
background-color: 'white';
}
It is reading it fine, but the style is not applying. Here it is in debug mode:
QDialog does not support "background-color", only "backgroud:".
(Further ideas if that doesnt work:
Maybe forgot to specify Q_OBJECT for newGameDialog class?
Alternatively use Qt Designer to create a QDialog, copy your stylesheet source in the stylesheet property field and see if it works or if the designer shows an error or does apply the style correctly when test-instanciating the dialog (Ctrl+R i think).)

QT Resource File - Images Not Showing When Running

I just upgraded to QT Creator 5.3 and created a brand new QT Widgets Application project and am using the Microsoft VC++ compiler. All I have is a resource file with "logo.png" added (which opens in QT if I double click it), and a label that I am trying to set the background image for. The problem is that no matter what I do I cannot get the image to show up when the program is running. The only way I can get it to show the image is by not using the resource file and instead map directly to the file (ex. "c:/blah/blah/logo.png")
Also, if I set the background image in the UI designer, the background shows up in the IDE but NOT when I run the program. I have tried probably 20+ variations of the code, including resource file aliases as well as adding the files to the project directly, and nothing seems to work.
I'm not sure if there is a step I am missing, or if perhaps there is something that I have to do to get the project to compile the image.
Resources.qrc
<RCC>
<qresource prefix="/">
<file>logo.png</file>
</qresource>
</RCC>
loginform.cpp
QPixmap pixmap = QPixmap (":/logo.png");
ui->label->setPixmap( QPixmap(pixmap));
Project.pro
OTHER_FILES += \
logo.png
RESOURCES += \
Resources.qrc
After countless hours of trial, error, and mostly frustration, I have been able to resolve this issue by simply running qmake.
To do this I just right clicked on the project and clicked "Run qmake" and suddenly the image errors (as well as a few others) were resolved!

Qt qrc resource file - can't load icon

I have a Qt5 desktop project and I added a "resource.qrc" file with the Qt Creator editor which created the following line into the project's .pro file:
RESOURCES = resource.qrc
I put a blank prefix and a png file (14x14) and I tried to use it like this:
QPixmap pixmap = QPixmap ("://my_image.png");
ui->combobox->addItem(QIcon(pixmap), "itemname");
The problem is: the icon won't show up!
The following works:
QPixmap pixmap(14,14);
pixmap.fill(QColor("red"));
ui->combobox->addItem(QIcon(pixmap), "itemname");
so the problem must be in the resource embedding process.. I noticed that the generated "exe" hasn't a resource section inside it... I don't have static linked external libraries, so I don't think I need the Q_INIT_RESOURCE(resource) macro (it gives me undefined external)
Update:
I'm posting here my qrc file:
<RCC>
<qresource prefix="/">
<file>my_image.png</file>
</qresource>
</RCC>
it's pretty simple and I don't understand why at runtime icons are not showing up
I had this same issue recently, where I malformed the resource string. If you are using a current version of Qt Creator, you can open your .qrc file for edit and then right-click the resource (in this case image) that you are trying to address, then click "Copy Resource Path to Clipboard". And voila, you have the correct resource string every time.
Qt Creator is awesome.
Hope this helps!
#Nikos C. gives you useful advice, but I think your main problem was that you didn't use the correct link to the resource.
In your code, you have:
QPixmap pixmap = QPixmap ("://my_image.png");
but, according to the documentation, it should be
QPixmap pixmap = QPixmap (":/my_image.png");
or you can give aliases to your resources, and use those instead.
Issue is solved is use rcc.exe
C:\root\QT>c:\root\QT\4.7.4\bin\rcc.exe Headless.qrc -o qtresources.cpp
During compilation you should have images in the path.
Create the qtresources.cpp file include this file in makefile or project. You should able to see the image.

How to set application icon in a Qt-based project?

How do you set application icon for application made using Qt? Is there some easy way? It's a qmake-based project.
For Qt 5, this process is automated by qmake. Just add the following to the project file:
win32:RC_ICONS += your_icon.ico
The automated resource file generation also uses the values of the following qmake variables: VERSION, QMAKE_TARGET_COMPANY, QMAKE_TARGET_DESCRIPTION, QMAKE_TARGET_COPYRIGHT, QMAKE_TARGET_PRODUCT, RC_LANG, RC_CODEPAGE.
For Qt 4, you need to do it manually. On Windows, you need to create a .rc file and add it to your project (.pro). The RC file should look like this:
IDI_ICON1 ICON DISCARDABLE "path_to_you_icon.ico"
The .pro entry should also be win32 specific, e.g.:
win32:RC_FILE += MyApplication.rc
Verified in Linux (Qt 4.8.6) and Windows (Qt 5.6):
1) Add the icon file to your project folder;
2) In the main function call setWindowIcon() method. For example:
QApplication a(argc, argv);
a.setWindowIcon(QIcon("./images/icon.png"));
To extend Rob's answer, you can set an application icon for macOS by adding and modifying the following line onto your .pro file.
macx: ICON = <app_icon>.icns
Note that the ICON qmake variable is only meant to target macOS.
For Windows, use
RC_ICONS = <app_icon>.ico if you're attaching a .ico file
or RC_FILE = <app_icon>.rc if you want to attach your icon through a .rc file. (Be sure to add IDI_ICON1 ICON DISCARDABLE "myappico.ico" into the rc file. Indentation not mine.)
For further reading, see Setting the Application Icon.
For Qt5 on Windows, move your ico file to project folder and add this line to your .pro file:
RC_ICONS = myappico.ico
Offical link: https://doc.qt.io/qt-5/appicon.html
Now that Qt has upgraded to 5.0.1, there is new method to add an application icon. First, you need prepare a resource file, named as .qrc
1) Without Qt Designer, I assume there is a QMainWindow instance whose name is MainWin. You can use:
QIcon icon(":icon/app.icon");
MainWin.setWindowIcon(icon);
2) With Qt Designer, you can modify the property of QMainWindow. Choose icon resource from .qrc and insert it into the row of windowIcon.
The above method can be used in Qt4.7, Qt4.8.x.