Hey I want to create a 8x8 field of pushButtons. When using this Code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVector>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
QGridLayout* layout = new QGridLayout();
ui->centralwidget->setLayout(layout);
QVector<QVector<QPushButton*>> buttons2DVector(8);
for (int i=0;i<8;i++){
buttons2DVector[i].resize(8);
for(int j=0;j<8;j++){
QPushButton *b = new QPushButton("button");
layout->addWidget(b,i,j);
buttons2DVector[i][j] = b;
}
}
the error is:
unknown type Name "QGridLayout"
unknown type Name "ui"
the Code was given to me but I dont know how to use it properly. As you can probably see I am a beginner in QT and C++ but it would be nice if someone could help me.
Assuming you added the following to your MainWindow class declaration:
QVector<QVector<QPushButton*>> buttons2DVector(8);
void createGrid();
You could create the grid programmatically as follows:
#include "mainwindow.h"
#include <QVector>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
createGrid();
}
void MainWindow::createGrid() {
QFrame *frame = new QFrame(this);
QGridLayout *layout = new QGridLayout(frame);
for (int i=0;i<8;i++){
buttons2DVector[i].resize(8);
for(int j=0;j<8;j++){
QPushButton *b = new QPushButton("button", layout);
layout->addWidget(b,i,j);
buttons2DVector[i][j] = b;
}
}
setCentralWidget(frame);
}
Related
I would like to make a push button visible when I insert a number in one of the lineedit. The button and linedit are on the same row. I know the position or name of the lineedit but i dont know how to link back to the push button to make it visible or to be able to change color.
If you look in the eventFilter thats where I'm stuck and I need some help.
Thank you
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include <QRegion>
#include <QLayoutItem>
#include <QList>
#include<QObject>
#include <QEvent>
#include <QKeyEvent>
#include <QModelIndexList>
#include <QKeySequence>
#include <QSignalMapper>
#include<QIntValidator>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
ui->scrollArea->setWidgetResizable( true );
QWidget *widget = new QWidget(this);
ui->scrollArea->setWidget( widget );
QGridLayout *gridLayout = new QGridLayout();
widget->setLayout( gridLayout );
QPushButton *button[20];
for (int i = 0; i < 20; i++)
{
QLabel *label = new QLabel( QString( "%1" ).arg( i ) );
gridLayout->addWidget(label,i,1,1,1 );
gridLayout->setColumnMinimumWidth(1, 100);
gridLayout->setColumnMinimumWidth(2, 10);
if (i==5)
{
QLineEdit *lineEdit = new QLineEdit;
gridLayout->addWidget(lineEdit,i,5,1,1);
} else
{
QLineEdit *lineEdit = new QLineEdit(this);
gridLayout->addWidget(lineEdit,i,3,1,1);
lineEdit->setValidator(new QIntValidator(0,100,this));
lineEdit->setObjectName(QString::number(i));
lineEdit->installEventFilter(this);
gridLayout->setColumnMinimumWidth(4, 25);
gridLayout->setColumnMinimumWidth(5, 50);
gridLayout->setColumnMinimumWidth(6, 25);
QLineEdit *lineEdit_B = new QLineEdit;
gridLayout->addWidget(lineEdit_B,i,7,1,1);
lineEdit_B->setValidator(new QIntValidator(0,100,this));
gridLayout->setColumnMinimumWidth(8, 10);
}
gridLayout->setColumnMinimumWidth(9, 10);
button[i] = new QPushButton();
gridLayout->addWidget(button[i],i,10,1,1);
gridLayout->setColumnStretch(10,20);
button[i]->setFixedHeight(20);
button[i]->setFixedWidth(20);
button[i]->setStyleSheet("background-color:red;");
button[i]->setText(QString::number(i));
QRegion* region = new QRegion(*(new QRect(button[i]->x(),button[i]->y(),15,15)),QRegion::Ellipse);
button[i]->setMask(*region);
button[i]->setVisible(false);
gridLayout->setColumnMinimumWidth(10, 50);
gridLayout->setColumnMinimumWidth(11, 10);
}
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject * obj, QEvent *event)// *event)
{
if (event->type() == QEvent::KeyPress)
{
ui->lineEdit->setText(QString("%1").arg(obj->objectName()));
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug() << "key " << keyEvent->key() << "from" << obj;
// HERE'S WHERE I NEED HELP
QPushButton* button = ui->scrollArea->findChild<QPushButton*>();
// I think here I would use a for loop to match the listedit
// with a number with the corresponding push button.
// When i find the push button I set it to yellow.
}
return QObject::eventFilter(obj, event);
}
void MainWindow::on_pushButton_clicked()
{
}
It is not necessary to use eventFilter, you complicate the task since it is difficult to discriminate which element it is. One possible option is to use the textChanged signal to display the button.
Also you had many tasks that are executed many times in the loop unnecessarily, those tasks that do not depend on the index must be out.
Also if you are going to store buttons do not use arrays, use containers like QList.
Also, you should not create pointers indiscriminately, since it is your responsibility to eliminate them, for example, QRegion is passed by value to setMask(), so it is not necessary to create a pointer.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QScrollArea>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
ui->scrollArea->setWidgetResizable( true );
QWidget *widget = new QWidget(this);
ui->scrollArea->setWidget( widget );
QGridLayout *gridLayout = new QGridLayout(widget);
for(int i=0; i<20; i++){
QLabel *label = new QLabel(QString::number(i));
gridLayout->addWidget(label, i, 1);
QPushButton *button = new QPushButton(QString::number(i));
gridLayout->addWidget(button,i,10,1,1);
button->setFixedSize(20, 20);
button->setStyleSheet("background-color:red;");
QRegion region(QRect(button->pos(),QSize(15,15)),QRegion::Ellipse);
button->setMask(region);
button->hide();
if(i==5){
QLineEdit *lineEdit = new QLineEdit;
gridLayout->addWidget(lineEdit,i,5);
connect(lineEdit, &QLineEdit::textChanged, [button](const QString &text){
button->setVisible(!text.isEmpty());
});
}
else{
QLineEdit *lineEdit_A = new QLineEdit;
gridLayout->addWidget(lineEdit_A,i,3);
lineEdit_A->setValidator(new QIntValidator(0,100,this));
QLineEdit *lineEdit_B = new QLineEdit;
gridLayout->addWidget(lineEdit_B,i, 7);
lineEdit_B->setValidator(new QIntValidator(0,100,this));
connect(lineEdit_A, &QLineEdit::textChanged, [button](const QString &text){
button->setVisible(!text.isEmpty());
});
connect(lineEdit_B, &QLineEdit::textChanged, [button](const QString &text){
button->setVisible(!text.isEmpty());
});
}
}
gridLayout->setColumnMinimumWidth(1, 100);
gridLayout->setColumnMinimumWidth(2, 10);
gridLayout->setColumnMinimumWidth(4, 25);
gridLayout->setColumnMinimumWidth(5, 50);
gridLayout->setColumnMinimumWidth(6, 25);
gridLayout->setColumnMinimumWidth(8, 10);
gridLayout->setColumnMinimumWidth(9, 10);
gridLayout->setColumnStretch(10,20);
gridLayout->setColumnMinimumWidth(10, 50);
gridLayout->setColumnMinimumWidth(11, 10);
}
MainWindow::~MainWindow()
{
delete ui;
}
The complete example can be found in the following link.
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);
}
I'm pretty new to c++ and Qt but I'm trying to create a simple battleship game. So far I have been able to set up the grid and also place ships but I wan't to put in a menu before the main game window where you choose "difficulty" which will determine how big the grid size for battleship is.
The code for drawing the game board goes as follows:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "boardrender.h"
#include "playerrender.h"
#include "../BattleScrabble/BattleshipBoard.h"
#include "../BattleScrabble/HumanPlayer.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// BattleshipBoard *a = new BattleshipBoard(10);
// Ship *b = new Ship(AircraftCarrier);
ui->battleshipBoardView->setMouseTracking(true);
// BoardRender *scene=new BoardRender(a, ui->battleshipBoardView->width(), this);
// ui->battleshipBoardView->setScene(scene);
ui->battleshipBoardView->resize(ui->battleshipBoardView->width(),ui->battleshipBoardView->width());
Player *b = new HumanPlayer(10,0);
PlayerRender *a = new PlayerRender(b, ui->battleshipBoardView,this);
}
MainWindow::~MainWindow()
{
delete ui;
}
where new HumanPlayer(10,0); is the grid size.
And now the interesting part, the difficulty window:
#include "difficultywindow.h"
#include "ui_difficultywindow.h"
#include "DifficultyWindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
DifficultyWindow::DifficultyWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::DifficultyWindow)
{
ui->setupUi(this);
}
DifficultyWindow::~DifficultyWindow()
{
delete ui;
}
void DifficultyWindow::on_pushButton_clicked()
{
mWindow = new MainWindow(this);
mWindow->show();
}
when I'm instantiating the "new MainWindow" is there a way to change the HumanPlayer parameters in this class instead?
Any help is greatly appreciated. Thanks :)
edit: and my main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "difficultywindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DifficultyWindow w;
w.show();
return a.exec();
}
You have to make some modifications, to make your code more understandable you must first add a parameter to MainWidow, in this case it will be called grid
mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(int grid, QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
Player *b;
PlayerRender *g;
};
mainwindow.cpp
MainWindow::MainWindow(int grid, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->battleshipBoardView->setMouseTracking(true);
ui->battleshipBoardView->resize(ui->battleshipBoardView->width(),ui->battleshipBoardView->width());
b = new HumanPlayer(grid, 0);
g = new PlayerRender(b, ui->battleshipBoardView,this);
}
Then we will create an enumeration and to handle a signal instead of 3 we will use QButtonGroup:
difficultyWindow.h
class difficultyWindow : public QDialog
{
Q_OBJECT
public:
enum Difficult{
EASY,
MEDIUM,
HARD
};
explicit difficultyWindow(QWidget *parent = 0);
~difficultyWindow();
private slots:
void onClicked(int id);
private:
Ui::difficultyWindow *ui;
MainWindow *mWindow;
QButtonGroup *group;
};
difficultyWindow.h
difficultyWindow::difficultyWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::difficultyWindow)
{
ui->setupUi(this);
group = new QButtonGroup(this);
group->addButton(ui->easyDiffButton, EASY);
group->addButton(ui->mediumDifficultyButton, MEDIUM);
group->addButton(ui->hardDifficultyButton, HARD);
connect(group, SIGNAL(buttonClicked(int)), this, SLOT(onClicked(int)));
}
difficultyWindow::~difficultyWindow()
{
delete ui;
}
void difficultyWindow::onClicked(int id)
{
int grid;
switch (id) {
case EASY:
grid = 10;
break;
case MEDIUM:
grid = 20;
break;
case HARD:
grid = 30;
break;
}
mWindow = new MainWindow(grid, this);
mWindow->show();
}
Note: eliminate the slots, its are unnecessary for this case.
I have written a code to print an Icon accessed from FontAwesome installed in the system. I would like to change the color for the Icon printed on the Screen.I have tried using QPixmap and QIcon,but to no avail.Attached the output :
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QPushButton>
#include <QGridLayout>
#include <QWidget>
#include <QLabel>
#include "qfonticon.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget *centralWidget;
QGridLayout *gridLayout;
centralWidget = new QWidget(this);
gridLayout = new QGridLayout( centralWidget );
QFontIcon::addFont("/usr/share/fonts/fontawesome-webfont.ttf");
QIcon icon = QFontIcon::icon(0xf2e0,QColor(1,0,1,255));
//QFontIconEngine::addFile("/usr/share/fonts/fontawesome-webfont.ttf");
//QPixmap pix = QFontIconEngine::;
QPushButton *b = new QPushButton();
//QLabel *l = new QLabel();
b->setIcon(icon);
b->setIconSize(QSize(75,75));
//l->setPixmap(pix);
gridLayout->addWidget(b);
//gridLayout->addWidget(l);
this->setCentralWidget(centralWidget);
}
MainWindow::~MainWindow()
{
delete ui;
}
I want to make application on Qt, which can play more then one video simultaneously. I want to make application for security camera, so I have to show all camera's stream simultaneously. I made media application with help of QMediaPlayer and QVideoWidget, which support all type of video.
I also get help from bellow site, but I don't want to use Vlc Lib.
Playing multiple video using libvlc and Qt
Please guide me path to achieve my destination.
Should I have to use Phonon?
I try some code to display two video simultaneously, but get some problem.
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.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGridLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
player[i][j] = new QMediaPlayer(this);
vw[i][j] = new QVideoWidget(this);
player[i][j]->setVideoOutput(vw[i][j]);
ui->graphicsView->setViewport(vw[i][j]);
//ui->graphicsView_2->setViewport(vw[i][j]);
slider = new QSlider(this);
bar = new QProgressBar(this);
slider1 = new QSlider(this);
slider->setOrientation(Qt::Horizontal);
ui->statusBar->addPermanentWidget(slider);
ui->statusBar->addPermanentWidget(bar);
ui->statusBar->addPermanentWidget(slider1);
connect(player[i][j],&QMediaPlayer::durationChanged,slider,&QSlider::setMaximum);
connect(player[i][j],&QMediaPlayer::positionChanged,slider,&QSlider::setValue);
connect(slider,&QSlider::sliderMoved,player[i][j],&QMediaPlayer::setPosition);
slider1->setValue(50);
connect(slider1,&QSlider::sliderMoved,player[i][j],&QMediaPlayer::setVolume);
connect(player[i][j],&QMediaPlayer::durationChanged,bar,&QProgressBar::setMaximum);
connect(player[i][j],&QMediaPlayer::positionChanged,bar,&QProgressBar::setValue);
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
QString filename = QFileDialog::getOpenFileName(this,"Open a File","","Video File (*.*)");
on_actionStop_triggered();
player[1][1]->setMedia(QUrl::fromLocalFile(filename));
on_actionPlay_triggered();
}
void MainWindow::on_actionPlay_triggered()
{
player[1][1]->play();
ui->statusBar->showMessage("Playing");
}
void MainWindow::on_actionPause_triggered()
{
player[1][1]->pause();
ui->statusBar->showMessage("Paused...");
}
void MainWindow::on_actionStop_triggered()
{
player[1][1]->stop();
ui->statusBar->showMessage("Stopped");
}
void MainWindow::on_actionMute_triggered()
{
player[1][1]->setMuted(1);
ui->statusBar->showMessage("Muted...");
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QFileDialog>
#include <QProgressBar>
#include <QSlider>
#include <QWidget>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_actionOpen_triggered();
void on_actionPlay_triggered();
void on_actionPause_triggered();
void on_actionStop_triggered();
void on_actionMute_triggered();
private:
Ui::MainWindow *ui;
QMediaPlayer* player[2][2];
QVideoWidget* vw[2][2];
QProgressBar* bar;
QSlider* slider;
QSlider* slider1;
};
#endif // MAINWINDOW_H
mainwindow.ui
Application Image:
Now I want to play same video on Graphics widget. Is it possible? If yes, then How?
Can anyone guide me, how can I add thread on ui->GraphicsView and ui->GraphicsView_2 in mainwindo.cpp file?
Thanks
Tejas Virpariya
I done, I can play two video simultaneously.
I change my mainwindow.cpp file only.Its not a perfect code, but its a solution.
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
player = new QMediaPlayer;
vw = new QVideoWidget;
player->setVideoOutput(vw);
ui->graphicsView->setViewport(vw);
//ui->graphicsView_2->setViewport(vw1);
slider = new QSlider(this);
bar = new QProgressBar(this);
slider1 = new QSlider(this);
slider->setOrientation(Qt::Horizontal);
ui->statusBar->addPermanentWidget(slider);
ui->statusBar->addPermanentWidget(bar);
ui->statusBar->addPermanentWidget(slider1);
connect(player,&QMediaPlayer::durationChanged,slider,&QSlider::setMaximum);
connect(player,&QMediaPlayer::positionChanged,slider,&QSlider::setValue);
connect(slider,&QSlider::sliderMoved,player,&QMediaPlayer::setPosition);
slider1->setValue(50);
connect(slider1,&QSlider::sliderMoved,player,&QMediaPlayer::setVolume);
connect(player,&QMediaPlayer::durationChanged,bar,&QProgressBar::setMaximum);
connect(player,&QMediaPlayer::positionChanged,bar,&QProgressBar::setValue);
player1 = new QMediaPlayer;
vw1 = new QVideoWidget;
player1->setVideoOutput(vw1);
//ui->graphicsView->setViewport(vw);
ui->graphicsView_2->setViewport(vw1);
slider2 = new QSlider(this);
bar1 = new QProgressBar(this);
slider3 = new QSlider(this);
slider2->setOrientation(Qt::Horizontal);
ui->statusBar->addPermanentWidget(slider2);
ui->statusBar->addPermanentWidget(bar1);
ui->statusBar->addPermanentWidget(slider3);
connect(player1,&QMediaPlayer::durationChanged,slider2,&QSlider::setMaximum);
connect(player1,&QMediaPlayer::positionChanged,slider2,&QSlider::setValue);
connect(slider2,&QSlider::sliderMoved,player1,&QMediaPlayer::setPosition);
slider3->setValue(50);
connect(slider3,&QSlider::sliderMoved,player1,&QMediaPlayer::setVolume);
connect(player1,&QMediaPlayer::durationChanged,bar1,&QProgressBar::setMaximum);
connect(player1,&QMediaPlayer::positionChanged,bar1,&QProgressBar::setValue);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
QString filename = QFileDialog::getOpenFileName(this,"Open a File","","Video File (*.*)");
QString filename1 = "C:/Users/Public/Videos/Sample Videos/Wildlife.wmv";
on_actionStop_triggered();
player->setMedia(QUrl::fromLocalFile(filename));
player1->setMedia(QUrl::fromLocalFile(filename1));
on_actionPlay_triggered();
}
void MainWindow::on_actionPlay_triggered()
{
player->play();
player1->play();
ui->statusBar->showMessage("Playing");
}
void MainWindow::on_actionPause_triggered()
{
player->pause();
player1->pause();
ui->statusBar->showMessage("Paused...");
}
void MainWindow::on_actionStop_triggered()
{
player->stop();
player1->stop();
ui->statusBar->showMessage("Stopped");
}
void MainWindow::on_actionMute_triggered()
{
if(player->isMuted()){
player->setMuted(0);
}else{
player->setMuted(1);
}
if(player1->isMuted()){
player1->setMuted(0);
}else{
player1->setMuted(1);
}
ui->statusBar->showMessage("Muted...");
}