Mouse Press/Release Events
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
Code:
#ifndef APP_H
#define APP_H
#include <QMainWindow>
#include <QGraphicsScene>
#include <QPoint>
#include <QMouseEvent>
protected:
private:
int x,x1,y,y1;
#endif
Code:
#include "app.h"
#include "ui_app.h"
ui(new Ui::APP)
{
ui->setupUi (this);
}
{
x=point.x();
qDebug()<<x;
y=point.y();
qDebug()<<y
}
{
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.
Re: Mouse Press/Release Events
Call base class implementations of these event handlers after your class-specific code
Code:
{
//... my code
}
{
// ... my code
}
Re: Mouse Press/Release Events
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.
Re: Mouse Press/Release Events
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.
Re: Mouse Press/Release Events
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.
Re: Mouse Press/Release Events
Ya. I tried to implement both Mouse click and release events still the same release is not working on graphicsview.
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
Code:
#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
Code:
#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;
}
}
Re: Mouse Press/Release Events
Quote:
Originally Posted by
Vivek1982
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,
_
Re: Mouse Press/Release Events
I tried with event filter. i didnt get both press and release positions on GraphicsScene widget.I saw examples in
http://stackoverflow.com/questions/7...s-not-register
but still working on, how ever it is mouse press is okay.. but release pos is not coming.
Re: Mouse Press/Release Events
Tried you implementing the graphicsview's eventhandlers?
Cheers,
_
Re: Mouse Press/Release Events
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
Code:
#include <QDebug>
namespace Ui {
class MainWindow;
}
Q_OBJECT
public:
~MainWindow();
protected:
private:
Ui::MainWindow *ui;
float x,y,x1,y1;
};
{
Q_OBJECT
public:
MyGraphicsView
(QWidget *parent
=NULL);
~MyGraphicsView();
protected:
};
Code:
{
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)
{
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;
}
}
Re: Mouse Press/Release Events
Quote:
Originally Posted by
Vivek1982
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,
_
Re: Mouse Press/Release Events
Ya.. the same is implemented. event handlers are there in QGraphicView only. but to check for the positions im using QGraphicSceneMousePress events...
Re: Mouse Press/Release Events
Quote:
Originally Posted by
Vivek1982
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,
_
Re: Mouse Press/Release Events
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.
Re: Mouse Press/Release Events
Quote:
Originally Posted by
Vivek1982
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,
_
Re: Mouse Press/Release Events
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:)
Re: Mouse Press/Release Events
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:)
Re: Mouse Press/Release Events
Quote:
calling paint event from switch (event filter) is not possible
Store the mouse coordinates and use them later in paintEvent.
Re: Mouse Press/Release Events
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...
Re: Mouse Press/Release Events
Quote:
by releasing the mouse button it has to draw line
Call "update()" after storing the coordinates and releasing the mouse button.