Results 1 to 6 of 6

Thread: Graphics View mouse events

  1. #1
    Join Date
    Feb 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Graphics View mouse events

    Hi, im new with Qt and id like to learn a few simple things about event handling in QGraphicsView.

    For now, id like to draw rects and be able to pop a messagebox with some info about clicked rect (position, size).

    To achieve this, i put a QGraphicsView widget in my window and wrote a function that generate a random QRect and call myScene->addRect(myRect) to populate my scene.

    Finally I use myScene->show();

    Until here, is everything fine.

    The problem is to write mouse event function. Ive googled a lot and read Graphics View Framework documentation too and i still dont understand how events get propagated from QGraphicsView, to QGraphicsScene, and then to QGraphicsItem

    As I seen on "Diagram Scene" example, Ive create a class that "extends" QGraphicsScene and I reimplemented "void mousePressEvent(...)", but it didnt worked.

    My question is: Which classes I must inherit, to override mouse events, and where should i handle them.

    Thanks in advance

  2. #2
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: Graphics View mouse events

    Ive create a class that "extends" QGraphicsScene and I reimplemented "void mousePressEvent(...)", but it didnt worked.
    In what way doesn't it work? It would also help if you provide the piece of code that contains your implementation of mousePressEvent.

  3. #3
    Join Date
    Feb 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Graphics View mouse events

    Hi. The function mousePressEvent doesnt "run" when I click. Here is the code:

    customscene.h
    Qt Code:
    1. #ifndef CUSTOMSCENE_H
    2. #define CUSTOMSCENE_H
    3.  
    4. #include <QGraphicsScene>
    5. #include <QGraphicsSceneEvent>
    6.  
    7. class CustomScene : public QGraphicsScene
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit CustomScene(QObject *parent = 0);
    12.  
    13. signals:
    14.  
    15. public slots:
    16.  
    17. protected:
    18. void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    19.  
    20. };
    21.  
    22. #endif // CUSTOMSCENE_H
    To copy to clipboard, switch view to plain text mode 

    customscene.cpp
    Qt Code:
    1. #include "customscene.h"
    2. #include <QMessageBox>
    3.  
    4. CustomScene::CustomScene(QObject *parent) :
    5. {
    6.  
    7. }
    8.  
    9. void CustomScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    10. {
    11.  
    12. msg.setText("clicked"); //a test to see if the function is called
    13. msg.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. CustomScene *scene;
    2.  
    3. MainWindow::MainWindow(QWidget *parent) :
    4. QMainWindow(parent),
    5. ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8.  
    9. scene = new CustomScene;
    10. ui->GraphicsView->setScene(scene);
    11. }
    12.  
    13. void MainWindow::on_button_clicked()
    14. {
    15. scene->addRect(0,0,630,280);//draw a big rect
    16.  
    17. QPen borda;
    18. borda.setStyle(Qt::DashDotLine);
    19. borda.setWidth(2);
    20. borda.setBrush(Qt::green);
    21. borda.setCapStyle(Qt::RoundCap);
    22. borda.setJoinStyle(Qt::RoundJoin);
    23.  
    24. QBrush fundo;
    25. fundo.setStyle(Qt::SolidPattern);
    26. fundo.setColor(QColor(0,0,255,85));
    27.  
    28. QRect rect(30,30,60,60); //draw a small rect inside
    29.  
    30. scene->addRect(rect, borda, fundo);
    31.  
    32. ui->GraphicsView->show();
    33. }
    To copy to clipboard, switch view to plain text mode 

    It was supposed to pop the MessageBox when I click the GraphicsView, but nothing happens

  4. #4
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: Graphics View mouse events

    That seems ok, as far as I can see. Check the geometry of the GraphicsView object, i.e. that it is actually visible in the MainWindow. Maybe it has 0 size or is outside of the window's visible area or something.


    Added after 14 minutes:


    Also make sure that you haven't unchecked the 'enabled' checkbox for the GraphicsView in the UI designer and that the 'interactive' property is set to true.

    Qt Code:
    1. ui->GraphicsView->setInteractive(true);
    To copy to clipboard, switch view to plain text mode 
    Last edited by helloworld; 23rd February 2011 at 07:43.

  5. #5
    Join Date
    Feb 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Graphics View mouse events

    Confirmed now: Both Interactive and Enabled property at QT Creator are checked (I dont change them in my code), and it still doesnt work.

    Ill continue trying to figure out whats happening

    Edit: I didnt set anything about geometry. I just draw a big rect, that "acts" like borders occuping all the widget's area, and then i draw a small one inside. Both are shown in my window.

    Here is a screenshot of my UI:

    http://www.grad.icmc.usp.br/~wlmirand/fotos/ui.png
    Last edited by wlmirand; 23rd February 2011 at 11:28.

  6. #6
    Join Date
    Feb 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Graphics View mouse events

    After a few hours, I just decided make a new project and try implement it alone.... It worked perfectly, So I figured out, that My Layouts construction was somehow blocking the events.

    I Just moved GraphicsView Widget out of the Layout Widget, and it worked. Ill try find why.

Similar Threads

  1. Graphics View : too many lines
    By natnan in forum Qt Programming
    Replies: 6
    Last Post: 28th January 2010, 11:15
  2. OpenGL with Graphics View?
    By Dutch112 in forum Qt Programming
    Replies: 15
    Last Post: 19th May 2009, 08:21
  3. Graphics View or Paint ?
    By igor in forum Qt Programming
    Replies: 4
    Last Post: 21st January 2007, 13:21
  4. Ignore mouse events out of tree view
    By krishna.bv in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2006, 11:24
  5. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13

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.