Results 1 to 8 of 8

Thread: QMouseEvent on QPaintEvent

  1. #1
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QMouseEvent on QPaintEvent

    I want to check for a mouseClickEvent within the paintevent. If it is within a specific coordinates then I would like it to paint another object. Is there a way to set the coordinates to a variable?

    Is this a bad way to deal with a paintevent? Is it better to use a set of Qbuttons?

    Thank You.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QMouseEvent on QPaintEvent

    You want to paint an object where ? On a widget ?
    Look at QGraphicsScene, maybe it is more suitable for your needs.
    If you want to store the coordinates of last clicked position on a widget, reimplement QWidget::mousePressEvent(QMouseEvent * event); and store value of event->pos().
    Maybe provide more detailed explanation of what you are trying to achieve.

  3. #3
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QMouseEvent on QPaintEvent

    My intent is to create a tic tac toe game. I have already programmed the main parts of the game and just am looking to ad the functionality of mouseclicks. Right now I have the user select squares through the menubar. So basically I want to link those actions that are now attached to the menubar to mouseevents within given coordinants in the main window. With the program as it now stands, I am using a paintevent to redraw the board and the taken squares.

    If you want to see my source I can add that at request.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QMouseEvent on QPaintEvent

    Ok, so you probably you have some methods like
    Qt Code:
    1. void selectSquare1();
    2. void selectSquare2();
    3. //...
    To copy to clipboard, switch view to plain text mode 
    Then just use mousePressEvent method to get clicked position and determine which square was clicked:
    Qt Code:
    1. void MyWidgetClass::mousePressEvent( QMouseEvent * event ){
    2. const QPoint pos = event->pos();
    3. qDebug() << "clicked pos : " << pos;
    4. // calculate index of square using pos coordinates
    5. int index = getSquareIndex(pos);
    6. this->selectSquare( index );
    7. QMainWindow::mousePressEvent(event);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Too bad your game is almost finished, because using the Qt graphics view framework is more suitable for this kind of 2d graphics programming ( for example, if you want to add possibility of interaction with rendered items ).

  5. The following user says thank you to stampede for this useful post:

    GUIman (28th February 2011)

  6. #5
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QMouseEvent on QPaintEvent

    Quote Originally Posted by stampede View Post
    Ok, so you probably you have some methods like
    Qt Code:
    1. void selectSquare1();
    2. void selectSquare2();
    3. //...
    To copy to clipboard, switch view to plain text mode 
    That is correct.

    Quote Originally Posted by stampede View Post
    Then just use mousePressEvent method to get clicked position and determine which square was clicked:
    Qt Code:
    1. void MyWidgetClass::mousePressEvent( QMouseEvent * event ){
    2. const QPoint pos = event->pos();
    3. qDebug() << "clicked pos : " << pos;
    4. // calculate index of square using pos coordinates
    5. int index = getSquareIndex(pos);
    6. this->selectSquare( index );
    7. QMainWindow::mousePressEvent(event);
    8. }
    To copy to clipboard, switch view to plain text mode 
    My only confusion is how do I actually compare the pos to given coordinates.
    Say the square1 is at the coordinates (20,20), (20,100), (100, 20), (100, 100) within the mainwindow. How do I see if the mousepressevent is within square1?

    Thank you

  7. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QMouseEvent on QPaintEvent

    How do I see if the mousepressevent is within square1?
    event->pos() is a QPoint, it has x and y coordinate.
    Checking if (x,y) is in [a,b]x[c,d] is very simple math.
    But if you dont like math (how dare you!), you can always use QRect.

  8. #7
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QMouseEvent on QPaintEvent

    Quote Originally Posted by stampede View Post
    event->pos() is a QPoint, it has x and y coordinate.
    Checking if (x,y) is in [a,b]x[c,d] is very simple math.
    But if you dont like math (how dare you!), you can always use QRect.
    I am fine with the math. I am just unclear as to the syntax.

    how do I set the x coordinate to posx and the y coordinate to posx?

    I realize that this code as it is written may not work.

    Qt Code:
    1. if ((posx < 200) && (posy <200))
    2. {
    3. spot11Act();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Is there any tutorial on how to read the Qt reference documentation. This is basically the first time that I have dealt with actually going through the reference documentation to learn how to use the various functions of the Qt framework. When I look at the reference, all I see is a list of functions and classes.

    Thank You Very Much.

  9. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QMouseEvent on QPaintEvent

    This is basically the first time that I have dealt with actually going through the reference documentation to learn how to use the various functions of the Qt framework
    Then you are very lucky, because Qt has the best refrence manual I've ever seen.
    When I look at the reference, all I see is a list of functions and classes.
    You forgot to add: "... with detailed description of every method, class and module and lots of code samples."
    Is there any tutorial on how to read the Qt reference documentation
    I don't know how to help you with this, just read it and you'll get used to it.
    how do I set the x coordinate to posx and the y coordinate to posx?
    posx, posy are of type int ? Well, QPoint is a class, it has x() and y() methods, returning a value of type int...

Similar Threads

  1. How to sendEvent QPaintEvent ?
    By SABROG in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2009, 21:55
  2. QPaintEvent syntax question
    By last2kn0 in forum Newbie
    Replies: 5
    Last Post: 25th January 2008, 20:36
  3. QpaintEvent region problem with QPopupmenus
    By Qtkiller in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2007, 09:18
  4. QPaintEvent on button click?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2007, 08:44
  5. Inheritance and QpaintEvent
    By djoul in forum Qt Programming
    Replies: 22
    Last Post: 5th July 2006, 13:56

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.