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