PDA

View Full Version : Problem with MouseEvents



pedrohs
4th December 2014, 10:54
I have this in my Dialog header:

QGraphicsScene *scene;
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent*);
void mouseReleaseEvent(QMouseEvent *);
And this in my cpp:


scene=new QGraphicsScene(0,0,800,600);
ui->graphicsView->setScene(scene);
QPixmap p2("C:/Users/Pedro/Desktop/imagens/6.jpg");
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(p2);
scene->addItem(item);

Is possible to open a new window or to do any action when the item is clicked? Sorry people, I'm begginer. Can you help me, please?

void Dialog::mousePressEvent(QMouseEvent * e) {

}

wysota
4th December 2014, 12:06
Subclass the item class and reimplement its mousePressEvent handler. Or subclass the scene or the view and reimplement mousePressEvent there. However in the latter two cases you will first have to detect if you are pointing on an item.

pedrohs
5th December 2014, 04:17
I did what you said. But now I need to set a QPixmap for my subclass.
What can I do in the constructor to replace this part of my code?


QPixmap p2("C:/Users/Pedro/Desktop/imagens/6.jpg");
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(p2);
scene->addItem(item);

wysota
5th December 2014, 09:36
Replace QGraphicsPixmapItem with the class you created.

pedrohs
5th December 2014, 19:49
You saved my life. Thank you.