PDA

View Full Version : right click QPainter rectangle?



mattyev87
25th January 2011, 15:48
hey guys, im using PyQt4 and i have created a grid of rectangles using the QPainter:

painter = QtGui.QPainter(self)
painter.drawRect(Ypos, Xpos, square_width, square_height)

its nothing fancy, it just does the job for now.

my question is simply is it possible to store a different string (im going to use it to store names for now) in each painted square, right click and print the string for example?

im very new to PyQt4 and any examples would be much appreciated. thanks

matty

high_flyer
25th January 2011, 20:02
The way you are doing it now (painting a rect) the short answer is 'no', because the rect is not an object, just some coordinates you are giving to the painter.
But instead of drawing the rects your self, you could just place QFrames (or QLabels) on your widget, then you can either use the obejectName() method, or set properties, or subclass and add a special field, a setter and getter for it, and as added bonus you don't have to worry about painting at all!

mattyev87
26th January 2011, 07:34
thanks alot, thats fantastic. will research into it now :)

mattyev87
27th January 2011, 08:49
hey, using the frame method worked very well. i subclassed Qframe and added my own setter and getter methods. which work fine

but still having trouble figuring out how to use the getter method when a qframe is clicked.

any ideas how i might do this?

high_flyer
27th January 2011, 09:13
Have a look at QWidget::mousePressEvent()

mattyev87
27th January 2011, 12:59
hey thanks worked like a charm.

is there a way to do a mouse hover event on a qframe?

high_flyer
27th January 2011, 13:22
Check the QEvent type enums.