The question about QMediaPlayer playback MP4 program exits abnormally - c++

When using QMedaiPlayer in QT to play MP4 files, the build passes normally, and the program quits abnormally during runtime, which has troubled me for many days. I hope everyone can help!
There is no problem with the build, and it exits abnormally during runtime.
The replacement of a smaller MP4 file still does not work, so it is not a matter of MP4 file size.
The use of MKV files will not work, nor is it a file format issue.
The version is QT 5.12.8
This is the mainwindow.cpp file
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow){
this->setFixedSize(1024,576);
ui->setupUi(this);
QHBoxLayout *m_layout= new QHBoxLayout(this);
QMediaPlayer *m_player = new QMediaPlayer(this);
m_player->setMedia(QUrl::fromLocalFile(QString::fromLocal8Bit("D:/try.mkv")));
m_videoW = new QVideoWidget(this);
m_layout->addWidget(m_videoW);
this->setLayout(m_layout);
m_player->setVideoOutput(m_videoW);
m_videoW->show();
m_player->play();
qDebug()<<"hello";
}
void MainWindow::paintEvent(QPaintEvent *){
QPainter painter(this);
painter.drawPixmap(0,0,width(),height(), m_videoW->currentPixmap());
}
This is the mainwindow.h file
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
void paintEvent(QPaintEvent*);
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QVideoWidget *m_videoW;
};
The corresponding library is also added to the pro file:
QT += core gui
QT += multimedia
QT += multimediawidgets
I never found where the problem is, please help me find it! Thank you!
Test with qDebug, hello can output normally.
*The code has been displayed according to the principle of least recurrence.

Related

Using a Slot to Create a Dialog

I need to create a new window over top of my current program (I'm attempting a dialog in this case) when a button is clicked. I have this working, but I am having trouble editing the content of the Dialog. As far as I'm aware, no .ui file is created for this and it is only being created when I connect the button to the function.
I have tried using a QMessageBox but was unable to resize the window how I needed. And still ran into the issue of adding the other elements.
void MyNameSpace::openInfoDialog()
{
QDialog* Dialog = new QDialog(this);
Dialog->setWindowTitle("View Stuff");
Dialog->setMinimumSize(500,250);
Dialog->adjustSize();
DialogRunner* msgRunner = new DialogRunner(Dialog, this);
msgRunner->safeExec();
}
This is my connect
connect(_Widget.InfoBtn, SIGNAL(clicked(bool)), this, SLOT(openInfoDialog()));
This code does produce a dialog on click, but I need to be able to add things like labels etc to it. I also use QT Designer as my WYSIWYG.
What can I do to create the new window from a button click and have it filled with other text etc ?
I have this working, but I am having trouble editing the content of
the Dialog
You can add ui files in your project:
Using that option Qt Creator will make a class with cpp and h files and a ui file in which you can add other widgets as you're used to.
use the import directives to use your class where you need it like you did in the code above and you will have your ui file available.
Then connect the clicked signal of your button with the slot of your dialog class. You can do that in the constructor of the class that holds the button.
You can read this approach more in detail in the book C++ GUI Programming
with Qt 4
on chapter 2 : Creating Dialogs.
It is available for free on the web. It uses Qt4, but in Qt5 it works the same way around.
EDIT: Here is a minimal example that shows you where you can use the needed parts: a mainwindow with a button on it (in the ui file). a Dialog class which also has a ui file (with several widgets on it). When clicking the button on the mainwindow, the dialog form is shown. Thjs is what I asked you for before. It makes it easier to communicate / test.
pro file
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test
TEMPLATE = app
SOURCES += \
dialog1.cpp \
main.cpp \
mainwindow.cpp
HEADERS += \
dialog1.h \
mainwindow.h
FORMS += \
dialog1.ui \
mainwindow.ui
**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
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
**mainwindow.cpp**
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog1.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
Dialog1* dialog = new Dialog1(this);
connect( ui->pushButton_1, SIGNAL(clicked()), dialog, SLOT(show()));
}
MainWindow::~MainWindow()
{
delete ui;
}
dialog1.h
namespace Ui {
class Dialog1;
}
class Dialog1 : public QDialog
{
Q_OBJECT
public:
explicit Dialog1(QWidget *parent = nullptr);
~Dialog1();
private:
Ui::Dialog1 *ui;
};
#endif // DIALOG1_H
dialog1.cpp
#include "dialog1.h"
#include "ui_dialog1.h"
Dialog1::Dialog1(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog1)
{
ui->setupUi(this);
}
Dialog1::~Dialog1()
{
delete ui;
}

cannot inherit from web page class without runtime error

I have a Qt(QObject) class that, when declared, crash the application either right after initilization or few seconds after GUI show up.
The class is exactly this:
webPage.h
class webPage : public QWebPage
{
Q_OBJECT
public:
webPage(QObject *parent = 0);
~webPage();
};
webPage.cpp
webPage::webPage(QObject *parent)
: QWebPage(parent)
{
qDebug() << "webPage::webPage() got called!";
}
webPage::~webPage()
{
}
And my mainwindow.h class:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void closeEvent(QCloseEvent *e);
private slots:
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
public:
Ui::MainWindow *ui;
browserControl webControl;
webPage page; // <-- unless I remove this, the application crashs.
};
The constructor is like this:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//webControl.setPage(&page);
}
Can someone help me to point out the error reason? it isn't even SEGFAULT or something, start in debug mode doesn't helps, the application crashs/frooze.
Exactly from webkitwidgets documentation:
Building the Qt WebKit module with debugging symbols is problematic on many platforms due to the size of the WebKit engine. We recommend building the module only in release mode for embedded platforms. Currently Qt WebKit will always be compiled without debugging symbols when using gcc. Take a look at Tools/mkspecs/features/production_build.prf if you need to change this.
As corresponded in this BUG QTBUG-44108 ... Qt WebEngine is only available in the MSVS 2013 packages on Windows. MinGW and previous Visual Studio versions do not work at the moment, so you'll need Visual Studio 2013 or Visual Studio 2013 Express Edition
Also, some experienced recommend doing Porting from Qt WebKit to Qt WebEngine

Update MainWindow Qt from a non member function

I am doing an Application on Qt for my Internship and I have a problem.
The purpose of my Application is that I have a card connected by USB to my computer that sends CAN message.
I made an application in C++ on visual studio that received those CAN messages and printed them on the console with the help of a callback.
What I want to do know is to do the same thing but with a GUI application on Qt using the functions that I previously made (I didn't used classes in my visual studio project).
Iadded all my .cpp and .h files from my visual studio project into my QT project
So here is mainwindow.h file where I didn't changed anything. I just added a textLabel on my form.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
The main.cpp is very basic.
MainWindow.cpp :
Here is the constructor when I open the communication
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Function created in Library.
open_com(...)
}
open_com opens the communication and call the callback each time a message is received
Definition of the callback
void callback (message id )
{
Here I want to call my TextLabel from my application
}
And my problem in this application is I don't know how to access my application from a function that is not part of the MainWindow class.
If someone already had this kind of problem and has the solution I would be very gratefull !
Thank you.

Qt Designer: Edit other window than mainwindow

I have a project which provides the user with a GUI via Qt. I designed it with the Qt Designer (integrated in the Qt Creator) and now I would like to add another window in order to let the user change settings.
Afaik I have to use a QWidget to create another window and now I'm wondering how I may edit this QWidget in Qt Designer because I am only able to design mainwindow.
My code looks like this:
mainwindow.hpp
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
// various slot calls
// action triggered when clicking an entry in the QMenu of mainwindow
void on_action_dummy();
private:
Ui::MainWindow *ui;
QWidget dummy;
};
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
// various implementations of the slot calls in mainwindow.hpp
void MainWindow::on_action_dummy()
{
dummy.show();
}
Maybe I need a own class for my new window? Is QWindow even the right class for this task?
You need to add a new UI file as well as header/class. There's an option for this in the "New File" dialog in Qt Creator (Qt Designer Form Class under the "Qt" sub-category on the sidebar). Then you open that up and instantiate the class like MainWindow in your program's entry point (int main()). Something like:
MySettingsDialog *dialog = new MySettingsDialog(this);
dialog->show();
You need to be careful how you instantiate it--mainly making sure the object will survive when it leaves the current scope (e.g. using a pointer if you are calling this in a method inside your class). Also, how you show/exec your dialog can vary. This is usually the case when you want a blocking (modal) dialog instead of a new "window".
Edit: To handle the memory management, you can set the WA_DeleteOnClose attribute:
dialog->setAttribute(Qt::WA_DeleteOnClose);

Qt WindowMaximize not changing geometry (C++)

So I'm making a web browser as my first Qt project (surprise!) and I'm wondering why calling setWindowState(Qt::WindowMaximized) is not changing window geometry. I have this code:
From mainwindow.h:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
private:
Ui::MainWindow *ui;
};
From mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
// this->geometry() is the same here...
setWindowState(Qt::WindowMaximized);
ui->webView->setGeometry(0, 60, geometry().width(), geometry().height()-60);
// ...as it is here.
}
As you may be able to tell, I'm trying to start the application with the window maximized and the QWebView also maximized. Basically, whenever the main window is resized, I also want to call ui->webView->setGeometry with the update height and width. But MainWindow::geometry doesn't seem to be updating. What am I doing wrong?
I would have to double check, but your geometry might not get updated properly until your main window gets a show event.
However, I would suggest you put your QWebView inside of a layout instead of trying to size it manually every time your main window changes size.