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.
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 im writing a QT application that will read values from a serial port and display them in a graph during runtime. I managed to update my QChart during runtime with just a random generated value to try out updating in real time and it all works fine.
But my application slows down the more and more i append until it gets completely unusable.
I do understand that the list containing my points grows, but after a 100 points or so it really really slows down, thats really fast, it feels like i have some sort of memory leak?
I know the usual answer is "Don't use QCharts" but im a beginner at both C++ and QT so this is what i'm using for simplicity.
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QGridLayout>
#include <QLabel>
#include <QDebug>
QT_CHARTS_USE_NAMESPACE
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
series = new QLineSeries();
chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->axes(Qt::Vertical).back()->setRange(-10, 10);
chart->axes(Qt::Horizontal).back()->setRange(0, 100);
chart->setContentsMargins(0, 0, 0, 0);
chart->setBackgroundRoundness(0);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
ui->setupUi(this);
QLabel *label = new QLabel();
label->setText("Hello World");
QGridLayout *layout = new QGridLayout;
QWidget * central = new QWidget();
setCentralWidget(central);
centralWidget()->setLayout(layout);
layout->addWidget(chartView, 0, 0);
clock = 0;
SerialPortReader *reader = new SerialPortReader(this);
connect(reader, SIGNAL(onReadValue(int)), this, SLOT(onReadValue(int)));
reader->run();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onReadValue(int value){
++clock;
series->append(clock + 30, value);
chart->axes(Qt::Horizontal).back()->setRange(0 + clock, 100 + clock);
}
SerialPortReader.cpp
#include "serialportreader.h"
#include "mainwindow.h"
#include <QtCore>
#include <QRandomGenerator>
#include <QDebug>
SerialPortReader::SerialPortReader(QObject *parent) : QThread(parent)
{
this->parent = parent;
this->randomGenerator = QRandomGenerator::global();
}
void SerialPortReader::run() {
QTimer *timer = new QTimer(this);
timer->start(100);
connect(timer, SIGNAL(timeout()), this, SLOT(readValue()));
}
void SerialPortReader::readValue() {
int value = randomGenerator->bounded(-10, 10);
emit onReadValue(value);
}
I was just wondering if anyone has any suggestion to what could be wrong? or if there is a anything i can do, except for changing chart-lib.
After tinkering around i found out that the culprit wasn't actually a memory leak it was the:
chartView->setRenderHint(QPainter::Antialiasing);
As more and more data was presented the slower it got to do all the Antialiasing.
When i removed this everything suddenly went very smooth.
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 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: