Creating a progress bar for audio playback in Qt Widgets - c++

I'm currently working on a Qt Widget (using Qt 6.3) that plays audio and shows a progressbar. But I'm struggling to find a way to connect the audio progress value with the progress bar status.
Here's my code:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
mMediaPlayer = new QMediaPlayer(this);
audio_output = new QAudioOutput(this);
//this is the part I need help with
connect(mMediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
void MainWindow::on_Button_open_media_clicked()
{
QString file_name = QFileDialog::getOpenFileName(this, "Open");
if(file_name.isEmpty()){
return;
}
mMediaPlayer->setSource(QUrl::fromLocalFile(file_name));
mMediaPlayer->setAudioOutput(audio_output);
audio_output->setVolume(ui->volume_slider->value());
on_play_button_clicked();
}
void MainWindow::on_stop_button_clicked()
{
mMediaPlayer->stop();
}
void MainWindow::on_pause_button_clicked()
{
mMediaPlayer->pause();
}
void MainWindow::on_play_button_clicked()
{
audio_output->setVolume(100);
mMediaPlayer->play();
}
void MainWindow::on_mute_button_clicked()
{
if(ui->mute_button->text() == "Mute"){
audio_output->setMuted(true);
ui->mute_button->setText("Unmute");
}
else{
audio_output->setMuted(false);
ui->mute_button->setText("Mute");
}
}
void MainWindow::on_volume_slider_valueChanged(int value)
{
audio_output->setVolume(value);
}
}
I couldn't find a way to do it using the documentation. It might be simple, but I need help.
Any help is gladly appreciated.

Related

Pass variable from mainwindow to another qt c++ forms

I want to pass three strings (cellTitle,cellPoem,cellGroup) from mainwindows to another dialog. I know when i initiate the second form the values become zeros. I read somewhere it's possible with slots but i don't know how.
mainwindows.h
public:
QString cellTitle,cellPoem,cellGroup;
mainwindows.cpp
void MainWindow::on_tableView_pressed(const QModelIndex &index)
{
cellText = ui->tableView->model()->data(index).toString();
QSqlQuery * qry2 = new QSqlQuery(mydb);
qry2->prepare("select * from Poems where Title='"+cellText+"'");
qry2->exec();
while(qry2->next()){
cellTitle = qry2->value(1).toString();
cellPoem = qry2->value(2).toString();
cellGroup = qry2->value(3).toString();
ui->textEdit->setText(qry2->value(2).toString());
}
}
void MainWindow::on_btnUpdate_clicked()
{
frmUpdate frmupdate;
frmupdate.setModal(true);
frmupdate.exec();
}
frmupdate.cpp
#include "frmupdate.h"
#include "ui_frmupdate.h"
#include <mainwindow.h>
frmUpdate::frmUpdate(QWidget *parent) :
QDialog(parent),
ui(new Ui::frmUpdate)
{
ui->setupUi(this);
MainWindow mainwindow;
ui->lineEdit->setText(mainwindow.cellTitle);
ui->lineEdit_2->setText(mainwindow.cellGroup);
ui->textEdit->setText(mainwindow.cellPoem);
}
frmUpdate::~frmUpdate()
{
delete ui;
}
void frmUpdate::on_btnUpdate_clicked()
{
}
void frmUpdate::on_pushButton_2_clicked()
{
this->close();
}
frmUpdate::frmUpdate(QWidget *parent) :
QDialog(parent),
ui(new Ui::frmUpdate)
{
ui->setupUi(this);
MainWindow mainwindow;
You here have created a new, temporary MainWindow instance only with default constructor being called. Of course, this new instance is then created with empty default texts:
ui->lineEdit->setText(mainwindow.cellTitle);
ui->lineEdit_2->setText(mainwindow.cellGroup);
ui->textEdit->setText(mainwindow.cellPoem);
}
Easiest now would be passing the original main window as parameter:
frmUpdate::frmUpdate(QWidget* parent, MainWindow* mainWindow) :
QDialog(parent),
ui(new Ui::frmUpdate)
{
ui->setupUi(this);
ui->lineEdit->setText(mainwindow->cellTitle);
ui->lineEdit_2->setText(mainwindow->cellGroup);
ui->textEdit->setText(mainwindow->cellPoem);
}
If the parent is the main window, you can simply cast:
frmUpdate::frmUpdate(QWidget* parent) :
QDialog(parent),
ui(new Ui::frmUpdate)
{
ui->setupUi(this);
MainWindow* mainWindow = qobject_cast<MainWindow*>(parent);
if(mainWindow)
{
ui->lineEdit->setText(mainwindow->cellTitle);
ui->lineEdit_2->setText(mainwindow->cellGroup);
ui->textEdit->setText(mainwindow->cellPoem);
}
else
{
// appropriate error handling
}
}
You might search the main window from parent as well:
for(;;)
{
MainWindow* mainWindow = qobject_cast<MainWindow*>(parent);
if(mainWindow)
{
ui->lineEdit->setText(mainwindow->cellTitle);
ui->lineEdit_2->setText(mainwindow->cellGroup);
ui->textEdit->setText(mainwindow->cellPoem);
break;
}
parent = parent->parent();
if(!parent)
{
// appropriate error handling
break;
}
}
All above assuming that MainWindow inherits from QObject...

How to get a signal when the widget size changes

I need to perform an operation when the size of a widget (MyForm) changes.
Is there a signal which is emitted when the size of a widget changes? (I could not find it in the documentation).
I need to do something like this:
#include "myform.h"
#include "ui_myform.h"
MyForm::MyForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyForm)
{
ui->setupUi(this);
connect(this, SIGNAL(sizeChanged()), this, SLOT(refresh()));
}
MyForm::~MyForm()
{
delete ui;
}
void MyForm::refresh()
{
ui->label->setText( QString::number(this->width()) + ", " + QString::number(this->height()));
}
When the widget size changes, it 'calls' the refresh function which updates a label with the current width and height. Note: it is only an example which can be easily reproduced.
Sure I can use a QTimer, for example:
#include "myform.h"
#include "ui_myform.h"
MyForm::MyForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyForm)
{
ui->setupUi(this);
timer_ = new QTimer(this);
connect(timer_, SIGNAL(timeout()), this, SLOT(refresh()));
timer_->start(100);
}
MyForm::~MyForm()
{
delete ui;
}
void MyForm::refresh()
{
ui->label->setText( QString::number(this->width()) + ", " + QString::number(this->height()));
}
But I don't think it is the best solution.
Note: I'm using Qt 5.3.
It is not necessary to create a signal, you just have to overwrite resizeEvent() and call refresh from that method:
*.h
protected:
void resizeEvent(QResizeEvent *event);
*.cpp
void MyForm::resizeEvent(QResizeEvent *event)
{
refresh();
QWidget::resizeEvent(event);
}

Qt WebSockets, how to send a message to client

I'm following this example http://doc.qt.io/qt-5/qtwebsockets-echoserver-example.html but I can't figure out how to send a message to client after the connection has been made.
This seems to work but only when it's inside a function from another class.
void EchoServer::processTextMessage(QString message)
{
m_clients.at(0)->sendTextMessage("test");
}
Where / How should I put the code to get it working?
e. g. How to send a message to client after a button has been pressed in MainWindow?
This is working
void EchoServer::processTextMessage(QString message)
{
m_clients.at(0)->sendTextMessage("test");
}
This isn't working
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_clients.at(0)->sendTextMessage("test");
}
Neither this
void MainWindow::myclicked()
{
m_clients.at(0)->sendTextMessage("test");
}
I got it working. Here is the code:
QWebSocket *messageSocket;
void EchoServer::onNewConnection()
{
QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
connect(pSocket, &QWebSocket::textMessageReceived, this, &EchoServer::processTextMessage);
connect(pSocket, &QWebSocket::disconnected, this, &EchoServer::socketDisconnected);
m_clients << pSocket;
messageSocket=pSocket;
}
void MainWindow::myclicked()
{
if (messageSocket)
{
messageSocket->sendTextMessage("test");
}
}

How to Connect Signal from MainWindow to Slot in Dialog

I have signal in my MainWindow to emit a number thats in a line edit. When I click a button to open the dialog, I want that number to be copied to the line edit in the dialog. I can't get it to connect. I can see the signal is emitting with qDebug. Am I connecting it wrong or in the wrong place? I have tried many ways. Here are my code snippets.
Mainwindow
//My MainWindow
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
//This is the number I am trying to send to the dialog
ui->checkingAmount->setText(QString::number(1000.00, 'f', 2));
ui->checkingAmount->setReadOnly(true);
}
//Emit the data here
void MainWindow::on_transferButton_clicked() {
transferWindow = new TransferWindow(this);
transferWindow->show();
//trying to emit the data
QString data =ui->checkingAmount->text();
emit shareCheckingData(data);
qDebug()<<"emitting mainwin amount";
}
Dialog
//My Dialog
TransferWindow::TransferWindow(QWidget *parent) : QDialog(parent),ui(new Ui::TransferWindow) {
ui->setupUi(this);
//I have tried several variations of this
//mainWindow = new MainWindow();
connect(mainWindow, SIGNAL(shareCheckingData(QString)),this, SLOT(getAmountFromMainWin(QString)));
}
//Here is the connecting slot to get the data from main window
void TransferWindow::getAmountFromMainWin(QString n) {
float CheckTotal = n.toFloat();
ui->checkingAmount->setReadOnly(true);
ui->checkingAmount->setText(QString::number(CheckTotal));
qDebug()<<"setting amount";
}
How can I get this to connect? I read through many posts and it did not solve the problem. Thanks.
I notice in the comments of your code that you intend to create an instance of MainWindow and try to connect to this instance, that is a new instance different from the previous one so you will not be able to get it.
First we must create the instance and then connect it, that we can do in the constructor.
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->checkingAmount->setText(QString::number(1000.00, 'f', 2));
ui->checkingAmount->setReadOnly(true);
transferWindow = new TransferWindow(this);
connect(this, &MainWindow::shareCheckingData, transferWindow, &TransferWindow::getAmountFromMainWin);
//old style
//connect(this, SIGNAL(shareCheckingData(QString)), transferWindow, SLOT(getAmountFromMainWin(QString)));
}
void MainWindow::on_transferButton_clicked()
{
//trying to emit the data
QString data =ui->checkingAmount->text();
emit shareCheckingData(data);
qDebug()<<"emitting mainwin amount";
transferWindow->show();
}
TransferWindow.cpp
TransferWindow::TransferWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::TransferWindow)
{
ui->setupUi(this);
}
void TransferWindow::getAmountFromMainWin(QString n)
{
float CheckTotal = n.toFloat();
ui->checkingAmount->setReadOnly(true);
ui->checkingAmount->setText(QString::number(CheckTotal));
qDebug()<<"setting amount";
}

QT With C++: How to implement Save As

I'm trying to add a save as feature to a simple text editor I'm making with C++ and QT. I'm attempting to close the current tab when you save your file and open a new tab with the same index and has the name of the new file as the tab title. This is my code:
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), "", tr("All Files (*)"));
if (fileName.isEmpty())
return;
else
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
QTextStream out (&file);`
out << ui->plainTextEdit->toPlainText();
QFileInfo FileData(file);
int currentTab = ui->tabWidget->currentIndex();
ui->tabWidget->removeTab(currentTab);
QTextStream InputData(&file);
ui->tabWidget->insertTab(currentTab, new Form(), FileData.fileName());
ui->tabWidget->setCurrentIndex(currentTab);
ui->plainTextEdit->setPlainText(InputData.readAll());
file.flush();
file.close();
}
When I try to save the new file, it saves the file to the selected location and replaces the current tab with the file name, but it doesn't write the file to the text window. Any help would be great.
Here is my demo code and it seems to work:
form.cpp which has a plainTextEdit
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
QString Form::GetText()
{
return ui->plainTextEdit->toPlainText();
}
void Form::SetText(QString text)
{
ui->plainTextEdit->setPlainText(text);
}
And the mainwindow.cpp which contains a QTabWidget
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
ui->tabWidget->insertTab(0, new Form(), "My File");
ui->tabWidget->setCurrentIndex(0);
}
void MainWindow::on_pushButton_2_clicked()
{
int currentIndex = ui->tabWidget->currentIndex();
//Save the text to a file or a variable...
QString content = static_cast<Form*>(ui->tabWidget->widget(currentIndex))->GetText();
ui->tabWidget->removeTab(currentIndex);
ui->tabWidget->insertTab(currentIndex, new Form(), "Saved File");//new name
static_cast<Form*>(ui->tabWidget->widget(currentIndex))->SetText(content);
ui->tabWidget->setCurrentIndex(currentIndex);
}
It is working for me, please try.
Thank You.