Results 1 to 15 of 15

Thread: Can't trigger mousePressEvent on QGraphicsPixmapItem

  1. #1
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Can't trigger mousePressEvent on QGraphicsPixmapItem

    To learn Qt, I'm creating something that resembles a chess board. I'm not going to implement the game logic; I just want to toy with mouse click events and drag events.

    I want to be able to drag chess pieces (without drop for now), but the chess piece's mousePressEvent and mouseMoveEvent don't get triggered when I expect them to. I double checked it by putting breakpoints at the start of both event handling functions.

    The GUI



    Each chess piece is represented by an instance of this class.

    Qt Code:
    1. class ChessPiece : public QGraphicsPixmapItem
    To copy to clipboard, switch view to plain text mode 

    Each board tile is represented by an instance of this class.

    Qt Code:
    1. class BoardTile : public QGraphicsItem
    To copy to clipboard, switch view to plain text mode 

    ===============================

    chesspiece.h (snippet)

    Qt Code:
    1. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    2. void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    To copy to clipboard, switch view to plain text mode 

    If there are any important code snippets that you need to see to help me out, either ask me to post them or download the full source here. I work with QtCreator.

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    Hi,

    Did you override the scene mousepress/move/release event? If you did I think you need to call for example QGraphicsScene::mousePressEvent(mouseEvent) in the mousePress to propagate the event to the items.

  3. #3
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    Quote Originally Posted by qlands View Post
    Did you override the scene mousepress/move/release event? If you did I think you need to call for example QGraphicsScene::mousePressEvent(mouseEvent) in the mousePress to propagate the event to the items.
    I did override the default event. Are you saying that I need to do this?

    Qt Code:
    1. void ChessPiece::mousePressEvent(QGraphicsSceneMouseEvent *event) {
    2. QGraphicsScene::mousePressEvent(mouseEvent);
    3. // ...
    To copy to clipboard, switch view to plain text mode 

    But if that function will never get called, the call to QGraphicsScene::mousePressEvent(mouseEvent) will never be made, right?

  4. #4
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    Not ChessPiece but the scene.

    Inside your void myScene::mousePressEvent(QGraphicsSceneMouseEvent *event) you need to call QGraphicsScene::mousePressEvent(mouseEvent);

  5. #5
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    Oh, you meant if I overrode myScene::mousePressEvent(QGraphicsSceneMouseEvent *event). No, I didn't override the default mousePressEvent handler for the graphics scene. I do have some classes (BoardTile and ChessPiece) that inherit from QGraphicsItem that do have mousePressEvent overrides.

    Either way, if I define a custom myScene::mousePressEvent(QGraphicsSceneMouseEvent *event) with a call to QGraphicsScene::mousePressEvent(mouseEvent) in it, I can see that clicks are being registered on the level of the graphics scene, but the objects are still not getting the click.

  6. #6
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    Hi,

    It work when you set the flag ItemIsSelectable to true:

    boardTiles[i][j]->setFlag(QGraphicsItem::ItemIsSelectable,true);

  7. #7
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    I set the flag on both the tiles and the pieces. But when I click on a white piece, the mouseReleaseEvent of the underlaying tile is triggered instead of the mousePressEvent of the white piece. Isn't Qt supposed to trigger the mouse event for the topmost object on the graphics scene when it is being clicked? In case you're wondering why I implemented a mouseReleaseEvent instead of a mousePressEvent on the tiles, it's because the press event behaved oddly and the release event worked fine.

  8. #8
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    The click on the piece does not work because the QGraphicsPixmapItem does not have any pixmap. This because you are wrongly overriding the paint event to set the image... No need for this!!!

    Remove the paint event form the piece and add:
    QPixmap logo(":/img/pawn.png","PNG");
    newCP->setPixmap(logo);

    When you click on a piece you will find a box around it, you can remove it by properly overriding the paint()

    Qt Code:
    1. void ChessPiece::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
    2. {
    3. if (isSelected())
    4. {
    5. QStyleOptionGraphicsItem *newOption = const_cast<QStyleOptionGraphicsItem*>(option);
    6. //Remove the style
    7. newOption->state &= ~QStyle::State_Selected;
    8. QGraphicsPixmapItem::paint(painter, newOption, widget);
    9. }
    10. else
    11. QGraphicsPixmapItem::paint(painter,option,widget);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Basically you are braking the events of items and this cause things to behave weird. I suggest you to take an look at the documentation and examples on how to use QGraphicsPixmapItem

  9. #9
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    Cool... I can now initiate a drag on the chess pieces. I've figured out that I can set the position of the QGraphicsPixmapItem by doing setPos(x, y) but I also want to set the width and height. As far as I can tell from reading the doc, I can only do setScale(a number between 0 and 1).

    About the incorrect use of the paint event... What about my paint override for the board tiles? Here's what I did there:

    Qt Code:
    1. void BoardTile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
    2. if (tileColor == 0) {
    3. painter->setBrush(QBrush(QColor(251, 201, 159)));
    4. painter->setPen(QPen(QColor(251, 201, 159)));
    5. } else {
    6. painter->setBrush(QBrush(QColor(207, 139, 70)));
    7. painter->setPen(QPen(QColor(207, 139, 70)));
    8. }
    9. painter->drawRect(x, y, width, height);
    10. }
    To copy to clipboard, switch view to plain text mode 

    I was trying to apply this example from the doc for QGraphicsItem to my objects:

    Qt Code:
    1. class SimpleItem : public QGraphicsItem
    2. {
    3. public:
    4. QRectF boundingRect() const
    5. {
    6. qreal penWidth = 1;
    7. return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
    8. 20 + penWidth, 20 + penWidth);
    9. }
    10.  
    11. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    12. QWidget *widget)
    13. {
    14. painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
    15. }
    16. };
    To copy to clipboard, switch view to plain text mode 

    Why is that a good override for the paint event and why was mine bad?

  10. #10
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    With void BoardTile:: paint().. You are overriding the paint event and modifying the pointer to the painter (line 9) but not calling the ancestor. You are sub-classing BoardTile from QGraphicsItem thus you usually need to call the ancestor paint event otherwise you don't know what your are braking. In my experience overriding a paint event and not calling the ancestor usually ended up in unpredictable behavior.

    Look at this post where they discuss a little of overriding events: http://www.qtcentre.org/threads/2401...nts-overriding

    Width and height is defined by the size of your QPixmap. You can use a QPixmap.scaled() to return a scaled version of the pixmap and use it in the QGraphicsPixmapItem.
    Last edited by qlands; 12th November 2010 at 06:04.

  11. #11
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    I updated the paint overrides. The dragging works on the top left piece, but other pieces still won't budge.

    Screen capture: http://www.screentoaster.com/watch/s...1RVFVX/qtchess
    Updated code: http://www.pieterdedecker.be/tmp/QtChess-20101112.zip

    Any thoughts on why the code doesn't work in all cases?

  12. #12
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    Hi,

    You are implementing the movement in the piece itself and not in the scene... Both of them are different as they have different coordinate systems for example.

    I mode some changes in your code to allow the movement of the pieces and to control where the piece if moved to.

    You can get the code here:

    http://www.qlands.com/other_files/Qt...0101112.tar.gz

    I made changes in piece,tile and the boargui.

    Basically I:
    1. removed the events from piece and tile.
    2. Added an type to piece and tile so I can cast them
    3. Added mousepress and mouserelease to the boardgui
    4. In those event I coded some bit so the piece can be moved and control when is taken and dropped
    5. Each piece need a flag movable to true.

    See that I dont use the event of drag and drop. If you want to use those events to set each piece to allow drops but is not really neccesary. You can also use the scene mousemove to check the current tile under the mouse while holding the piece and change its color to indicate that is allowed in that time.

    Hope this helps!

  13. #13
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    I appreciate your effort, but the dragging is glitchy on my computer. Have a look at this: http://www.screentoaster.com/watch/s...UVVU/dragndrop

    Do I have to repaint the board at the end of mousePressEvent and mouseReleaseEvent?

  14. #14
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    Yes, I saw that. What is happening is that the scene is not refreshing properly as the piece pass under the tile items. It could be a bug. I recommend you to post that as a Thread is this forums.

    Carlos.

  15. The following user says thank you to qlands for this useful post:

    blooglet (17th November 2010)

  16. #15
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't trigger mousePressEvent on QGraphicsPixmapItem

    Alright, thanks!

Similar Threads

  1. Replies: 2
    Last Post: 30th July 2010, 16:44
  2. Replies: 5
    Last Post: 23rd May 2010, 01:09
  3. how to captue sqlite's trigger generated err in QT?
    By BalaQT in forum Qt Programming
    Replies: 10
    Last Post: 14th November 2009, 13:42
  4. Trigger QAction by 'Key_Escape'
    By Raccoon29 in forum Newbie
    Replies: 5
    Last Post: 3rd April 2008, 10:24
  5. QComboBox as a trigger
    By ape in forum Newbie
    Replies: 8
    Last Post: 4th February 2008, 08: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
  •  
Qt is a trademark of The Qt Company.