Ya. I tried to implement both Mouse click and release events still the same release is not working on graphicsview.
#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);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPointF>
#include <QMouseEvent>
#include <QDebug>
namespace Ui {
class MainWindow;
}
Q_OBJECT
public:
~MainWindow();
protected:
protected:
private:
Ui::MainWindow *ui;
float x,y,x1,y1;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPointF>
#include <QMouseEvent>
#include <QDebug>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
protected:
void mousePressEvent(QMouseEvent * e);
void mouseReleaseEvent(QMouseEvent * e);
private:
Ui::MainWindow *ui;
QGraphicsView *view;
QGraphicsScene *scene;
QPointF *point,*point1;
float x,y,x1,y1;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->graphicsView->setScene(scene);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
{
if(e->MouseButtonPress)
{
qDebug("clicked");
QPointF point
= ui
->graphicsView
->mapFromScene
(e
->posF
());
x=point.x();
qDebug()<<x;
y= point.y();
qDebug()<<y;
qDebug()<<point;
}
}
{
if(e->MouseButtonRelease)
{
qDebug("released");
QPointF point1
= ui
->graphicsView
->mapFromScene
(e
->posF
());
x1=point1.x();
qDebug()<<x1;
y1= point1.y();
qDebug()<<y1;
qDebug()<<point1;
}
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene=new QGraphicsScene(0,0,740,400);
ui->graphicsView->setScene(scene);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::mousePressEvent(QMouseEvent *e)
{
if(e->MouseButtonPress)
{
qDebug("clicked");
QPointF point= ui->graphicsView->mapFromScene(e->posF());
x=point.x();
qDebug()<<x;
y= point.y();
qDebug()<<y;
qDebug()<<point;
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{
if(e->MouseButtonRelease)
{
qDebug("released");
QPointF point1= ui->graphicsView->mapFromScene(e->posF());
x1=point1.x();
qDebug()<<x1;
y1= point1.y();
qDebug()<<y1;
qDebug()<<point1;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks