PDA

View Full Version : yet another easy task i cant figure out



sincnarf
15th October 2007, 04:53
sorry guys for my noobness but i have another problem

Given this custom graphics view... Whenever I press a mouse button a messagebox should appear but it's not. But when I change the QGraphicsSceneMouseEvent into QMouseEvent, the message box appears. And that is weird.

My graphicsView by the way has a pre loaded image (so this means it already has a default graphicsscene)


#include <QtGui>
#include "iadfuqgraphicsview.h"

IADFUQGraphicsView::IADFUQGraphicsView(QGraphicsSc ene *scene, QWidget* parent) :
QGraphicsView(scene, parent) {
setAcceptDrops(true);
setAttribute(Qt::WA_InputMethodEnabled);
setBackgroundRole(QPalette::Base);
setDragMode(QGraphicsView::RubberBandDrag);
setInteractive(true);
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
setBackgroundBrush(QPixmap(":/images/background.png"));
setAutoFillBackground(true);
#ifdef QT_OPENGL_SUPPORT
setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
#endif
// QGraphicsPixmapItem *m_pixmapItem = new QGraphicsPixmapItem(QPixmap::fromImage(QImage(":/images/circle.png")), 0, scene);

}

IADFUQGraphicsView::IADFUQGraphicsView(QWidget* parent) :
QGraphicsView(parent) {
setAcceptDrops(true);
setAttribute(Qt::WA_InputMethodEnabled);
setBackgroundRole(QPalette::Base);
setDragMode(QGraphicsView::RubberBandDrag);
setInteractive(true);
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
setBackgroundBrush(QPixmap(":/images/background.png"));
setAutoFillBackground(true);
#ifdef QT_OPENGL_SUPPORT
setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
#endif
}

void IADFUQGraphicsView::mousePressEvent(QGraphicsScene MouseEvent *event) {

QMessageBox::information(this, tr("Problem"), tr("Why isn't this message box showing?"));

}

jpn
15th October 2007, 04:57
It's because QGraphicsView is a QWidget, not QGraphicsItem. Notice the difference between QWidget::mousePressEvent() and QGraphicsItem::mousePressEvent().

sincnarf
15th October 2007, 05:12
So i have to create my own graphics items ... I see. Oh well

I originally wantedd to implement drawing ellipses whenever a mouse click is done on a graphics view

sincnarf
15th October 2007, 06:42
iadfuqgraphicsitem.cpp - I subclassed QGraphicsPixmapItem


#include <QtGui>
#include "iadfuqgraphicsitem.h"

IADFUQGraphicsItem::IADFUQGraphicsItem(QGraphicsIt em * parent) :
QGraphicsPixmapItem(parent) {
setAcceptDrops (false);
setAcceptsHoverEvents(true);
setEnabled(true);
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable
| QGraphicsItem::ItemIgnoresTransformations);
setVisible(true);
setZValue(1);
}

void IADFUQGraphicsItem::mousePressEvent(QGraphicsScene MouseEvent *event) {
QMessageBox::information(0, tr("!!!!!!!"), tr("Y!!!!"));
}

void IADFUQGraphicsItem::mouseMoveEvent(QGraphicsSceneM ouseEvent * event) {
}
void IADFUQGraphicsItem::mouseReleaseEvent(QGraphicsSce neMouseEvent * event) {
}


iadfuqgraphicsitem.h


#ifndef IADFUQGRAPHICSITEM_H
#define IADFUQGRAPHICSITEM_H
#include <QGraphicsPixmapItem>
#include <QObject>
class QGraphicsSceneMouseEvent;
class IADFUQGraphicsItem : public QObject, public QGraphicsPixmapItem {
Q_OBJECT
public:
IADFUQGraphicsItem(QGraphicsItem* parent = 0);
protected:
void mousePressEvent ( QGraphicsSceneMouseEvent * event );
void mouseMoveEvent ( QGraphicsSceneMouseEvent * event );
void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event );
};
#endif



is this the right way to create a new IADFUQGraphicsItem?

IADFUQGraphicsItem *item = qobject_cast<IADFUQGraphicsItem *>(someQGraphicsPixmapItem);

sincnarf
15th October 2007, 08:58
Okay this is what I did



IADFUQGraphicsItem
*item = qgraphicsitem_cast<IADFUQGraphicsItem *>(items.first());


Now how can I add the custom graphics item to a graphics view? Casting item again to



QGraphicsPixmapItem
*pixmapItem = qgraphicsitem_cast<QGraphicsPixmapItem *>(item);

then using scene->addPixmap(pixmapItem->pixmap()) does not show the message box at all when i click on the item. Do I need to create a method just like addPixmap that accepts my custom graphics item?

sincnarf
16th October 2007, 05:46
Okay solved this...

just used addItem()

the item must be set as
item->setSelected(true)

cost me 5 hours