Qt-interest Archive, April 2007
QGraphicsView
Message 1 in thread
Hi,
I am facing two issues with using QT Graphics Framework.
1. contextMenuEvent() : I use the default context policy. For the
graphics item, the handler for context menu is invoked.
But when there is no graphic item under the cursor, I expect the
contextMenuEvent() handler of the QGraphicsView to be invoked.
But that does not happen. Does the handler of QGraphicsScene gets
invoked ? What is the flow like. It isnot clear from the documentation.
2. I want to handle the mousePressEvents of the individual graphicsItems
on the view. But those handlers, for indiviual items are never invoked,
when I
click the event gets captured in view itself. :-(
Would someone explain me the flow of event propogation.. The doc dosent
help :-(
Thnx
Prateek
Message 2 in thread
Prateek Tiwari wrote:
> 1. contextMenuEvent() : I use the default context policy. For the
> graphics item, the handler for context menu is invoked.
> But when there is no graphic item under the cursor, I expect the
> contextMenuEvent() handler of the QGraphicsView to be invoked.
> But that does not happen. Does the handler of QGraphicsScene gets
> invoked ? What is the flow like. It isnot clear from the documentation.
It should propagate, but doesn't... you might want to report this as a bug.
> 2. I want to handle the mousePressEvents of the individual graphicsItems
> on the view. But those handlers, for indiviual items are never invoked,
> when I
> click the event gets captured in view itself. :-(
Could you show some code, please? :-)
--
[ signature omitted ]
Message 3 in thread
Thnx Andreas,
For the second query, here is a compilable piece of code that highlights
the issue:-
I have created this from a larger project so there may be some unwanted
information.
//myview.h
#pragma once
#include <QMouseEvent>
#include <QRubberBand>
#include <QGraphicsScene>
#include <QDialog>
#include <QGraphicsView>
#include <QPainter>
#include <QPen>
#include <QTextEdit>
#include <QGraphicsRectItem>
#include <QGraphicsSceneContextMenuEvent>
#include <QLabel>
#include <QPushButton>
class __declspec(dllexport) TextRect : public QGraphicsRectItem
{
QGraphicsTextItem *m_textEdit;
QAction *m_copyAction, *m_pasteAction ;
protected:
virtual void contextMenuEvent ( QGraphicsSceneContextMenuEvent *
contextMenuEvent );
virtual void mousePressEvent(QMouseEvent *);
public:
TextRect(const QRectF & rect, QGraphicsItem * parent = 0,
QGraphicsScene * scene = 0);
};
//myview.cpp
#include <QAction>
#include <QList>
#include "myview.h"
///////////////TextRect//////////////////////////////
void TextRect::mousePressEvent(QMouseEvent *)
{
//Control never Reaches here
}
void TextRect::contextMenuEvent(QGraphicsSceneContextMenuEvent
*contextMenuEvent)
{
}
TextRect::TextRect(const QRectF &rect, QGraphicsItem *parent,
QGraphicsScene *scene) : QGraphicsRectItem(rect, parent, scene)
{
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
setBrush( QColor(204,200,0) );
m_textEdit = new QGraphicsTextItem("Hello", this);
m_textEdit->setTextInteractionFlags(Qt::TextEditable);
m_textEdit->setFlag(QGraphicsItem::ItemIsMovable);
m_textEdit->moveBy(rect.left(), rect.top());
}
//main.cpp
#include <qapplication.h>
#include "myview.h"
int main(int argc, char** argv)
{
QApplication app(argc,argv);
QGraphicsScene *scene = new QGraphicsScene();
scene->setSceneRect(QRectF(0,0,500,500));
TextRect *rect = new TextRect(QRectF(0,0,100,100));
scene->addItem(rect);
QGraphicsView m_view(scene);
m_view.show();
return app.exec();
}
The control never reaches the onmousepress event handler of the TextRect
object..
Thnx
Prateek
-----Original Message-----
From: Andreas Aardal Hanssen [mailto:ahanssen@xxxxxxxxxxxxx]
Sent: Thursday, April 26, 2007 6:01 PM
To: qt-interest@xxxxxxxxxxxxx
Subject: Re: QGraphicsView
Prateek Tiwari wrote:
> 1. contextMenuEvent() : I use the default context policy. For the
> graphics item, the handler for context menu is invoked.
> But when there is no graphic item under the cursor, I expect the
> contextMenuEvent() handler of the QGraphicsView to be invoked.
> But that does not happen. Does the handler of QGraphicsScene gets
> invoked ? What is the flow like. It isnot clear from the
documentation.
It should propagate, but doesn't... you might want to report this as a
bug.
> 2. I want to handle the mousePressEvents of the individual
> graphicsItems on the view. But those handlers, for indiviual items are
> never invoked, when I
> click the event gets captured in view itself. :-(
Could you show some code, please? :-)
--
[ signature omitted ]
Message 4 in thread
Hi Prateek,
>
> ///////////////TextRect//////////////////////////////
> void TextRect::mousePressEvent(QMouseEvent *)
> {
> //Control never Reaches here
>
> }
The signature in an QGraphicsItem is:
void QGraphicsItem::mousePressEvent ( QGraphicsSceneMouseEvent* event )
and not QMouseEvent *
\Ralf
--
[ signature omitted ]
Message 5 in thread
Thnx a lot. Tht was a silly mistake :-(
I have another issue.
When I create a QGraphicsItem, it does not move in the entire area of the QGraphicsView.
This is more pronounced, when I stretch the QGraphicsView window (That is my topmost window).
On stretching the mainwindow, the graphicsitem moves in an area that is a subset of the entire space available.
What is going on here ?
Thnx
Prateek
-----Original Message-----
From: Ralf Neubersch [mailto:rneuber1@xxxxxxxxxxxxxxx]
Sent: Thursday, April 26, 2007 6:26 PM
To: qt-interest@xxxxxxxxxxxxx
Subject: RE: QGraphicsView
Hi Prateek,
>
> ///////////////TextRect//////////////////////////////
> void TextRect::mousePressEvent(QMouseEvent *) {
> //Control never Reaches here
>
> }
The signature in an QGraphicsItem is:
void QGraphicsItem::mousePressEvent ( QGraphicsSceneMouseEvent* event )
and not QMouseEvent *
\Ralf
--
[ signature omitted ]
Message 6 in thread
Prateek Tiwari wrote:
> I have another issue.
> When I create a QGraphicsItem, it does not move in the entire area of the
> QGraphicsView.
> This is more pronounced, when I stretch the QGraphicsView window (That is
> my topmost window). On stretching the mainwindow, the graphicsitem moves
> in an area that is a subset of the entire space available.
> What is going on here ?
By stretch, I assume you mean resize? When you resize the window,
QGraphicsView uses QGraphicsView::resizeAnchor() to find out how to align
the scene. Items can be moved anywhere interactively. The view doesn't move
items; it merely aligns the scene to some anchor in the view (typically the
top-left corner).
Hope that helps, otherwise, it would help if you show the code that
illustrates your problem. :-)
--
[ signature omitted ]
Message 7 in thread
Thnx. That is what I was looking for.
I think when I resize the view, the scene rect would not grow
automatically and hence the movement of the
items are restricted to the scene rect dimension.
Thnx again
--Prateek
-----Original Message-----
From: Andreas Aardal Hanssen [mailto:ahanssen@xxxxxxxxxxxxx]
Sent: Friday, April 27, 2007 4:27 PM
To: qt-interest@xxxxxxxxxxxxx
Subject: RE: QGraphicsView
Prateek Tiwari wrote:
> I have another issue.
> When I create a QGraphicsItem, it does not move in the entire area of
> the QGraphicsView.
> This is more pronounced, when I stretch the QGraphicsView window
> (That is my topmost window). On stretching the mainwindow, the
> graphicsitem moves in an area that is a subset of the entire space
available.
> What is going on here ?
By stretch, I assume you mean resize? When you resize the window,
QGraphicsView uses QGraphicsView::resizeAnchor() to find out how to
align the scene. Items can be moved anywhere interactively. The view
doesn't move items; it merely aligns the scene to some anchor in the
view (typically the top-left corner).
Hope that helps, otherwise, it would help if you show the code that
illustrates your problem. :-)
--
[ signature omitted ]
Message 8 in thread
Hi
The event handler signature doesn't match.
void TextRect::mousePressEvent(QMouseEvent *)
should be:
void TextRect::mousePressEvent(QGraphicsSceneMouseEvent *)
--
[ signature omitted ]
Message 9 in thread
> 1. contextMenuEvent() : I use the default context policy. For the graphics
> item, the handler for context menu is invoked.
> But when there is no graphic item under the cursor, I expect the
> contextMenuEvent() handler of the QGraphicsView to be invoked.
> But that does not happen. Does the handler of QGraphicsScene gets invoked
> ? What is the flow like. It isnot clear from the documentation.
As far as I know, the flow is like this: view -> scene -> item(s). The
event propagation to items beneath stops in case an event handler
accepts the event.
As a side note, the event handler signature for the view is like any
other QWidget's:
void contextMenuEvent(QContextMenuEvent* event)
so not
void contextMenuEvent(QGraphicsSceneContextMenuEvent* event).
> 2. I want to handle the mousePressEvents of the individual graphicsItems on
> the view. But those handlers, for indiviual items are never invoked, when I
> click the event gets captured in view itself. :-(
The view receives the event first. Make sure you pass the event to the
base class implementation in case you have overridden the event
handler.
--
[ signature omitted ]