Results 1 to 6 of 6

Thread: yet another easy task i cant figure out

  1. #1
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default yet another easy task i cant figure out

    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)
    Qt Code:
    1. #include <QtGui>
    2. #include "iadfuqgraphicsview.h"
    3.  
    4. IADFUQGraphicsView::IADFUQGraphicsView(QGraphicsScene *scene, QWidget* parent) :
    5. QGraphicsView(scene, parent) {
    6. setAcceptDrops(true);
    7. setAttribute(Qt::WA_InputMethodEnabled);
    8. setBackgroundRole(QPalette::Base);
    9. setDragMode(QGraphicsView::RubberBandDrag);
    10. setInteractive(true);
    11. setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    12. setBackgroundBrush(QPixmap(":/images/background.png"));
    13. setAutoFillBackground(true);
    14. #ifdef QT_OPENGL_SUPPORT
    15. setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    16. #endif
    17. // QGraphicsPixmapItem *m_pixmapItem = new QGraphicsPixmapItem(QPixmap::fromImage(QImage(":/images/circle.png")), 0, scene);
    18.  
    19. }
    20.  
    21. IADFUQGraphicsView::IADFUQGraphicsView(QWidget* parent) :
    22. QGraphicsView(parent) {
    23. setAcceptDrops(true);
    24. setAttribute(Qt::WA_InputMethodEnabled);
    25. setBackgroundRole(QPalette::Base);
    26. setDragMode(QGraphicsView::RubberBandDrag);
    27. setInteractive(true);
    28. setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    29. setBackgroundBrush(QPixmap(":/images/background.png"));
    30. setAutoFillBackground(true);
    31. #ifdef QT_OPENGL_SUPPORT
    32. setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    33. #endif
    34. }
    35.  
    36. void IADFUQGraphicsView::mousePressEvent(QGraphicsSceneMouseEvent *event) {
    37.  
    38. QMessageBox::information(this, tr("Problem"), tr("Why isn't this message box showing?"));
    39.  
    40. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sincnarf; 15th October 2007 at 05:54. Reason: wrong punctuation
    Image Analysis Development Framework Using Qt (IADFUQ)

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: yet another easy task i cant figure out

    It's because QGraphicsView is a QWidget, not QGraphicsItem. Notice the difference between QWidget::mousePressEvent() and QGraphicsItem::mousePressEvent().
    J-P Nurmi

  3. #3
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: yet another easy task i cant figure out

    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
    Last edited by sincnarf; 15th October 2007 at 06:21. Reason: follow up
    Image Analysis Development Framework Using Qt (IADFUQ)

  4. #4
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: yet another easy task i cant figure out

    iadfuqgraphicsitem.cpp - I subclassed QGraphicsPixmapItem
    Qt Code:
    1. #include <QtGui>
    2. #include "iadfuqgraphicsitem.h"
    3.  
    4. IADFUQGraphicsItem::IADFUQGraphicsItem(QGraphicsItem * parent) :
    5. setAcceptDrops (false);
    6. setAcceptsHoverEvents(true);
    7. setEnabled(true);
    8. setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable
    9. | QGraphicsItem::ItemIgnoresTransformations);
    10. setVisible(true);
    11. setZValue(1);
    12. }
    13.  
    14. void IADFUQGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
    15. QMessageBox::information(0, tr("!!!!!!!"), tr("Y!!!!"));
    16. }
    17.  
    18. void IADFUQGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
    19. }
    20. void IADFUQGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) {
    21. }
    To copy to clipboard, switch view to plain text mode 

    iadfuqgraphicsitem.h
    Qt Code:
    1. #ifndef IADFUQGRAPHICSITEM_H
    2. #define IADFUQGRAPHICSITEM_H
    3. #include <QGraphicsPixmapItem>
    4. #include <QObject>
    5. class IADFUQGraphicsItem : public QObject, public QGraphicsPixmapItem {
    6. Q_OBJECT
    7. public:
    8. IADFUQGraphicsItem(QGraphicsItem* parent = 0);
    9. protected:
    10. void mousePressEvent ( QGraphicsSceneMouseEvent * event );
    11. void mouseMoveEvent ( QGraphicsSceneMouseEvent * event );
    12. void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event );
    13. };
    14. #endif
    To copy to clipboard, switch view to plain text mode 

    is this the right way to create a new IADFUQGraphicsItem?
    Qt Code:
    1. IADFUQGraphicsItem *item = qobject_cast<IADFUQGraphicsItem *>(someQGraphicsPixmapItem);
    To copy to clipboard, switch view to plain text mode 
    Image Analysis Development Framework Using Qt (IADFUQ)

  5. #5
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: yet another easy task i cant figure out

    Okay this is what I did

    Qt Code:
    1. IADFUQGraphicsItem
    2. *item = qgraphicsitem_cast<IADFUQGraphicsItem *>(items.first());
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. *pixmapItem = qgraphicsitem_cast<QGraphicsPixmapItem *>(item);
    To copy to clipboard, switch view to plain text mode 

    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?
    Last edited by sincnarf; 15th October 2007 at 10:02. Reason: code
    Image Analysis Development Framework Using Qt (IADFUQ)

  6. #6
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: yet another easy task i cant figure out

    Okay solved this...

    just used addItem()

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

    cost me 5 hours
    Image Analysis Development Framework Using Qt (IADFUQ)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.