PDA

View Full Version : Graphics View mouse events



wlmirand
22nd February 2011, 15:02
Hi, im new with Qt and id like to learn a few simple things about event handling in QGraphicsView.

For now, id like to draw rects and be able to pop a messagebox with some info about clicked rect (position, size).

To achieve this, i put a QGraphicsView widget in my window and wrote a function that generate a random QRect and call myScene->addRect(myRect) to populate my scene.

Finally I use myScene->show();

Until here, is everything fine.

The problem is to write mouse event function. Ive googled a lot and read Graphics View Framework (http://doc.trolltech.com/4.7/graphicsview.html) documentation too and i still dont understand how events get propagated from QGraphicsView, to QGraphicsScene, and then to QGraphicsItem

As I seen on "Diagram Scene" example, Ive create a class that "extends" QGraphicsScene and I reimplemented "void mousePressEvent(...)", but it didnt worked.

My question is: Which classes I must inherit, to override mouse events, and where should i handle them.

Thanks in advance

helloworld
22nd February 2011, 19:44
Ive create a class that "extends" QGraphicsScene and I reimplemented "void mousePressEvent(...)", but it didnt worked.

In what way doesn't it work? It would also help if you provide the piece of code that contains your implementation of mousePressEvent.

wlmirand
23rd February 2011, 01:16
Hi. The function mousePressEvent doesnt "run" when I click. Here is the code:

customscene.h


#ifndef CUSTOMSCENE_H
#define CUSTOMSCENE_H

#include <QGraphicsScene>
#include <QGraphicsSceneEvent>

class CustomScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit CustomScene(QObject *parent = 0);

signals:

public slots:

protected:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);

};

#endif // CUSTOMSCENE_H


customscene.cpp


#include "customscene.h"
#include <QMessageBox>

CustomScene::CustomScene(QObject *parent) :
QGraphicsScene(parent)
{

}

void CustomScene::mousePressEvent(QGraphicsSceneMouseEv ent *mouseEvent)
{
QMessageBox msg;

msg.setText("clicked"); //a test to see if the function is called
msg.exec();
}


mainwindow.cpp



CustomScene *scene;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

scene = new CustomScene;
ui->GraphicsView->setScene(scene);
}

void MainWindow::on_button_clicked()
{
scene->addRect(0,0,630,280);//draw a big rect

QPen borda;
borda.setStyle(Qt::DashDotLine);
borda.setWidth(2);
borda.setBrush(Qt::green);
borda.setCapStyle(Qt::RoundCap);
borda.setJoinStyle(Qt::RoundJoin);

QBrush fundo;
fundo.setStyle(Qt::SolidPattern);
fundo.setColor(QColor(0,0,255,85));

QRect rect(30,30,60,60); //draw a small rect inside

scene->addRect(rect, borda, fundo);

ui->GraphicsView->show();
}


It was supposed to pop the MessageBox when I click the GraphicsView, but nothing happens

helloworld
23rd February 2011, 08:43
That seems ok, as far as I can see. Check the geometry of the GraphicsView object, i.e. that it is actually visible in the MainWindow. Maybe it has 0 size or is outside of the window's visible area or something.

Added after 14 minutes:

Also make sure that you haven't unchecked the 'enabled' checkbox for the GraphicsView in the UI designer and that the 'interactive' property is set to true.



ui->GraphicsView->setInteractive(true);

wlmirand
23rd February 2011, 12:14
Confirmed now: Both Interactive and Enabled property at QT Creator are checked (I dont change them in my code), and it still doesnt work.

Ill continue trying to figure out whats happening

Edit: I didnt set anything about geometry. I just draw a big rect, that "acts" like borders occuping all the widget's area, and then i draw a small one inside. Both are shown in my window.

Here is a screenshot of my UI:

http://www.grad.icmc.usp.br/~wlmirand/fotos/ui.png

wlmirand
23rd February 2011, 15:13
After a few hours, I just decided make a new project and try implement it alone.... It worked perfectly, So I figured out, that My Layouts construction was somehow blocking the events.

I Just moved GraphicsView Widget out of the Layout Widget, and it worked. Ill try find why.