QMediaPlayer - Playing BGM Continuously in Qt - qt

I am new to Qt. I want to make a game with many different screens (e.g. Main Screen, Loading Screen...), but in a single window, switched by index in a QstackWidget. When switching to the Main Screen, it should play a background music continuously. Searching the web, I found QMediaPlayer with QMediaPlayerlist can do the trick. So I tried like this (using a basic QMainWindow to simplify the code):
QtWidgetApplication1.h
#pragma once
#include <QtWidgets/QMainWindow>
#include <QMediaPlayer>
#include <QMediaPlaylist>
class QtWidgetsApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtWidgetsApplication1(QWidget *parent = Q_NULLPTR);
void playAudio();
void stopAudio();
private:
QMediaPlayer* player;
QMediaPlaylist* playList;
};
QtWidgetApplication1.cpp
#include "QtWidgetsApplication1.h"
QtWidgetsApplication1::QtWidgetsApplication1(QWidget* parent)
: QMainWindow(parent)
{
playAudio();
}
void QtWidgetsApplication1::playAudio() {
player = new QMediaPlayer();
playList = new QMediaPlaylist();
playList->addMedia(QUrl::fromLocalFile("main_theme.mp3"));
playList->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
player->setPlaylist(playList);
player->play();
}
void QtWidgetsApplication1::stopAudio() {
player->stop();
delete player;
delete playList;
player = Q_NULLPTR;
playList = Q_NULLPTR;
}
main.cpp
#include "QtWidgetsApplication1.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtWidgetsApplication1 w;
w.show();
return a.exec();
}
Error:
It only plays the audio once, and shows an error message
QObject::startTimer: Timers can only be used with threads started with
QThread
Note:
I've tried (1.) using QMediaPlayer only and detecting QMediaPlayer::endOfMedia signal, (2.) QSound with a .wav file. First approach cannot get QMediaPlayer::endOfMedia signal but get QMediaPlayer::pauseState signal, however, I still fail to replay the audio. Second approach doesn't even play the audio and shows a QEventLoop error.
Enviroment:
Windows 10
Qt 5.15.2 MSVC2019 X64
Visual Studio 2019

Instead of adding QT += multimedia in the .pro file, which the projects in VS doesn't have, I tried to include those .lib to the project settings (Refer to How to add Qt libraries to visual studio). Actually, the best way to include QtMultiMedia is: go to
Extensions (in VS) > Qt VS tools > Qt Project Settings > General > Qt Modules and select the modules you want to include. By including the right modules, the problem I have faced has gone.

Related

Connect QML signal of arbitrary placed .qml-file to Qt slot

I am trying to connect QML signal to Qt slot. I have read that example and here is code
int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
qDebug()<<QUrl::fromLocalFile("main.qml");
QQuickView view(QUrl::fromLocalFile("main.qml"));//I should replace that line
QObject *item = view.rootObject();
MyClass myClass;
QObject::connect(item, SIGNAL(qmlSignal(QString)),
&myClass, SLOT(cppSlot(QString)));
view.show();
return app.exec();
}
It works fine, but the problem is that main.qml should be in the folder where .exe file is. So, every time I modify main.qml I should copy it to another foder. File path to main.qml is: "C:\Qt\projects\ConnectionsQT\main.qml". I have tried to replace line by
QQuickView view(QUrl("‪C:\Qt\projects\ConnectionsQT\main.qml");
and by
QQuickView view(QUrl("‪C:\\Qt\\projects\\ConnectionsQT\\main.qml");
but in that cases the program cannot find the main.qml file. What should I do?
The good practice is making your qml file part of the app executable by putting into .qrc resource file with Qt creator or by hand:
http://qt-project.org/doc/qt-5/qtquick-deployment.html#managing-resource-files-with-the-qt-resource-system
And the you can make use of it with something like:
QQuickView view(QUrl("qrc:///res/qml/main.qml"));

Qt Custom Slot QWebView

I'm fairly new to Qt and I'm trying to work with Signals and Slots and I'm having a bit of trouble creating custom Slots:
public slots:
void resetUrl(){
this->load(QUrl("http://www.google.com"));
}
(Then, in my main.cpp)
#include <QWebView>
#include <QPushButton>
QWebView *web = new QWebView(mainwindow);
QPushButton *button = new QPushButton(mainwindow);
web->load(QUrl("http://www.yahoo.com"));
button->setText("Google");
QObject::connect(button, SIGNAL(clicked()), web, SLOT(resetUrl()));
Thats about all I got, any help is appreciated. What it says when I try to run this is "'class google' has no member named 'load'".
I'm sure your class has no load function, you want web->load(url) NOT this->load(url)

QT GUI freezes when capturing video from webcam using OpenCV

I am using Opencv for some real-time video processing.
As a front-end I am using QT framework.
On my GUI, I have an input image window (mapped to a Label) and an output image window (mapped to another Label) and 3 push buttons. One to Start input video capture, the second to process the video (code not written yet), and third to Exit.
I am currently able to stream my video and display it on the Front-end. But this locks my GUI and am unable to Exit.
I tried using QTimers (using suggestions from this and the QT forum), but my GUI still remains locked.
Would appreciate if someone can point my in the right direction.
Below is the code:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp> // for cvtColor
#include <iostream>
#include <QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_buttonCaptureVideo_clicked();
void on_buttonExit_clicked();
public slots:
virtual void doNextFrame() {repaint();}
private:
Ui::MainWindow *ui;
CvCapture *capture; // OpenCV Video Capture Variable
IplImage *frame; // Variable to capture a frame of the input video
cv::Mat source_image; // Variable pointing to the same input frame
cv::Mat dest_image; // Variable to output a frame of the processed video
QTimer *imageTimer;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
cvReleaseImage(&frame);
cvReleaseCapture(&capture);
}
void MainWindow::on_buttonCaptureVideo_clicked()
{
// Set to 25 frames per second
const int imagePeriod = 1000/25; // ms
imageTimer = new QTimer(this);
imageTimer->setInterval(imagePeriod);
connect(imageTimer, SIGNAL(timeout()), this, SLOT(doNextFrame()));
// Use the default camera
capture = cvCreateCameraCapture(-1);
while(capture)
{
// Capture a frame
frame = cvQueryFrame(capture);
// Point to the same frame
source_image = frame;
// Resize Image
cv::resize(source_image, source_image, cv::Size(128,128) , 0, 0);
// Change to RGB format
cv::cvtColor(source_image,source_image,CV_BGR2RGB);
// Convert to QImage
QImage qimg = QImage((const unsigned char*) source_image.data, source_image.cols, source_image.rows, QImage::Format_RGB888); // convert to QImage
// Display on Input Label
ui->labelInputVideo->setPixmap(QPixmap::fromImage(qimg));
// Resize the label to fit the image
ui->labelInputVideo->resize(ui->labelInputVideo->pixmap()->size());
}
}
void MainWindow::on_buttonExit_clicked()
{
connect(ui->buttonExit, SIGNAL(clicked()), qApp, SLOT(closeAllWindows()));
}
When you click your button, the
while(capture) { ... }
loop will run forever, as capture will never be set to NULL.
This means the code flow never leaves your loop and thus the main thread cannot process anything else, like e.g. repainting.
The QTimer will emit its timeout() signals, but they will be placed as Events in Qt's Event Queue. As long as your on_buttonCaptureVideo_clicked() method is running, those Events will not be processed.
Here are my suggestions how to make it work:
This code:
// Set to 25 frames per second
const int imagePeriod = 1000/25; // ms
imageTimer = new QTimer(this);
imageTimer->setInterval(imagePeriod);
connect(imageTimer, SIGNAL(timeout()), this, SLOT(doNextFrame()));
// Use the default camera
capture = cvCreateCameraCapture(-1);
belongs into the constructor of MainWindow as you want to set that up one time only. There is no need to do it again when the user clicks the button the second, third, etc time.
The code which is within your while loop should go into the doNextFrame() slot (without the while construct).
Then your button will only do
imageTimer->start();
and then e.g. do
imageTimer->stop();
when it is clicked again.
Example code:
void MainWindow::on_buttonCaptureVideo_clicked()
{
if( imageTimer->isActive() )
{
imageTimer->stop();
}
else
{
imageTimer->start();
}
}
What will happen if you do that?
When you click the button, your on_buttonCaptureVideo_clicked() clicked slot will be called from the GUI thread, the timer will be started, and the method will return almost instantly.
Now the GUI thread is free and able to handle repaints etc.
From that point on, the timer will be sending timeout() signals every 40ms. Whenever the GUI thread is free, it will handle this signal and call your doNextFrame slot.
This slot will capture the next frame and return when it is done. When it is done, the GUI thread will be able to process other Events (e.g. repaint) again.
As soon as you click the button again, the timer will stop, and no new timeout() events will be sent. If you still see a couple of frames after the button has been clicked, this could mean that the timer events were sent faster than they could be processed.
Preface: I am not strong in C++ so I cannot offer specific code, but I am experienced in PyQt
This is a common pitfall for newcomers to Qt. What it seems your on_buttonCaptureVideo_clicked is doing is entering into a loop in your main GUI thread to do work. In QT, you want to avoid doing anything busy in your main thread. The Qt eventloop needs to be able to constantly process and flush your GUI events as they come in. What you are doing is blocking the event loop.
There are two different things you can do here. The first is the more basic approach but allows you to see more immediate results. You can "pump" the eventloop. Depending on how fast your while loop iterates, you can call qApp->processEvents();. This will allow Qt to process the pending GUI events and make your app appear more responsive. Its basically sharing time between your while loop and the main loop. Maybe you want to call this on every nth frame. Depends how often you want to make sure the GUI refreshes.
The other option, which is more preferable, is to place your capture loop into a QThread. When a new frame is available you can emit a signal with the frame data. The signal will get placed into the Qt event loop to be processed with everything else and will not hold up your GUI. Generally this is the approach you want to take with any heavy crunching or long running callables.
Edit
I just realized that you start a QTimer in addition to doing a loop in the main thread. If you want to use a QTimer, and your image processing is not too heavy (it doesn't take long per cycle) then you should move everything into the doNextFrame and completely remove the while loop. If your doNextFrame is a heavy process, then you should be using a QThread and signals.

Qt browser app with opengl-es2 strange behaviour (not working)

I have enabled opengl-es2 support in Qt/E and I wanted to make a browser app and the code is :
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsView g;
g.setScene(new QGraphicsScene(&g));
g.scene()->setItemIndexMethod(QGraphicsScene::NoIndex);
g.setAttribute(Qt::WA_DeleteOnClose);
g.setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
g.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
g.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
g.setAlignment(Qt::AlignTop | Qt::AlignHCenter);
g.setFrameStyle(QFrame::NoFrame);
g.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
g.setViewport(new QGLWidget());
g.showFullScreen();
QGraphicsWebView view;
view.load(QUrl("http://www.google.com"));
view.setGeometry(QRectF(0,0,800,400));
view.show();
g.scene()->addItem(&view);
a.exec();
}
I can see google page getting loaded for a fraction of second and then after it disappears.
Error log paste-bin link ==> http://pastebin.com/bgbQqd1M
Ashish,
What changes did you make for eglfs platform plug-in?
I also modified eglfs plugin to make it run on an arm board.
Two place that I changed are:
avoid call eglMakeCurrent twice, when EGLDisplay, EGLSurface(Read), EGLSurface(Draw), EGLDisplay not change --- On my board, call eglMakeCurrent twice will cause the program abort.
The problem is same as you (QGLShader::QGLShader: 'context' must be the current context or sharing with it.)
In QtOpengl library, there is a function QGLWidget* qt_gl_share_widget(), that will create a new QGLContext and set it to QPlatformGLContext.
In bool QGLShaderProgram::bind(), it will check the currentContext with the one in QGLSharedResourceGuard. QGLContext::areSharing(d->programGuard.context(), QGLContext::currentContext()).
To fix this problem.
I add the following code in qeglplatformcontext.cpp
#include <QGLContext>
class QEGLFSContext : public QGLContext
{
public:
bool chooseContext(const QGLContext* shareContext = 0)
{
QGLContext::chooseContext(shareContext); // in QGLContext, this guy is protected
}
};
void QEGLPlatformContext::makeCurrent()
{
QPlatformGLContext::makeCurrent();
QGLContext * ctx = QGLContext::fromPlatformGLContext(this);
QEGLFSContext* eglctx = (QEGLFSContext*)ctx;
static QEGLFSContext * s_ctx = eglctx;
if (s_ctx != eglctx)
{
s_ctx->chooseContext();
}
//...
}
After use these change, I can run hellogl_es2 and show the animation for show the Qt logo and bubbles well.
But I still have some problem:
QLabel, QMenu... can not show.
Do you have any idea about this problem.
I also got some idea from some guy, qws/simplegl also have these problem.

Qt quick project - how to invoke cpp function in qml file?

I' ve created quick project in Qt, selected from wizard at the begin, when Qt creator is started.
Qt creator create project. There are qmlapplicationvierwer subproject and qml files in main project. I want to add new cpp source and header files (MainMenu.cpp and MainMenu.h) to project and then invoke function from these files in main.qml file.
How I can register new type in qmlapplicationviewer and invoke function from ManiMenu ?
qmlapplicationvierwer has only few function:
QApplication app(argc, argv);
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/PUTest/main.qml"));
viewer.showExpanded();
return app.exec();
and:
viewer.addImportPath(const string &path);
Better way is not create project without project wizard ?
Thanks
Normally, you use this wizard to create QML only projects.
The QmlApplication viewer is just a lightweight C++ wrapper around your QML file so a binary is generated and the QML file is loaded.
There's not much magic to do that on your own, see:
https://doc.qt.io/archives/qt-4.7/qtbinding.html
#include <QApplication>
#include <QDeclarativeView>
#include <QDeclarativeContext>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDeclarativeView view;
QDeclarativeContext *context = view.rootContext();
context->setContextProperty("backgroundColor",
QColor(Qt::yellow));
view.setSource(QUrl::fromLocalFile("main.qml"));
view.show();
return app.exec();
}
And with setContextProperty you can ad global QML types via cpp...
Of course you can also reuse the QmlApplicationViewer. The Mainclass QmlApplicationViewer is derived from QDeclarativeView, so you directly have access to the context in this class.
So like in the example above, it should be possible to use:
QDeclarativeContext *context = this.rootContext();
context->setContextProperty("backgroundColor", QColor(Qt::yellow));
somewhere in the QmlApplicationViewer costructor or afterwards (didn't try it for now, let me know if it doesn't work).

Resources