How do I access Awesomefonts in Qt on OpenSuse - c++

I wish to use some of the icons in fontawesome (http://fontawesome.io/icons) in my Qt Application, I have extracted the fontawesome-webfont.ttf file into usr/share/fonts.I tried searching online but could n't find any such examples.This is a sample code I have written for extracting an image out of a Resource(not what is required) and also accessing some Qfonts that were existent in Qfont library itself.( i.e courier new in the example below).
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QPixmap>
#include <QLabel>
#include <QHBoxLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
centralWidget = new QWidget(this);
gridLayout = new QGridLayout( centralWidget );
mylabel = new QLabel();
mylabel2= new QLabel();
font = new QFont("courier");
addresspic = new QPixmap(":/new/prefix1/address.png");
*addresspic=addresspic->scaled(50,50,Qt::KeepAspectRatio, Qt::FastTransformation);
mylabel->setPixmap(*addresspic);
mylabel2->setTextFormat(Qt::RichText);
mylabel2->setGeometry(QRect(QPoint(100,100),QSize(150, 150)));
mylabel2->setText(" ADDRESS ICON ");
gridLayout->addWidget(mylabel2);
gridLayout->addWidget(mylabel);
font->setItalic(true);
font->setPixelSize(20);
mylabel2->setFont(*font);
// gridLayout->setVerticalSpacing(1);
// gridLayout->setHorizontalSpacing(1);
this->setCentralWidget(centralWidget);
}
MainWindow::~MainWindow()
{
delete ui;
}
Thanks again
EDIT: The screenshot of error
EDIT 2: Trying G.M.'s method resulted in the following error : Any Idea why?

From https://github.com/dridk/QFontIcon download and add the qfonticon.h and qfonticon.cpp files to your project, then create the icons with the following code:
QFontIcon::addFont("/path/your/fonts/{your font}.ttf");
QIcon icon = QFontIcon::icon(0xf2b9);
{your widget}->setIcon(icon);
Example:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include "qfonticon.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget *centralWidget;
QGridLayout *gridLayout;
centralWidget = new QWidget(this);
gridLayout = new QGridLayout( centralWidget );
QFontIcon::addFont("/usr/share/fonts/fontawesome-webfont.ttf");
for(int i = 0; i < 15; i++){
QIcon icon = QFontIcon::icon(0xf2b9+i);
QPushButton *b = new QPushButton();
b->setIcon(icon);
gridLayout->addWidget(b);
}
this->setCentralWidget(centralWidget);
}
MainWindow::~MainWindow()
{
delete ui;
}
More Information: https://github.com/dridk/QFontIcon
I tested it with Qt 5.7 and Qtcreator 4.2

Try loading the font explicitly with...
int fid = QFontDatabase::addApplicationFont("/usr/share/fonts/fontawesome-webfont.ttf");
You can then query the font family name(s) using...
QFontDatabase::applicationFontFamilies(fid);
In my case the above resulted in the single family name "FontAwesome". That being the case you should then be able to use the font with...
QFont f("FontAwesome");
Note: The above seems to work as far as it goes but specifying a point size for the font isn't working as expected. Not sure why as the ttf file appears to contain the requested glyph sizes.
Edit 1
The QFont as constructed above can be used like any other unicode font. So a QLabel could be created with...
QLabel label;
label.setFont(QFont("FontAwesome"));
label.setText("\uf0fe"); /* f0fe is the code point for fa-plus-square */
Note that the answer provided by #eyllanesc is probably a far better approach. I'm simply adding this edit for completeness sake.

Related

Why isn't my C++/Qt program showing label object after to click on related PushButton?

I want to design a program including a pushButton and a lineEdit object in Qt with C++ that when the pushButton is clicked, it creates a label on the program and then sets its text to the written text in the lineEdit.
As you see inside void MainWindow::on_pushButton_clicked() function in the code below, I defined in my code to create an object of QLabel type and show the written text in the lineEdit but when I build and run my program, I don't see anything and it looks like the function didn't create the label. What's wrong with my code and what should I do?
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QLabel *label = new QLabel("Name: ", this);
label->setGeometry(10, 10, 150, 15);
label->setText(ui->lineEdit->text());
}
By the way, I'm new to Qt and that's why I accept other issues in my code. Thanks for helping.

QVideoWidget doesn't resize well

I have a Qt application which simply captures from the default webcam and shows it on a QVideoWidget. In the ui, I have a simple MainWindow with a QGraphicsView inside a VerticalLayout:
ui design
My mainwindow.cpp=============================================
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_viewfinder = new QVideoWidget(ui->captureView);
m_camera = new QCamera(QCameraInfo::defaultCamera());
m_camera->setViewfinder(m_viewfinder);
m_camera->start();
}
MainWindow::~MainWindow()
{
m_camera->stop();
delete m_viewfinder;
delete m_camera;
delete ui;
}
When I execute this, I get the application running, but the video contents do not scale according to the mainwindow size. Examples:
When I start the application
Resizing mainwindow down
Resizing mainwindow up
Is there some way to make the video content resize well and fit the available area?
I have seen this answer: QVideoWidget: Video is cut off, but it doesn't offer any solution that works for me. When use the QGraphicsView-QGraphicsScene-QGraphicsVideoItem chain, I see nothing at all.
When you use the following instruction:
m_viewfinder = new QVideoWidget(ui->captureView);
You are setting as the parent of m_viewfinder to captureView, so the positions of m_viewfinder will be relative to captureView, but this does not indicate that it will be the same size as the parent.
One of the easiest ways to do this is to use a layout. Also, it is not necessary to create the QGraphicsWidget or the QVBoxLayout, so I recommend you to delete it and get the design as it was established by default:
and then we establish a layout that is placed in the centralWidget, and in this layout we add the QVideoWidget.
...
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_viewfinder = new QVideoWidget;
QVBoxLayout *lay = new QVBoxLayout(ui->centralWidget);
lay->addWidget(m_viewfinder);
m_camera = new QCamera(QCameraInfo::defaultCamera());
m_camera->setViewfinder(m_viewfinder);
m_camera->start();
}
...
In the following link you can find the complete example.

Qt : Changing color of an icon used through FontAwesome in Qt when accessed through Unicode.

I have written a code to print an Icon accessed from FontAwesome installed in the system. I would like to change the color for the Icon printed on the Screen.I have tried using QPixmap and QIcon,but to no avail.Attached the output :
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QPushButton>
#include <QGridLayout>
#include <QWidget>
#include <QLabel>
#include "qfonticon.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget *centralWidget;
QGridLayout *gridLayout;
centralWidget = new QWidget(this);
gridLayout = new QGridLayout( centralWidget );
QFontIcon::addFont("/usr/share/fonts/fontawesome-webfont.ttf");
QIcon icon = QFontIcon::icon(0xf2e0,QColor(1,0,1,255));
//QFontIconEngine::addFile("/usr/share/fonts/fontawesome-webfont.ttf");
//QPixmap pix = QFontIconEngine::;
QPushButton *b = new QPushButton();
//QLabel *l = new QLabel();
b->setIcon(icon);
b->setIconSize(QSize(75,75));
//l->setPixmap(pix);
gridLayout->addWidget(b);
//gridLayout->addWidget(l);
this->setCentralWidget(centralWidget);
}
MainWindow::~MainWindow()
{
delete ui;
}

QT add Label to certain position

I'm new to QT and I want to know how to add a label on a certain position by code. I create a new application and I have these code automatically:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
I want to add a label to a certain position, for example, its geometry is (10, 10, 30, 80). How do I do this by code? Someone can help me?
You need to create an instance of QLabel, like this: (Here is the example from the docs. Try implementing it yourself with your standards. The docs are helpful)
QLabel *label = new QLabel(this);
label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
label->setText("first line\nsecond line");
label->setAlignment(Qt::AlignBottom | Qt::AlignRight);
//Here is how to change position:
label->setGeometry(QRectF(10,10,30,80));
By the way, here is what an example QLabel looks like:

Playing video on fullscreen mode in QT

I want to play(shared, not multiple) video file(mp4) using QMediaPlayer(with output videothread to QGraphicsVideoItem) on multiple monitors on the single computer, connected by HDMI/DVI cable.
I made a video playback on a single monitor, but I need to play this video on several monitors as one.
I need of crossplatform(Linux/Windows) solution and i must provide configuration monitors for playing video in my application.
Whether it is possible to organize a means of QT 5.4 or extended C++ libraries?
If yes, please prompt me the best way to do it.
Sorry for bad English and thanks for the help in advance!
Hello! I need to make playing video overlay images.
I am used QT components QMediaPlayer and QGraphicsVideoItem to organize it.
But I could not stretch the video to fill the screen(fullscreen mode).
It looks like this:
Please, help me. Thank you.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebView>
#include <QVBoxLayout>
#include <QFile>
#include <QMediaPlayer>
#include <QtMultimedia>
#include <QGraphicsVideoItem>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVBoxLayout *layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
this->centralWidget()->setLayout(layout);
layout->addWidget(ui->graphicsView);
ui->graphicsView->setStyleSheet("background: transparent");
this->showMaximized();
QMediaPlayer * player = new QMediaPlayer();
QGraphicsVideoItem *item = new QGraphicsVideoItem;
QGraphicsScene * scene = new QGraphicsScene;
item->setSize(QSize(ui->graphicsView->width(),ui->graphicsView->height()));
ui->graphicsView->setScene(scene);
player->setVideoOutput(item);
qDebug() << item;
ui->graphicsView->scene()->addItem(item);
QPixmap pix1( "/home/ibragim/examples/ntv.png" );
player->setMedia(QUrl::fromLocalFile("/home/ibragim/examples/sm.mp4"));
player->play();
ui->graphicsView->show();
ui->graphicsView->scene()->addPixmap(pix1.scaledToHeight(100))->setPos(160,30);
}
MainWindow::~MainWindow()
{
delete ui;
}
this might do what you desire ...
void QGraphicsView::fitInView(const QGraphicsItem * item, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
This is an overloaded function.
Ensures that item fits tightly inside the view, scaling the view according to aspectRatioMode.