Results 1 to 7 of 7

Thread: QGraphicsview and mouse press event

  1. #1
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default QGraphicsview and mouse press event

    Hi all,

    I have a problem which need help.

    I have a graphicsview which contains a graphicsscene. I want to do something on a mouse click on the graphicsview object.
    How can I do this?

    mainwindow .h
    Qt Code:
    1. private:
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. item->setRect(50, 100, 5, 5); //for setting position and size
    5. item->setBrush(Qt::red);
    6.  
    7. scene = new QGraphicsScene;
    8. scene->setSceneRect(0, 0, 600, 600);
    9. scene->addItem(item);
    10.  
    11. view = new QGraphicsView(scene);
    12. view->setDragMode(QGraphicsView::ScrollHandDrag);
    13. view->setBackgroundBrush(QPixmap(":/image/map.jpg"));
    14. ...
    15. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsview and mouse press event

    reimplement the QGraphicsScen
    void QGraphicsScene::keyPressEvent ( QKeyEvent * keyEvent ) [virtual protected]
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsview and mouse press event

    Quote Originally Posted by wagmare View Post
    reimplement the QGraphicsScen
    void QGraphicsScene::keyPressEvent ( QKeyEvent * keyEvent ) [virtual protected]
    I guess you meant QGraphicsScene::mousePressEvent
    @Eva
    May be if you tell what you want to do on click, we can help you better

  4. #4
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QGraphicsview and mouse press event

    actually I wanted to have something like when I click the mouse on the graphicsview, I will display a circle on that spot and remove any other previously over items items on the scene (except for the background).

    which mousepressevent should I edit? the graphicsview, graphicsscene or the graphicellipse?

    I have another problem. How can I prevent tiling of the Qpixmap in graphivew if my pixmap is smaller than the scene rect?

  5. #5
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QGraphicsview and mouse press event

    I manage to get the mouse press event to work. However I still have the tiled background issue.

    I loaded map onto the graphicsview by using setbackgroundbrush. but I notice that it got tiled if the graphicsScene scene rect is bigger than the map.

    How can I prevent this? I want to just white/ block out the areas that is not covered by the map.

    Furthermore, I have this scrollbar which will change the size of the map (via matrix transform). I would like the white/black out area to not show after the map is large enough to cover the graphicsScene.

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsview and mouse press event

    Dont set the brush. Draw the background yourself using QGraphicsScene::drawBackground or QGraphicsView::drawBackground

  7. #7
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QGraphicsview and mouse press event

    thx. by the way, is there any way to remove the background brush after involving setBackgroundBrush?

Similar Threads

  1. Mouse hovering or press event not working?
    By qtzcute in forum Newbie
    Replies: 11
    Last Post: 15th July 2009, 10:53
  2. Mouse press event in a QGraphicsScene
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 10:28
  3. Mouse press event detection
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2009, 13:08
  4. Checking for key press on mouse event
    By Cruz in forum Newbie
    Replies: 1
    Last Post: 24th January 2009, 18:18
  5. Draw QtCanvasElipse on mouse press event position
    By YuriyRusinov in forum Newbie
    Replies: 1
    Last Post: 31st May 2006, 11:57

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.