#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QMainWindow>
#include<QMouseEvent>
#include <QPushButton>
#include<QLabel>
namespace Ui
{
class MainWindow;
class MyWidget;
}
{
Q_OBJECT
public:
~MainWindow();
bool inc;
double x[100];
double y[100];
private:
Ui::MainWindow *ui;
public slots:
void upd();
private slots:
void on_pushButton_clicked();
};
#endif // MAINWINDOW_H
#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
To copy to clipboard, switch view to plain text mode
#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;
bool Ship_Flag = true;
//double x[100];
//double y[100];
MainWindow
::MainWindow(QWidget *parent
){
MyWidget *wid;
ui->setupUi(this);
wid = ui->widget;
inc =false;
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->setInterval(1000);
timer->start();
connect(timer, SIGNAL(timeout()), this, SLOT(upd()));
}
MainWindow::~MainWindow()
{
delete ui;
}
{
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_Cont++;
for(int i = 1 ; i<= Ship_Cont;i++)
{
QIcon icon
("C:/Users/Pankaj/Desktop/ship1.jpg");
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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks