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:
Related
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.
so I an new to C++ and QT and I am trying to add a widget to a scroll area but without a layout as I want the widgets to be click and dragged.
Here's my code (main window.cpp):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "player_window.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
player_window *player = new player_window(ui->scrollArea_WidgetContents);
player->setGeometry(50,50,450,130);
ui->scrollArea_WidgetContents->layout()->addWidget(player);
}
Thanks!
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.
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.
I'm using QGraphicsDropShadowEffect to make my GUI look pretier. Minimal working sample:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsDropShadowEffect>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsDropShadowEffect *g = new QGraphicsDropShadowEffect(this);
ui->pushButton->setGraphicsEffect(g);
ui->pushButton_2->setGraphicsEffect(g);
ui->pushButton_3->setGraphicsEffect(g);
}
MainWindow::~MainWindow()
{
delete ui;
}
As you see, I have 3 buttons and want to have a fancy shadow at the top of every button. Although I set graphics effect on every button it can be seen only on the last button, heres the image:
How can I improve it and what is the cause?
This works:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsDropShadowEffect>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsDropShadowEffect *g1 = new QGraphicsDropShadowEffect(this);
QGraphicsDropShadowEffect *g2 = new QGraphicsDropShadowEffect(this);
QGraphicsDropShadowEffect *g3 = new QGraphicsDropShadowEffect(this);
ui->pushButton->setGraphicsEffect(g1);
ui->pushButton_2->setGraphicsEffect(g2);
ui->pushButton_3->setGraphicsEffect(g3);
}
MainWindow::~MainWindow()
{
delete ui;
}
but seem not to be the best solution I can have.
It's the normal behavior of the function you are calling
see documentation
http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#setGraphicsEffect
If effect is the installed on a different item, setGraphicsEffect()
will remove the effect from the item and install it on this item.