Cant load music file with QMediaPlayer - c++

I am creating media player with qt5. Code below works fine ( it open file dialog, load one song and play it after loading) but it can't play some MP3 files. These files are not damaged or something like this, I can open them with other media players. I noticed that the most of them are larger than 20 mB. Please help me if you find the solution.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QFileDialog>
#include <QDebug>
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&player, &QMediaPlayer::mediaStatusChanged,
this, [&](QMediaPlayer::MediaStatus status){
if(status == QMediaPlayer::LoadedMedia) //play if loaded
player.play();
});
connect(&player , QOverload<QMediaPlayer::Error>::of(&QMediaPlayer::error), // notify about errors
[=](QMediaPlayer::Error error){
if (error==QMediaPlayer::ResourceError)
qDebug("resource");
if (error==QMediaPlayer::FormatError)
qDebug("format");
if (error==QMediaPlayer::AccessDeniedError)
qDebug("acces");
if (error==QMediaPlayer::ServiceMissingError)
qDebug("service");
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{ QString file= QFileDialog::getOpenFileName(); //select file
player.setMedia(QUrl::fromLocalFile(file)); // load
}

Related

Qt - How to find children right

I'm trying to find a widget that I added to the screen and do some manipulations with it, but the list of children is empty every time. What am I doing wrong?
Here is the code (to run it, create a new clean project and replace the text in MainWindow.cpp):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QLabel>
#include <QLayout>
#include <QList>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QLabel* wid = new QLabel("hello");
ui->centralWidget->layout()->addWidget(wid);
QList<QLabel*> list = ui->centralWidget->findChildren<QLabel*>();
qDebug() << list.isEmpty();
}
MainWindow::~MainWindow()
{
delete ui;
}

QFileDialog not opening from Dialog window, but it does open from MainWindow

I'm trying to open the file explorer dialog from a dialog window using Qt and C++. My "dialog.cpp" file looks like this:
#include "dialog.h"
#include "ui_dialog.h"
#include "QFileDialog"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_Browse_clicked()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "C://", "All files (*.*)");
}
Everywhere I have looked says that this should work without a problem. So I tried putting the same exact code in my "mainwindow.cpp" file. Which is this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_dialog.h"
#include "QFileDialog"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_Open_dialog_clicked()
{
dialogUi.setupUi(dialog);
dialog->show();
}
void MainWindow::on_pushButton_Save_clicked()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "C://", "All Files (*.*)");
}
This opens up the file explorer dialog with no issues. However, I want to open the file explorer from the dialog window that is opened by the main window, which isn't working even though it's the same exact code. I even added a new MainWindow class ("mainwindow2.cpp") and tried from that to see if maybe it was the Dialog class causing an issue, but the file explorer wouldn't open from that either. It only seems to work from the primary MainWindow class. (By this I mean any window opened from "mainwindow.cpp" cannot open a file explorer dialog, but "mainwindow.cpp" can open the explorer)

sending a dbus message from shell to qt app

I want to do dbus-send from shell/console to a qt application.
This is the code for a simple QT app
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QtDBus>
#include <QDBusConnection>
#include <QDebug>
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
bool ret = QDBusConnection::sessionBus().connect(
"MyHome", //service
"/", //path
"com.mydomain.mcp", //interface
"usb", //name
this, //receiver
SLOT(messageSlot(QString)));
}
void MainWindow::messageSlot(const QString &t1)
{
qDebug("%s", QString("%1").arg(t1).toUtf8().data());
}
From the terminal, I and sending this command
dbus-send --session --print-reply --reply-timeout=2000 --type=method_call / com.mydomain.mcp.usb string:'a'
I get this error: Method "usb" with signature "s" on interface "com.mydomain.mcp" doesn't exist
What am I doing wrong?
Thanks

how to play a .wav file using QSound in qt5

I tried using QSound in my project but I am getting an undefined refrence to _imp_ZN6Q:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtMultimedia/QSound>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
ui->setupUi(this);
QSound player;
QSound::play("E:\songs");
}
MainWindow::~MainWindow()
{
delete ui;
}
In your project file (.pro) add
QT += multimedia
QT += multimedia
and add :
#include <QtMultimedia/QSound>
QSound newMessage("sound/newChat.wave");
newMessage.play();

Controlling Multiple UI files in Qt framework

Question asked twice: see Handling multiple ui files in Qt
I am new to Qt framwork , i have been given this simple task:
In the MainWindow , i have a submit button , once its clicked another total different window should appear
i thought of doing this by doing one extra UI file called From.ui file and to switch from MainWindow to Form once submit is clicked , this is my code:
//main.cpp
#include "mainwindow.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.setOrientation(MainWindow::ScreenOrientationAuto);
mainWindow.showExpanded();
return app.exec();
}
//MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
#include <QtCore/QCoreApplication>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow:: SubmitClicked()
{
Form* f= new Form(this);
f->show();
f->raise();
f->activateWindow();
}
//Form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
this code compiled perfectly , but its not doing as expected , once submit is clicked , nothing is done...
can you please tell me whats wrong ?
It seems that SubmitClicked slot is not connected to clicked event of your button
Put a cout/printf at the top of your SubmitClicked method to make sure that it is called.