PDA

View Full Version : single timer for multiple objects



prabhatjha
29th August 2013, 08:23
hello everyone i am new in qt. i made a gui in qt in which on mousepressevent i am creating a object of button and on button i am adding a image .
when will be user clicked on mousepress a button will be appear on screen.now on a button click i want to rotate this image according to timer.
every image will be rotate on buttonclick.i am attaching my code with this.pleaase give me a solution how i will use a time for multiple objects.


this is .h file

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QMainWindow>
#include<QMouseEvent>
#include <QPushButton>
#include<QLabel>

namespace Ui
{
class MainWindow;
class MyWidget;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

bool inc;
void paintEvent(QPaintEvent * event);
void mousePressEvent(QMouseEvent *ev);
void mouseMoveEvent ( QMouseEvent * event );
double x[100];
double y[100];
QTimer *timer;

private:
Ui::MainWindow *ui;

public slots:

void upd();

private slots:
void on_pushButton_clicked();
};


this is .cpp file


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPainter>
#include<QPushButton>
#include<QMessageBox>
#include<QDebug>
#include<QLayout>
#include<QToolTip>
#include<QTimer>



int Ship_Cont =0;

QPushButton* button[100];


bool Ship_Flag = true;

//double x[100];
//double y[100];

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{

inc =false;

timer = new QTimer(this);
timer->setInterval(1000);
timer->start();
connect(timer, SIGNAL(timeout()), this, SLOT(upd()));



}




MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setBrush(Qt::black);
painter.drawRect(0,0,500,500);

if(Ship_Flag == false)
{
for (int j =1 ;j<= Ship_Cont; j++)
{

button[j]->setGeometry(QRect(x[j] , y[j] , 12, 25));

button[j]->show();
}
// Ship_Flag = true;
}



}

void MainWindow::mousePressEvent(QMouseEvent *ev)
{
Ship_Cont++;

QIcon icon("C:/Users/Pankaj/Desktop/ship1.jpg");
button[Ship_Cont] = new QPushButton(this);
button[Ship_Cont]->setIcon(icon);
button[Ship_Cont]->setStyleSheet("background-color: black");

button[Ship_Cont]->setStyleSheet( "border:none");

x[Ship_Cont] = ev->pos().x();
y [Ship_Cont]= ev->pos().y();

// qDebug()<<"Ship_Cont";
// qDebug()<<Ship_Cont;


button[Ship_Cont]->setGeometry(QRect(x[Ship_Cont] , y[Ship_Cont] , 12, 25));

button[Ship_Cont]->show();





}

void MainWindow::mouseMoveEvent ( QMouseEvent * event )
{
QToolTip::showText(event->globalPos(), QString::number( event->pos().x() ) + ", " + QString::number( event->pos().y() ) + ", " + "prabhat ",this, rect() );
}

void MainWindow::upd()
{
if (inc == true)
{


for(int i =1; i<=Ship_Cont;i++)
{
qDebug()<<"X";
qDebug()<< Ship_Cont;
qDebug()<< x[Ship_Cont];
qDebug()<<"Y";
qDebug()<< Ship_Cont;
qDebug()<< y[Ship_Cont];
x[i] = x[i]+10;

y[i] = y[i] +10;
}
update();
Ship_Flag = false;
}
}


void MainWindow::on_pushButton_clicked()
{
inc = true;

}

this is main.cpp


#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QMainWindow *window = new QMainWindow();


MainWindow w;
w.show();
return a.exec();
}

Santosh Reddy
29th August 2013, 08:32
This not a solution but just some pointer to how to go about


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMouseEvent>
#include <QPushButton>
#include <QDebug>
#include <QLabel>
#include <QList>

class RotatingButton : public QPushButton
{
Q_OBJECT
public :
explicit RotatingButton(int x, int y, QWidget * parent = 0)
: QPushButton(parent)
, rect(x, y, 12, 25)
{
QIcon icon("C:/Users/Pankaj/Desktop/ship1.jpg");
setIcon(icon);
setStyleSheet("{"
" background-color: black"
" border:none"
"}");

setGeometry(rect);

connect(this, SIGNAL(clicked()), SLOT(rotate()));
}

private slots:
void rotate(void)
{
int w = rect.width();
int h = rect.height();
qSwap(h, w);
rect.setWidth(w);
rect.setHeight(h);

setGeometry(rect);
qDebug() << "rotate" << rect;
}

void move(void)
{
rect.adjust(10, 10, 10, 10);
setGeometry(rect);
qDebug() << "move" << rect;
}

private:
QRect rect;
};

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

bool inc;

protected:
void paintEvent(QPaintEvent * event);
void mousePressEvent(QMouseEvent *ev);
void mouseMoveEvent(QMouseEvent * event);

private:
QWidget * centerWidget;
QTimer * timer;
QList<RotatingButton *> buttons;
};

#endif // MAINWINDOW_H



#include "mainwindow.h"
#include <QPainter>
#include <QPushButton>
#include <QMessageBox>
#include <QDebug>
#include <QLayout>
#include <QToolTip>
#include <QTimer>

MainWindow::MainWindow(QWidget * parent)
: QMainWindow(parent)
{
centerWidget = new QWidget;
setCentralWidget(centerWidget);

timer = new QTimer(this);
timer->setInterval(1000);
timer->start();
}

MainWindow::~MainWindow()
{
;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setBrush(Qt::black);
painter.drawRect(0,0,500,500);

QMainWindow::paintEvent(event);
}

void MainWindow::mousePressEvent(QMouseEvent *ev)
{
RotatingButton * button = new RotatingButton(ev->pos().x(), ev->pos().y(), centerWidget);
connect(timer, SIGNAL(timeout()), button, SLOT(move()));
buttons.append(button);
button->show();
}

void MainWindow::mouseMoveEvent(QMouseEvent * event)
{
QMainWindow::mouseMoveEvent(event);
QToolTip::showText(event->globalPos(), QString::number(event->pos().x() ) + ", " + QString::number( event->pos().y() ) + ", " + "prabhat ",this, rect() );
}




#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

MainWindow w;
w.show();

return a.exec();
}

prabhatjha
29th August 2013, 09:57
thanks a lot ....for giving such a idea.
but this was not my problem actually i want when user will press the mouse a button(image) will be appear on screen and it will move when user will cllick on move button.on move button my timer will be start .and every button(image) will be move when user click on move button.lets we press mouse first time a button image will be appear on screen and now when we will click move button the image will start to rotate.and now we pressed again mouse an other button(image) will be appear on screen and it will not move until user click on move button.please give me some solution.thanks in advance.

wagmare
29th August 2013, 10:14
one way is
QPropertyAnimation

QPropertyAnimation* anim = new QPropertyAnimation(button, "rotation");
http://qt-project.org/doc/qt-4.8/statemachine-api.html#animating-property-assignments

Santosh Reddy
29th August 2013, 10:34
So where is code for move push button? If that is you question, where do you want to put the move push button?

wagmare
29th August 2013, 10:45
direct rotation on QPushButton is not possible :(
see this sample code how to rotate a pushButton

int main(int argc, char** argv)
{
QApplication a(argc, argv);
QGraphicsView view;

QGraphicsScene *scene = new QGraphicsScene(-100, -100, 200, 200, &view);
view.setScene(scene);
view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

QIcon icon;
icon.addPixmap(QPixmap(":/Images/minitools.png"));
QPushButton *rotateButton = new QPushButton();
rotateButton->setFixedSize(60, 50);
rotateButton->setIcon(icon);
rotateButton->setIconSize(QSize(rotateButton->width()-2, rotateButton->height()-2));
QGraphicsProxyWidget *rotateItem = scene->addWidget(rotateButton);
rotateItem->setTransformOriginPoint(rotateButton->width()/2,
rotateButton->height()/2);
rotateItem->setPos(-rotateButton->width()/2, 10);

QPropertyAnimation *rotateAnimation =
new QPropertyAnimation(rotateItem, "rotation", &view);
rotateAnimation->setStartValue(0.0);
rotateAnimation->setEndValue(360.0);
rotateAnimation->setDuration(2000);
rotateAnimation->setEasingCurve(QEasingCurve::OutBounce);
QObject::connect(rotateButton, SIGNAL(clicked()),
rotateAnimation, SLOT(start()));
view.show();
return a.exec();
}

prabhatjha
29th August 2013, 11:12
my move pushbutton is on mainwindow ....and timer will be start on this button please look a view on give code...on pushbuttonclicked bool inc will be true and when boll inc will be true the my timer will be start.

Added after 5 minutes:

hello santosh sir may i get ur mail id...
i can send my code on dis..

Santosh Reddy
29th August 2013, 12:30
Can't you post it here?

prabhatjha
30th August 2013, 08:54
i am adding my code ..please give a look on this.


this is .h file


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QMainWindow>
#include<QMouseEvent>
#include <QPushButton>
#include<QLabel>

namespace Ui
{
class MainWindow;
class MyWidget;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

bool inc;
void paintEvent(QPaintEvent * event);
void mousePressEvent(QMouseEvent *ev);
void mouseMoveEvent ( QMouseEvent * event );
double x[100];
double y[100];
QTimer *timer;

private:
Ui::MainWindow *ui;

public slots:

void upd();

private slots:
void on_pushButton_clicked();
};

#endif // MAINWINDOW_H


this is .cpp file


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPainter>
#include<QPushButton>
#include<QMessageBox>
#include<QDebug>
#include<QLayout>
#include<QToolTip>
#include<QTimer>



int Ship_Cont =0;

QPushButton* button[100];


bool Ship_Flag = true;

//double x[100];
//double y[100];

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{


MyWidget *wid;
ui->setupUi(this);
wid = ui->widget;

inc =false;


QRect *rect = new QRect(0,0,500,500);
QRegion* region = new QRegion(*rect,QRegion::Ellipse);
wid->setMask(*region);
// wid->setStyleSheet("background-color: yellow");
// connect(ui->pushButton,SIGNAL(clicked()),wid,SLOT(Button()));
// connect(ui->pushButton_2,SIGNAL(clicked()),wid,SLOT(Button1()) );


timer = new QTimer(this);
timer->setInterval(1000);
timer->start();
connect(timer, SIGNAL(timeout()), this, SLOT(upd()));



}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setBrush(Qt::black);
painter.drawRect(0,0,500,500);

if(Ship_Flag == false)
{
for (int j =1 ;j<= Ship_Cont; j++)
{

button[j]->setGeometry(QRect(x[j] , y[j] , 12, 25));

button[j]->show();
}
}

}

void MainWindow::mousePressEvent(QMouseEvent *ev)
{
Ship_Cont++;
for(int i = 1 ; i<= Ship_Cont;i++)
{
QIcon icon("C:/Users/Pankaj/Desktop/ship1.jpg");
button[Ship_Cont] = new QPushButton(this);
button[Ship_Cont]->setIcon(icon);
button[Ship_Cont]->setStyleSheet("background-color: black");

button[Ship_Cont]->setStyleSheet( "border:none");

x[Ship_Cont] = ev->pos().x();
y [Ship_Cont]= ev->pos().y();

// qDebug()<<"Ship_Cont";
// qDebug()<<Ship_Cont;


button[Ship_Cont]->setGeometry(QRect(x[Ship_Cont] , y[Ship_Cont] , 12, 25));

button[Ship_Cont]->show();
}


}

void MainWindow::mouseMoveEvent ( QMouseEvent * event )
{
QToolTip::showText(event->globalPos(), QString::number( event->pos().x() ) + ", " + QString::number( event->pos().y() ) + ", " + "prabhat ",this, rect() );
}

void MainWindow::upd()
{
if (inc == true)
{


for(int i =1; i<=Ship_Cont;i++)
{
qDebug()<<"X";
qDebug()<< Ship_Cont;
qDebug()<< x[Ship_Cont];
qDebug()<<"Y";
qDebug()<< Ship_Cont;
qDebug()<< y[Ship_Cont];
x[i] = x[i]+10;

y[i] = y[i] +10;
}
update();
Ship_Flag = false;
}
}


void MainWindow::on_pushButton_clicked()
{
inc = true;

}



this is main.cpp


#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QMainWindow *window = new QMainWindow();


MainWindow w;
w.show();
return a.exec();
}


in this programm in mainwindow constructor i added timer and added SLOT (upd) with this.
on pushbutton clicked i did true the inc variable.
when inc will be true my timer will be start.In upd function value of x and y will be increase with 10 every time.
and now in paintevet i am passing update value of x and y so, button is moving. but i want every button will start to move when pushbutton will be click.