Multiple video play on QT application simultaneously - c++

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...");
}

Related

Qt not removing item from layout

I have a simple Qt application that dynamically adds and deletes items from the layout, but it does not work, so I wrote the code below to illustrate what I'm doing:
The header file justs put an additional layout field in addition to Qt creator template:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFrame>
#include <QVBoxLayout>>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QFrame* create_frame();
void remove_frame();
QVBoxLayout* layout;
};
#endif // MAINWINDOW_H
The cpp file adds a few QFrame to the current layout, then clicking the button will remove them one by one. The QCoreApplication::processEvents and QThread::sleep just adds some animation to show what is happening.
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QPushButton>
#include <QThread>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
auto window = new QWidget(this);
setCentralWidget(window);
auto button = new QPushButton(window);
button->setGeometry(100, 100, 100, 100);
layout = new QVBoxLayout;
window->setLayout(layout);
for (int i = 0; i < 5; i++) {
auto x = create_frame();
layout->addWidget(x);
}
connect(button, &QPushButton::clicked, this, &MainWindow::remove_frame);
}
void MainWindow::remove_frame() {
while(layout->count() > 0) {
auto item = layout->itemAt(0);
layout->removeItem(item);
delete item;
QCoreApplication::processEvents();
QThread::msleep(200);
}
}
QFrame* MainWindow::create_frame() {
auto x = new QFrame();
x->setMinimumHeight(20);
x->setMaximumHeight(20);
x->setFrameStyle(QFrame::StyledPanel);
return x;
}
MainWindow::~MainWindow()
{
delete ui;
}
The animation is a mess, and does not completely remove all the QFrame boxes. What did I do wrong here? And later I find that if I use auto item = layout->itemAt(0)->widget(); layout->removeWidget(item); then I can indeed remove them, are removeItem and removeWidget doing different things?

How to get QString from one window to another window, via pressing a button in a 3rd window

New to C++ and Qt as part of a research project (biology) and have been struggling with presumably some quite simple stuff. I'd really appreciate someone's help.
I'm working with a GUI for a pre-existing programme and I'm trying to transfer a QString variable from the QLineEdit of one of the windows (inputform), to the QLineEdit of a second window (output form).
The bit I'm stuck with is that I need the output form to appear, with it's LineEdit pre-populated, when I click a button on a third window (filedialog).
Problem:
At start up --> two windows appear: filedialog and inputform.
User enters data into inputform's QLineEdit
User presses 'transferButton' on filedialog window
On button press --> outputform appears, with a QLineEdit pre-populated with the user's data (from the inputform).
I assume the problem is of the getter/setter variety and my variable is probably going out of scope, but I've tried following lots of similar examples but can't make it work.
Thanks in advance.
Here's my code:
Main.cpp
#include "filedialog.h"
#include "inputform.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FileDialog w;
InputForm w2;
w.show();
w2.show();
return a.exec();
}
filedialog.h
#ifndef FILEDIALOG_H
#define FILEDIALOG_H
#include <QDialog>
namespace Ui {
class FileDialog;
}
class FileDialog : public QDialog
{
Q_OBJECT
public:
explicit FileDialog(QWidget *parent = nullptr);
~FileDialog();
void setFileName();
QString getFileName();
private slots:
void on_transferButton_clicked();
private:
Ui::FileDialog *ui;
QString fileName;
};
#endif // FILEDIALOG_H
filedialog.ccp
#include "filedialog.h"
#include "ui_filedialog.h"
#include "inputform.h"
#include "ui_inputform.h"
#include "outputform.h"
#include "ui_outputform.h"
FileDialog::FileDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::FileDialog)
{
ui->setupUi(this);
}
FileDialog::~FileDialog()
{
delete ui;
}
void FileDialog::setFileName()
{
InputForm *inputform = new InputForm;
fileName = inputform->ui->inputLineEdit->text();
}
QString FileDialog::getFileName()
{
return fileName;
}
void FileDialog::on_transferButton_clicked()
{
setFileName();
OutPutForm *outputform = new OutPutForm;
outputform->ui->outputLineEdit->setText(getFileName());
outputform->show();
}
inputform.h
#ifndef INPUTFORM_H
#define INPUTFORM_H
#include <QWidget>
namespace Ui {
class InputForm;
}
class InputForm : public QWidget
{
Q_OBJECT
public:
explicit InputForm(QWidget *parent = nullptr);
~InputForm();
Ui::InputForm *ui;
};
#endif // INPUTFORM_H
inputform.ccp
#include "inputform.h"
#include "ui_inputform.h"
InputForm::InputForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::InputForm)
{
ui->setupUi(this);
}
InputForm::~InputForm()
{
delete ui;
}
outputform.h
#ifndef OUTPUTFORM_H
#define OUTPUTFORM_H
#include <QWidget>
namespace Ui {
class OutPutForm;
}
class OutPutForm : public QWidget
{
Q_OBJECT
public:
explicit OutPutForm(QWidget *parent = nullptr);
~OutPutForm();
Ui::OutPutForm *ui;
};
#endif // OUTPUTFORM_H
outputform.ccp
#include "outputform.h"
#include "ui_outputform.h"
OutPutForm::OutPutForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::OutPutForm)
{
ui->setupUi(this);
}
OutPutForm::~OutPutForm()
{
delete ui;
}
Thank you for your brief pointer.
After some playing around:
Setup mainwindow (or in my case main dialog window). Generate inputform instance, connect button to inputform.
FileDialog::FileDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::FileDialog)
{
ui->setupUi(this);
InputForm *inputForm = new InputForm;
connect(ui->transferButton,SIGNAL(clicked()),inputForm,SLOT(getLineEditTextFunc()));
inputForm->show();
}
FileDialog::~FileDialog()
{
delete ui;
}
void FileDialog::on_transferButton_clicked()
{
}
Then from the input form:
Define a function to get the input form's LineEdit text (fileName); and then also generate an output form and populate it's LineEdit with the fileName variable.
InputForm::InputForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::InputForm)
{
ui->setupUi(this);
}
InputForm::~InputForm()
{
delete ui;
}
void InputForm::getLineEditTextFunc()
{
fileName = this->ui->inputLineEdit->text();
OutPutForm *outputform = new OutPutForm;
outputform->ui->outputLineEdit->setText(fileName);
outputform->show();
}

Widget is not properly visible

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);
}

Change parameters of a class when clicking button on different window

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.

QtPushButton wont click

Hello Im working through Qt tutorials I've have copy the code for the communicate section of this tutorial. the code compiles and shows but none of my buttons are clickable.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QPushButton;
class QLabel;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void OnPlus();
void OnMinus();
private:
Ui::MainWindow *ui;
QLabel *label;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QPushButton *plus = new QPushButton("+", this);
plus->setGeometry(50, 40, 75, 30);
QPushButton *minus = new QPushButton("-", this);
minus->setGeometry(50, 100, 75, 30);
label = new QLabel("0", this);
label->setGeometry(190, 80, 20, 30);
connect(plus, SIGNAL(clicked()), this, SLOT(OnPlus()));
connect(minus, SIGNAL(clicked()), this, SLOT(OnMinus()));
ui->setupUi(this);
}
void MainWindow::OnPlus()
{
int val = label->text().toInt();
val++;
label->setText(QString::number(val));
}
void MainWindow::OnMinus()
{
int val = label->text().toInt();
val--;
label->setText(QString::number(val));
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QPushButton>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
Your problem is with this line:
ui->setupUi(this);
It creates an invisible central widget for your main window, which blocks all events destined for your buttons, which is why they don't depress when you click them. Move this line to the beginning of your constructor for MainWindow and the problem will go away.