If I have some painter, some rectangle and some string:
QPainter* pPainter;
//Initializing it
QRectF RectF;
//Initializing it
std::string strText = "Some string";
And I draw the rectangle with text using painter on some QGraphicsItem object and then we draw it on the scene:
pPainter->drawText(RectF, Qt::AlignCenter, strText);
Note: I cannot modify the code above, only to add something.
My goal is to get that text after a click on the rectangle. Suppose, that I've found mouseclick coordinates correctly.
Is there any way to get rectangle and its text using the coordinates of click? Or maybe I should hold some list of such rectangles to process them next? I've thought about use of itemAt() method, but I couldn't get rectangle that way.
std::string GetTextByCoordinates(int x, int y)
{
//What to do here?
}
Update: Method paint is used in class that inherited from QGraphicsItem (CustomItem), and there is several text rectangles on such items.
Method "GetTextByCoordinates" supposed to be called from that CustomItem object.
Since you draw something on a widget, you can operate only with pixels that are drawn. You have no information about this pixels origin. So you need some additional information.
The normal way to manipulate logical objects drawn on scene is to place
QGraphicsItems on the scene. There areQGraphicsRectItemQGraphicsSimpleTextItemfor your case. It looks like this:If you really cannot modify the code with painter, you must remember all rectangles and texts on them:
See no another ways. Text recognize by bitmap is not considered : )