PDA

View Full Version : QMouseEvent on QPaintEvent



GUIman
27th February 2011, 20:10
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.

stampede
27th February 2011, 22:39
You want to paint an object where ? On a widget ?
Look at QGraphicsScene (http://doc.qt.nokia.com/latest/qgraphicsscene.html), 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.

GUIman
28th February 2011, 04:34
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.

stampede
28th February 2011, 09:05
Ok, so you probably you have some methods like


void selectSquare1();
void selectSquare2();
//...

Then just use mousePressEvent method to get clicked position and determine which square was clicked:


void MyWidgetClass::mousePressEvent( QMouseEvent * event ){
const QPoint pos = event->pos();
qDebug() << "clicked pos : " << pos;
// calculate index of square using pos coordinates
int index = getSquareIndex(pos);
this->selectSquare( index );
QMainWindow::mousePressEvent(event);
}

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 ).

GUIman
28th February 2011, 20:57
Ok, so you probably you have some methods like


void selectSquare1();
void selectSquare2();
//...

That is correct.



Then just use mousePressEvent method to get clicked position and determine which square was clicked:


void MyWidgetClass::mousePressEvent( QMouseEvent * event ){
const QPoint pos = event->pos();
qDebug() << "clicked pos : " << pos;
// calculate index of square using pos coordinates
int index = getSquareIndex(pos);
this->selectSquare( index );
QMainWindow::mousePressEvent(event);
}



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

stampede
28th February 2011, 23:13
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 (http://doc.qt.nokia.com/4.7/qrect.html).

GUIman
1st March 2011, 02:15
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 (http://doc.qt.nokia.com/4.7/qrect.html).

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.


if ((posx < 200) && (posy <200))
{
spot11Act();
}

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.

stampede
1st March 2011, 09:51
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...