Cannot play video in Qt - c++

Given below is the code for playing a video. On running it, it says that it could not open file.
#include <QApplication>
#include <QtMultimediaWidgets/QVideoWidget>
#include <QtMultimedia/QMediaPlayer>
#include <QtMultimedia/QMediaPlaylist>
#include <QFile>
#include <QHBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *widget=new QWidget;
widget->resize(400,300);
QMediaPlayer *player=new QMediaPlayer;
QVideoWidget *vw= new QVideoWidget;
QHBoxLayout *layout=new QHBoxLayout;
layout->addWidget(vw);
widget->setLayout(layout);
player->setVideoOutput(vw);
QFile file=("1minute.mp4");
if(!file.open(QIODevice::ReadOnly))
qDebug()<<"Could not open file";
player->setMedia(QUrl::fromLocalFile("1minute.mp4"));
player->play();
widget->show();
qDebug()<<player->availableMetaData()<<player->currentMedia().canonicalUrl();
return a.exec();
}
Where am I going wrong?

Finally found the answer. Added the complete link of the video and it started playing easily.
SO the code for the video link should be:
player->setMedia(QUrl::fromLocalFile("C:/Users/Administrator/Desktop/1minute.mp4"));

Related

QDialog expanding base on the QLabel content

I'd like to show and then close a dialog after 5 seconds. The dialog needs to be automatically resized (horizontally and vertically) based on the content of a label. Here is my code:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(&notify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.adjustSize();
QTimer::singleShot(intTime, &notify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
It does not not expand the dialog based on the label size. Here is how it looks:
How can I fix it? (Please also let me know if there is better way of doing this.)
I am using Qt5 in Linux.
Since you have not used a QLayout the QLabel will be displayed as large as you can, a possible request is to change the size of QDialog to the recommended size of QLabel with sizeHint():
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(&notify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.resize(lbl->sizeHint());
QTimer::singleShot(intTime, &notify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
The other possible solution is to use a QLayout:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
#include <QVBoxLayout>
void notify (int intTime=1000)
{
QDialog notify;
QVBoxLayout *lay = new QVBoxLayout(&notify);
//notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel;
lay->addWidget(lbl);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
QTimer::singleShot(intTime, &notify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}

QtMultimedia Video Play example

I have Qt5 built with submodule QtMultimedia on Debian Jessie 8.2 (BeagleBone Black), I use GStreamer backend (version 0.10.36). I tried to use this simple example to play a video, but it gives me error when trying to play .AVI file: Internal data flow error.
Is the problem in video plugins/codecs I have?
Here is the code:
#include "mainwindow.h"
#include <QApplication>
#include <QMediaPlayer>
#include <QVideoWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMediaPlayer *player = new QMediaPlayer;
QVideoWidget *vw = new QVideoWidget;
player->setVideoOutput(vw);
player->setMedia(QUrl::fromLocalFile("/home/debian/video.avi"));
vw->setGeometry(100, 100, 320, 240);
vw->show();
player->play();
return a.exec();
}

Why does QtGUI not include all GUI elements in Qt 5

I watched VoidRealm tutorial, and he easily include QtGui and start using it! but i do the same thing and it doesnt work for me! for example my code doesnt know the QWidget until i include QLabel! or all other Gui element...
#include <QApplication>
#include <QtGui>
#include <QtCore>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *win = new QWidget;
win->setWindowTitle("MBS");
QGridLayout *gLay = new QGridLayout;
QLabel *label1 = new QLabel("Name: ");
win->show();
return a.exec();
}
In Qt5 most of the previously known as QtGui functionality now is being called QtWidgets.
So try to write #include <QtWidgets>.

Play a Live video Stream using Qt

Given below is the code for playing a video file using Qt. Instead of playing the video I want to play a live video stream from an IP Camera. Another approach is to embed the VLC Player in Qt and a link for the project is provided here. The problem is I do not know how to include the player in Qt. So how do I proceed?
#include <QApplication>
#include <QtMultimediaWidgets/QVideoWidget>
#include <QtMultimedia/QMediaPlayer>
#include <QtMultimedia/QMediaPlaylist>
#include <QFile>
#include <QHBoxLayout>
#include "DemoPlayer.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *widget=new QWidget;
widget->resize(400,300);
QMediaPlayer *player=new QMediaPlayer;
QVideoWidget *vw= new QVideoWidget;
QHBoxLayout *layout=new QHBoxLayout;
layout->addWidget(vw);
widget->setLayout(layout);
player->setVideoOutput(vw);
player->setMedia(QUrl::fromLocalFile("C:/Users/Administrator/Desktop/1minute.mp4"));
player->play();
widget->show();
qDebug()<<player->availableMetaData()<<player->currentMedia().canonicalUrl();
return a.exec();
}
follow this code . you can embed a widget inside another widget using a valid window id .
How to show output video of other application in Qt?
you can use the qx11embedwidget and qx11embedwidgetcontainer
QX11EmbedWidget and QX11EmbedContainer

Error in Qt program

This code
#include <QtWidgets/QApplication>
#include <QLabel>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QLabel *label = new QLabel("my first app");
label->show();
return app.exec();
}
Causing an error:
QLabel: there is no such directory
I am using Qt 5.0.1 in Windows
change
#include <QLabel>
to #include <QtWidgets/QLabel>
This is where QLabel actually resides (if this is that QLabel that you want)
Put QT += widgets in your .pro file.