I try to play movie with Qt5 on OS X El Capitan v10.11.6.
I use QMediaPlayer, QMediaPlaylist, and QVideoWidget to play.
Write source code as same as Qt's documentation, but it show only black window and not play any movie.
Here is my source code.
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow mainwindow;
mainwindow.show();
return app.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
class QMediaPlayer;
class QMediaPlaylist;
class QVideoWidget;
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget* parent = 0);
private:
QMediaPlayer* player;
QMediaPlaylist* playlist;
QVideoWidget* videoWidget;
};
#endif
mainwindow.cpp
#include <QtWidgets>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QVideoWidget>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget* parent)
: QWidget(parent)
{
player = new QMediaPlayer;
playlist = new QMediaPlaylist;
videoWidget = new QVideoWidget;
player->setPlaylist(playlist);
player->setVideoOutput(videoWidget);
playlist->addMedia(QUrl::fromLocalFile("box.mp4"));
videoWidget->show();
playlist->setCurrentIndex(1);
player->play();
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addWidget(videoWidget);
setLayout(mainLayout);
}
I check "box.mp4" exists in the same directory.
Where is problem? How should I fix source code to solve this problem?
Just modify media file path to full path in mainwindow.cpp.
Before
playlist->addMedia(QUrl::fromLocalFile("box.mp4"));
After
playlist->addMedia(QUrl::fromLocalFile("/path/to/box.mp4"));
Related
In my sample application, the MainWindow closes instead of the dialog window. Why is that and how do I change it?
I've tried setQuitOnLastWindowClosed() but it didn't have any effect.
When clicking the x in the top right corner, the dialog window closes and the MainWindow remains open as expected. But that's of course not what I want.
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDialog>
#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
void on_mainWindowBtn();
void on_closeBtn();
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
};
#endif // MAINWINDOW_H
MainWindow.cpp:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget *mainWindowWidget = new QWidget(this);
QHBoxLayout *mainWindowHBox = new QHBoxLayout();
QPushButton *mainWindowPushButton = new QPushButton();
mainWindowPushButton->setText("Open Dialog Window");
mainWindowHBox->addWidget(mainWindowPushButton);
mainWindowWidget->setLayout(mainWindowHBox);
this->setCentralWidget(mainWindowWidget);
connect(mainWindowPushButton, &QPushButton::released, this, &MainWindow::on_mainWindowBtn);
}
void MainWindow::on_mainWindowBtn()
{
QDialog *newDialog = new QDialog();
QHBoxLayout *newHBox = new QHBoxLayout();
QPushButton *closeBtn = new QPushButton();
closeBtn->setText("Close Dialog Window");
newHBox->addWidget(closeBtn);
newDialog->setLayout(newHBox);
newDialog->setModal(true);
connect(closeBtn, &QPushButton::released, this, &MainWindow::on_closeBtn);
newDialog->exec();
}
void MainWindow::on_closeBtn()
{
close(); // closes the MainWindow but not the dialog window
}
MainWindow::~MainWindow()
{
}
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(false);
MainWindow w;
w.show();
return a.exec();
}
In this code which can able to play video and play *.mp3. code works properly,in my mainwindow.ui I added widget called widgetGif by drag and drop .This widget containing label also. but this widget not visible when I run the program. How can I display this widget called widgetGif
here is part of my code :
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMovie>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
player = new QMediaPlayer(this);
vw = new QVideoWidget (this);
player->setVideoOutput(vw);
this->setCentralWidget(vw); //I think this is the reason
slider = new QSlider(this);
bar = new QProgressBar(this);
slider->setOrientation(Qt::Horizontal);
ui->statusBar->addPermanentWidget(slider);
ui->statusBar->addPermanentWidget(bar);
connect(player,&QMediaPlayer::durationChanged,slider,&QSlider::setMaximum);
connect(player,&QMediaPlayer::positionChanged,slider,&QSlider::setValue);
connect(slider,&QSlider::sliderMoved,player,&QMediaPlayer::setPosition);
connect(player,&QMediaPlayer::durationChanged,bar,&QProgressBar::setMaximum);
connect(player,&QMediaPlayer::positionChanged,bar,&QProgressBar::setValue);
sliderVolumn = new QSlider(this);
sliderVolumn->setOrientation(Qt::Horizontal);
ui->statusBar->addPermanentWidget(sliderVolumn);
connect(sliderVolumn,&QSlider::sliderMoved,player,&QMediaPlayer::setVolume);
QMovie *movie=new QMovie(":/res/icons/giphy.gif");
if (!movie->isValid())
{
// Something went wrong :(
}
ui->labelGif->setMovie(movie);
movie->start();
ui->widgetGif->setVisible(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
QString filename= QFileDialog::getOpenFileName(this,"Open Folder","","Open a File(*.*)");
on_actionStop_triggered();
player->setMedia(QUrl::fromLocalFile(filename));
on_actionPlay_triggered();
if(filename.endsWith(".mp3")){
qDebug() << " file is mp3";
}else{
qDebug() << " not is mp3";
}
}
QMainWindow needs a centralWidget. You must use layouts to include your QVideoWidget and also your widgetGif and then set it as centralWidget of the QMainWindow.
In the following example, textEdit would be your QVideoWidget and the object label is like your widgetGif.
We have also two buttons to hide or show label using the method setVisible.
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class QLabel;
class QPushButton;
class QTextEdit;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QWidget *centralWidget;
QLabel *label;
QPushButton *pushButton;
QPushButton *pushButton_2;
QTextEdit *textEdit;
public slots:
void showSlot();
void hideSlot();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTextEdit>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Vertical layout
QVBoxLayout *mainLayout = new QVBoxLayout;
// Widgets
centralWidget = new QWidget(this);
label = new QLabel();
pushButton = new QPushButton();
pushButton_2 = new QPushButton();
textEdit = new QTextEdit();
// Title and texts
this->setWindowTitle("MainWindow");
label->setText("TextLabel");
pushButton->setText("Show");
pushButton_2->setText("Hide");
// Add widgets to layout
mainLayout->addWidget(textEdit);
mainLayout->addWidget(label);
mainLayout->addWidget(pushButton);
mainLayout->addWidget(pushButton_2);
centralWidget->setLayout(mainLayout);
// Set the central widget
this->setCentralWidget(centralWidget);
connect(pushButton, SIGNAL (clicked()), this, SLOT (showSlot()));
connect(pushButton_2, SIGNAL (clicked()), this, SLOT (hideSlot()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showSlot()
{
label->setVisible(true);
}
void MainWindow::hideSlot()
{
label->setVisible(false);
}
Alright so is there any way to make this program randomly change the variables x and y every time the button is clicked i am new to programming...
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtGUI>
#include <QWidget>
#include <cstdlib>
#include <ctime>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new QWidget;
srand(time(0));
int x = 1+(rand()%900);
int y = 1+(rand()%400);
QPushButton *MainInter = new QPushButton("Push me!",window);
QPropertyAnimation *animation = new QPropertyAnimation(MainInter, "pos");
animation->setDuration(0);
animation->setEndValue(QPoint(x,y));
Object::connect(MainInter,SIGNAL(released()),animation,SLOT(start()));
window->resize(900,500);
window->show();
return a.exec();
}
What you can do is, instead of connecting the released() signal of your button directly to your animations start() SLOT, you would create your own custom SLOT. Then you connect the button to it, handle the action, and call the animation.
First read up on how to create a custom QWidget, instead of creating top level object in your main(). Simple example here
A custom widget might look like this:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class QPushButton;
class QPropertyAnimation;
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
private:
QPushButton *button;
QPropertyAnimation *animation;
public slots:
void randomizeAnim();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <QPushButton>
#include <QPropertyAnimation>
#include <ctime>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
button = new QPushButton("Push me!", this);
animation = new QPropertyAnimation(button, "pos");
animation->setDuration(0);
QObject::connect(button, SIGNAL(released()), this, SLOT(randomizeAnim()));
}
void MyWidget::randomizeAnim()
{
srand(time(0));
int x = 1+(rand()%900);
int y = 1+(rand()%400);
animation->setEndValue(QPoint(x,y));
animation->start();
}
And now your main.cpp can be reduced to the boilerplate code:
#include <QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new MyWidget;
window->resize(900,500);
window->show();
return a.exec();
}
Every time you click, your custom slot will handle the action and do the animation.
I am a QT newbie and trying to play around with Apps. I have just coded a very trivial App with some buttons. The main idea is to have a small "logo" in my App. LAter I would like to add some background image as well.
I have coded from an example App with a grid layout within which is a QBoxLayout which groups my buttons.
As you can see in my code, I have tried adding the Logo everywhere. When I added it in main.cpp, I have two views one showing buttons and the other my logo. Of course I do not want this. So I tried adding it in mainwindow.cpp but in this case, I don't see my Logo appearing anywhere at all :(
Please advise.
Here is the code:
main.cpp:
#include <QtGui/QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window window;
window.show();
/*
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsPixmapItem item(QPixmap("/home/marc/Desktop/Niranjana/Images/logo.9.png"));
scene.addItem(&item);
view.show();
*/
return a.exec();
}
mainwindow.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
#include <QRadioButton>
class QGroupBox;
class Window : public QWidget
{
Q_OBJECT
public:
Window(QWidget *parent = 0);
void onCheck_remote(int flag);
void onCheck_local(int flag);
private:
QRadioButton *button_local;
QRadioButton *button_remote;
QGroupBox *createPushButtonGroup();
};
#endif
mainwindow.cpp
#include <QtGui>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include "mainwindow.h"
Window::Window(QWidget *parent)
: QWidget(parent)
{
QGridLayout *grid = new QGridLayout;
QGraphicsScene scene;
QGraphicsPixmapItem item(QPixmap("/home/test/logo.png"));
QGraphicsView view(&scene);
scene.addItem(&item);
view.show();
grid->addWidget(view.viewport(), 1, 1);
grid->addWidget(createPushButtonGroup(), 2, 1);
setLayout(grid);
setWindowTitle(tr("My App"));
resize(480, 420);
}
QGroupBox *Window::createPushButtonGroup()
{
QGroupBox *groupBox = new QGroupBox();
/*
QGraphicsScene scene;
QGraphicsPixmapItem item(QPixmap("/home/marc/Desktop/Niranjana/Images/logo.9.png"));
QGraphicsView view(&scene);
scene.addItem(&item);
scene.setBackgroundBrush(Qt::white);
view.show();
*/
QPushButton *button1 = new QPushButton(tr("&Start"));
QPushButton *button2 = new QPushButton(tr("&Stop"));
button_local = new QRadioButton(tr("&with power"));
button_remote = new QRadioButton(tr("without power"));
button_local->setChecked(1);
QVBoxLayout *vbox = new QVBoxLayout;
// vbox->addSpacing(10);
// vbox->addWidget(view.viewport());
//vbox->addSpacing(10);
vbox->addWidget(button1);
vbox->addSpacing(10);
vbox->addWidget(button2);
vbox->addSpacing(50);
vbox->addWidget(button_local);
vbox->addWidget(button_remote);
vbox->addStretch(1);
groupBox->setLayout(vbox);
return groupBox;
}
You should
Make your scene, view, item pointer members of your window class
Instantiate them in your Window ctor
do grid->addWidget(view, 1, 1);
Otherwise, all the items instantiated on the stack in the ctor will get deleted when exiting.
I am getting an error while trying to run this Application ... the error message is:
main.cpp(11): error: expression must have class type
int r = dialog.exec(); and I am not sure why!!!
I am using qmake to generate the make file... I have added the necessary files to the *.pro file since Dialog is inherited from QDialog I should have access to the function exec!
#include <QtGui>
#include <QDialog>
#include <QtUtil.h>
#include <Mathematics.h>
#include <Pair.h>
#include "View.h"
class QMesseageBox;
class QAction;
class QDialogButtonBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QTextEdit;
class Dialog : public QDialog {
Q_OBJECT
public:
Dialog() {
QHBoxLayout *layout = new QHBoxLayout;
// prevent left vertical box from growing when main window resized
layout->addStretch(1);
QLabel* lab_Layers = new QLabel(tr("Layers"));
d_inline = new QLineEdit;
d_inline->setText("50");
scene = new QGraphicsScene(0, 0, 500, 500);
view = new View;
layout->addWidget(view);
view->setScene(scene);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(layout);
setLayout(mainLayout);
setWindowTitle(tr("VI Smooth 0.4"));
}
private slots:
// scroll the "after" window when "before" one is scrolled (so they
// remain in sync)
private:
QAction* exitAction;
QtUtil qt;
QLineEdit* d_inline;
QGraphicsScene* scene;
QGraphicsView* view;
};
main class
#include <QApplication>
#include <QMessageBox>
#include "Dialog.h"
int
main(int argc, char **argv) {
QApplication app(argc, argv);
argv++;
Dialog dialog();
// dialog.showMaximized();
int r = dialog.exec();
return 0;
}
It should look something like this. If you create a Dialog object, you need to call show(). And you also need to return app.exec() in main().
#include <QApplication>
#include <QMessageBox>
#include "Dialog.h"
int
main(int argc, char **argv) {
QApplication app(argc, argv);
argv++;
Dialog dialog;
dialog.show()
return app.exec(argc, argv);
}