Results 1 to 4 of 4

Thread: QGraphicScene MousePressEvent

  1. #1
    Join Date
    Nov 2007
    Posts
    6

    Post QGraphicScene MousePressEvent

    Hi all,


    I would like to know on wich position a user clicked in the scene.
    I know I have to do this with the mousePressEvent(QGrapicsSceneMouseEvent *event),
    but it just aint working...

    This code pops me a dialog when I click anywhere on my form, except when I click in the scene:
    Qt Code:
    1. void GUIGame::mousePressEvent(QMouseEvent *event){
    2.  
    3. QMessageBox::about(this,"xxxx!","yyyyy!");
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    When I change this event like this:

    Qt Code:
    1. void GUIGame::mousePressEvent(QGraphicsSceneMouseEvent *event){
    2.  
    3. QMessageBox::about(this,"xxxx!","yyyyy!");
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 
    it does'nt do anything.

    The constructor of my mainwindow is like this:

    Qt Code:
    1. GUIGame::GUIGame() : QMainWindow(0, Qt::WindowFlags())
    2. {
    3. setWindowTitle(tr("DuckBoard"));
    4. setWindowIcon(QPixmap("logo.png"));
    5.  
    6. this->setWindowState(Qt::WindowMaximized);
    7. setWindowTitle(tr("DuckBoard"));
    8. setWindowIcon(QPixmap("logo.png"));
    9. gamemenu = menuBar()->addMenu(tr("&Game"));
    10. gamemenu->addAction(QPixmap("icons/wand.png"), tr("&New"), this, SLOT(newGame()), QKeySequence("Ctrl+N"));
    11. gamemenu->addAction(QPixmap("icons/cancel.png"), tr("&Quit"), this, SLOT(close()), QKeySequence("Ctrl+Q"));
    12.  
    13. helpmenu = menuBar()->addMenu(tr("&Help"));
    14. helpmenu->addAction(QPixmap("icons/rosette.png"), tr("&Credits"), this, SLOT(credits()), QKeySequence("Ctrl+C"));
    15. helpmenu->addAction(QPixmap("icons/help.png"), tr("&Help"), this, SLOT(help()), QKeySequence("Ctrl+H"));
    16. canvas = new QGraphicsScene;
    17. canvas->setSceneRect(0,0,640,640);
    18. view = new QGraphicsView(canvas, NULL);
    19. view->setFixedSize(642,642);
    20. view->setAlignment(Qt::AlignLeft);
    21. gamelog = new logGUI(this);
    22. setBord(new Bord());
    23. m_GUIBord = new BordGUI(canvas, geefBord());
    24. setDobbel(new Dobbel());
    25. m_GUIDobbel = new DobbelGUI(this);
    26. QWidget *test = new QWidget;
    27. QHBoxLayout *layout = new QHBoxLayout;
    28. QVBoxLayout *layout2 = new QVBoxLayout;
    29. layout->addWidget(view);
    30. layout2->addWidget(m_GUIDobbel);
    31. layout2->addWidget(gamelog);
    32. layout->addLayout(layout2);
    33. test->setLayout(layout);
    34. setCentralWidget(test);
    35. int j = 1+1;
    36. int i=0;
    37. disableBeforePlay();
    38. gedaan = false;
    39. aandebeurt = 0;
    40.  
    41. connect(m_GUIDobbel->geefWerpKnop(), SIGNAL(clicked()), this, SLOT(zetPion()));
    42. }
    To copy to clipboard, switch view to plain text mode 

    Does anyone knows how I can activate the mousePressEvent for my scene?

    Thanks in advance!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicScene MousePressEvent

    Either subclass QGraphicsScene and override mousePressEvent or install GUIGame as event filter for the scene and catch mouse press events for the scene in there.

    What you did in the first case applies only for the GuiGame widget and the second case makes no sense.

  3. #3
    Join Date
    Nov 2007
    Posts
    6

    Default Re: QGraphicScene MousePressEvent

    thanks for your answer,
    It worked with subclassing!
    Last edited by Spitz; 16th November 2007 at 19:37.

  4. #4
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicScene MousePressEvent

    Hard to tell what you're doing, but for a game, you probably want to create your own QGraphicsItems and detect the mouse press event on those, instead of detecting them on the scene and trying to figure out which item was pressed...

Similar Threads

  1. Replies: 8
    Last Post: 16th April 2010, 09:41
  2. Replies: 2
    Last Post: 1st November 2007, 08:25
  3. MousePressEvent for QTextEdit
    By anju123 in forum Qt Programming
    Replies: 9
    Last Post: 16th August 2007, 06:08
  4. Custom objects in a QGraphicScene
    By draand in forum Qt Programming
    Replies: 2
    Last Post: 24th July 2007, 11:31
  5. Replies: 6
    Last Post: 30th April 2007, 23:59

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.