PDA

View Full Version : Mouse Press/Release Events



Vivek1982
22nd July 2014, 05:41
Dear All,

Im getting mouseevent at when i click the mouse on the GraphicsScene. Im getting the position/co-ordinate also, where as release event im getting the co-ordinate/positions. please Suggest me


#ifndef APP_H
#define APP_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QPoint>
#include <QMouseEvent>

protected:
void changeEvent(QEvent *e)
void mousePressEvent (QMouseEvent *e)
void mouseReleaseEvent (QMouseEvent *e)

private:
int x,x1,y,y1;
QPoint *point;

#endif





#include "app.h"
#include "ui_app.h"

APP::APP(QWidget *parent):

QMainWindow(parent),
ui(new Ui::APP)
{
ui->setupUi (this);
}

void APP::mousePressEvent(QMouseEvent *e)
{
QPoint point=e->pos();
x=point.x();
qDebug()<<x;

y=point.y();
qDebug()<<y

}

void APP::mouseReleaseEvent(QMouseEvent *e)
{
QPoint point=e->pos();
x1=point.x();
qDebug()<<x1;

y1=point.y();
qDebug()<<y1;

}



Im getting x,y position data of Graphic scene when i click on scene, but not x1, y1 positions when i release mouse. By getting all co-ordinates i can draw line.

stampede
22nd July 2014, 19:23
Call base class implementations of these event handlers after your class-specific code


void APP::mousePressEvent(QMouseEvent *e)
{
//... my code
QMainWindow::mousePressEvent(e);
}

void APP::mouseReleaseEvent(QMouseEvent *e)
{
// ... my code
QMainWindow::mouseReleaseEvent(e);
}

Vivek1982
24th July 2014, 13:01
Hi..

I tried by calling base class, but still same result. Im getting only MousePressEvent Co-ordinates, but not the Co-ordinates where i released the Mouse. When I click mouse after dragging I get mouse click event co-ordinates.

Vivek1982
4th August 2014, 10:16
Thanks alot for your replies. I got both the co-orodinates of Mouse on click and release events. Actually in my widget as QMainWindow. I have QGraphicsScene and QGraphicsView placed in my QMainWindow. scene has been created inside MainWindow.

Im getting mouse press and release event or positions on QMainWindow, whereas when i click on my GraphicScene area, im not getting press/release events on Scene. outside the graphicscene area im getting it..

How can i get the co-ordinates on graphicScene area.

ChrisW67
4th August 2014, 21:16
If you want the click events when the mouse is over the QGraphicsView widget then you can either:

Reimplement and use the mouseClickEvent() and mouseReleaseEvent() of the QGraphicsView object.
Install an event filter on the QGraphicsView object.

Vivek1982
5th August 2014, 13:35
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[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}






#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






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

}

anda_skoa
5th August 2014, 16:21
Ya. I tried to implement both Mouse click and release events still the same release is not working on graphicsview.

Instead of implementing the main window's event handler, you could try what ChrisW67 suggested.

Cheers,
_

Vivek1982
6th August 2014, 09:03
I tried with event filter. i didnt get both press and release positions on GraphicsScene widget.I saw examples in
http://stackoverflow.com/questions/7725174/qgraphicsview-mouserelease-event-does-not-register

but still working on, how ever it is mouse press is okay.. but release pos is not coming.

anda_skoa
6th August 2014, 12:52
Tried you implementing the graphicsview's eventhandlers?

Cheers,
_

Vivek1982
7th August 2014, 04:52
Yes. reimplemented by sub class QGraphicsView and also calling event handlers of QGraphicsView. Im getting error like :-1: error: collect2: ld returned 1 exit status



#include <QDebug>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();

protected:
void changeEvent(QEvent *e);


private:
Ui::MainWindow *ui;
QGraphicsView *view;
QGraphicsScene *scene;
QPointF *point,*point1;
float x,y,x1,y1;
};
class MyGraphicsView : public QGraphicsView
{
Q_OBJECT
public:
MyGraphicsView(QWidget *parent=NULL);
~MyGraphicsView();

protected:
void mousePressEvent(QGraphicsSceneMouseEvent * event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);

};




void MyGraphicsView::mousePressEvent(QGraphicsSceneMous eEvent *event)
{

if(event->MouseButtonPress)
{
qDebug("clicked");
QPointF point= event->pos();//ui->graphicsView->mapFromScene(event->pos());
//x=point.x();
//qDebug()<<x;
//y= point.y();
//qDebug()<<y;
qDebug()<<point;

}
}



//void MainWindow::mouseReleaseEvent(QMouseEvent *event)
void MyGraphicsView::mouseReleaseEvent(QGraphicsSceneMo useEvent *event)
{

if(event->MouseButtonRelease)
{
qDebug("released");
QPointF point1=event->pos();//ui->graphicsView->mapFromScene(event->pos());
//x1=point1.x();
//qDebug()<<x1;
//y1= point1.y();
//qDebug()<<y1;
qDebug()<<point1;
}

}

anda_skoa
7th August 2014, 07:26
Yes. reimplemented by sub class QGraphicsView and also calling event handlers of QGraphicsView. Im getting error like :-1: error: collect2: ld returned 1 exit status

You'll have to post the full error message, really.

Also it would help a lot if you implemented the event handler methods of QGraphicsView, not those of QGraphicsScene.
Unless your goal is to have methods that are never called, in which case that is of course fine.

Cheers,
_

Vivek1982
7th August 2014, 13:32
Ya.. the same is implemented. event handlers are there in QGraphicView only. but to check for the positions im using QGraphicSceneMousePress events...

anda_skoa
7th August 2014, 14:06
Ya.. the same is implemented. event handlers are there in QGraphicView only.

Your code above did not. There is are no such two methods in QGraphicsView.

Cheers,
_

Vivek1982
8th August 2014, 04:43
ok. let me tell the approach how to get mousevents on QGraphicView. Initially i have mainwindow widget along with the Ui file. In the Ui file i have dragged a Graphic view to mainwindow ui. when my mouse events are working fine on mainwindow widget. or i need to create separate mouseevent.cpp and mouseevent.h and call the positions in mainwindow as seen in scriblearea example. suggest how it can be done in other ways.
Or help with steps/procedure to how to set event filter to get mouse events on graphicview on mainwindow.

anda_skoa
8th August 2014, 05:57
suggest how it can be done in other ways.

You could create a subclass if QGraphicsView and implement the mouse event handler methods. Then promote the GraphicsView in designer to that class.

Cheers,
_

Vivek1982
8th August 2014, 06:33
ok. will create my Ui form in mainwindow form. But declaration of GraphicsView to be done in sub class mouseevent.cpp and mouseevent.h, which is not having the Ui form. Im confused by seeing Scribblearea example. Here positions are collected from scribblearea.cpp and same is brought on mainwindow of class. Let me give a try. or i will post the code. based on comments i will try to do it. Thats wat i can do now:)

Vivek1982
11th August 2014, 14:34
Dear All,

Finally I got mouse press/release event on Graphicscene by eventfilter. Im getting the co-ordinates by mouse events. For the same positions to draw lines I have made paint event,but calling paint event from switch (event filter) is not possible. Any methods to call the paint event.

Thanks in advance:)

stampede
11th August 2014, 14:54
calling paint event from switch (event filter) is not possible
Store the mouse coordinates and use them later in paintEvent.

Vivek1982
11th August 2014, 16:06
ya.. Im able to get mouse positions on QpointF .. but in event filter points are stored .. by releasing the mouse button it has to draw line.. in header file i have void PaintEvent (QPaintEvent *event ).
but it has to be called after release button is left...
In header.cpp i have void MainWindow::PaintEvent then following by painter pen then draw line.. the positions are from event filter...

stampede
11th August 2014, 16:27
by releasing the mouse button it has to draw line
Call "update()" after storing the coordinates and releasing the mouse button.

Vivek1982
12th August 2014, 07:36
Im not able to access the values of Mouse press/release events from Case events to and not able to carry the same values to void MainWindow::paintEvent(QPaintEvent *e).
To draw the line, I have called update() function at Mouse release case in event filter.Im getting all junk or Hexa values in qDeug...

anda_skoa
12th August 2014, 10:25
And you want us to guess what is wrong?

Vivek1982
12th August 2014, 12:35
Sorry. Im not able to point it out. In case QEvent::GraphicsSceneMouseRelease: Im getting the ScenePos, but when it is called in void MainWindow::paintEvent(QPaintEvent *e) same data/value is not coming. Or im getting the address of it or wat. Im feeling wrong somewhere, but finding it hard to correct it

Added after 51 minutes:

Yes I have stored in Case events in int x,y,x1,y1 for respective points. while calling same x,y,x1,y1 im not getting it when draw line or void MainWindow::paintEvent(QPaintEvent *e)

anda_skoa
12th August 2014, 13:09
This is pointless.

Unless you actually show the code where you store and retrieve the values, there is nothing anyone else can do.

Cheers,
_

Vivek1982
22nd August 2014, 12:16
Hi..
I have attached the code in a zip file. I have to store the co-ordinates but mouse points are not getting stored in this variable for paint on scene.

anda_skoa
22nd August 2014, 12:57
If you want to have access to the values from a different method, you should store them in the member variables, not in local variables.

Cheers,
_

Vivek1982
22nd August 2014, 13:14
ok.. plz can i get link supporting on member varaible or how it can be used in my condition to store data and process while painting

stampede
22nd August 2014, 13:17
can i get link supporting on member varaible
http://www.cplusplus.com/doc/tutorial/classes/