he
i am back with my unsloved problem.
look at this code
Qt Code:
int x = e->x(); int y = e->y(); p->setBackgroundColor(white); p->drawText(x, y, text);To copy to clipboard, switch view to plain text mode
i do not need to draw the text in my paintEvent, i am doing this in the mousereleaseEvent. when i debug i get the correct information but it is not drawn in the frame. What am i doing wrong?
thanks in advance
In Qt4 all widget painting must be done inside a paint event.
In your code you don't initialize the painter wit ha paint device( e.g. this ). If you did initialized it with 'this', then it would have crashed.
In mouse press/release, set the data that has to be painted somewhere in your class and call update(). In paint event check that data and paint it.
he guys,
Please can someone take a look at my problem?
I am so stucked in this paint thing(i am new to qt).
I have a situation which is related to mouseReleaseEvent and paintEvent. On right clicking on a graphic line on the screen, i have to draw then some text on that point.
So i have here my functions:
Qt Code:
{ // myCode; } { // myCode; Qpainter Paint(this); paintText(Paint, e); }To copy to clipboard, switch view to plain text mode
In the below function i try to right click on the chosen point in the screen and then get the text with ID() and draw it on that point. It compiles well but the there is going to be no draw on the screen, i dont see the text.
Qt Code:
{ QMouseEvent* me; if (me->button() == Qt::RightButton) { int x = me->x(); int y = me->y(); QString text; FPoint P = DevToWin(x,y); Test *pTest = mp->Find(mi, P, mp->Sometthing->GetFilter()); if (pTest) { painter.drawText(x, y, text); } } }To copy to clipboard, switch view to plain text mode
What am I doing wrong? Is there another way to do it?
Can someone please help me?
Thanks in advance!
This is not the way to do it...
You can't be sure if a paintEvent coincides with a mouse click.
Do what I asked you the first time.
In a mouse press/release store the mouse position and the text you need to draw in a structure or a few variables. At the end of your mouse event, call an update(). This will post a paint event in the event queue and will be executed next time the event loop is gaining control.
In paint event, draw the text at the mouse position specified in the vars/structure you set in the mouse event.
Pharell (5th October 2007)
Hey guys,
@Marcel
I am back with another question. Now that i can draw the text on the given position, i see that when i scroll in the view, the text moves too. I just want to draw the text on the given position. Another problem is that when i choose to draw in another position that the first drawn text vanish from the screen. I can draw one text at a time. Can someone plz tell what am I doing wrong?
thanks in advance!
For the scrolling issue. Do you add the frame in a scroll area? The this is normal.
To get the expected behavior then you either change your approach( QFrame, scroll area etc) and use a graphics view, or create a transparent overlay widget with the same size as the scroll area's viewport and draw the text on that widget.
If you'd use a graphics view then you could draw the text in drawForeground and it will ignore any transformations/scrolling.
As for the second issue, you need to store the strings in a list. Each time you need to draw a new string append it to the list and in paint event draw all the strings in the list. You will also need a list of coordinates, for each text.
Bookmarks