Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsSvgItem and mousePressEvent troubles
QtWS25 Last Chance

QGraphicsSvgItem and mousePressEvent troubles

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hiwa
    wrote on last edited by
    #1

    The following program draws a red SVG rectangle in a white window.
    When you click on the white area, you'll see a messagebox with the text "QGraphicsView (White)".
    When you click on the red rectangle, I'd expect to see a messagebox with the text "QGraphicsSvgItem (Red)".
    However, I see "QGraphicsView (White)" instead.
    Can anyone point out what I'm doing wrong here ?

    [code]

    //------------------------------------------------------------------------------
    #include <QtGui/QApplication>
    #include <QtGui/QGraphicsView>
    #include <QtGui/QMessageBox>
    #include <QtGui/QMouseEvent>
    #include <QtSvg/QGraphicsSvgItem>
    #include <QtSvg/QSvgRenderer>
    //------------------------------------------------------------------------------
    class View : public QGraphicsView
    {
    Q_OBJECT
    public:
    View() : QGraphicsView()
    {
    }

    protected:

    void mousePressEvent(QMouseEvent *event)
    {
    QMessageBox::warning(0, "QGraphicsView (White)",
    "QGraphicsView (White)",
    QMessageBox::Ok,
    QMessageBox::Ok);
    }
    };
    //------------------------------------------------------------------------------
    class SvgItem : public QGraphicsSvgItem
    {
    public:
    SvgItem(QGraphicsItem * p = NULL ) :
    QGraphicsSvgItem( p )
    {
    setFlag( QGraphicsItem::ItemIsSelectable );
    setFlag( QGraphicsItem::ItemIsMovable );

    QSvgRenderer* mySvgRenderer;
    QByteArray svgData = QByteArray("<svg><rect x="0" y="0" width="50" height="50" fill="red" stroke="black" /></svg>");
    mySvgRenderer = new QSvgRenderer(svgData, this);

    QRect svgSize = mySvgRenderer->viewBox();
    this->setSharedRenderer(mySvgRenderer);

    }
    ~SvgItem()
    {
    }
    protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    QGraphicsItem::mousePressEvent(event);
    QMessageBox::warning(0, "QGraphicsSvgItem (Red)",
    "QGraphicsSvgItem (Red)",
    QMessageBox::Ok,
    QMessageBox::Ok);
    }
    };
    //------------------------------------------------------------------------------
    #include "main.moc"
    //------------------------------------------------------------------------------
    int main( int argc, char ** argv )
    {
    QApplication app(argc, argv );

    View * view = new View();

    QGraphicsScene * scene = new QGraphicsScene( 0, 0, 150, 150 );

    SvgItem * rect = new SvgItem();
    scene->addItem( rect );

    rect->setAcceptDrops( true );
    rect->setAcceptHoverEvents( true );
    rect->setPos(50,50);

    view->setScene( scene );
    view->show();

    return( app.exec() );
    }
    //------------------------------------------------------------------------------
    [/code]

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      This is easy to understand. QGraphicsView is a QWidget, but QGraphicsSvgItem is not a QWidget, it just a item located in QGraphicsScene.

      QGraphicsView(Receive mouseEvent) ==>QGraphicsScene ==> QGraphicsXXXItem

      In your View class, you have override the mousePressEvent, but you forget to call mousePressEvent of it's base class.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hiwa
        wrote on last edited by
        #3

        I got it.
        Thanks

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved