PDA

View Full Version : QFrame inserting text



Pharell
3rd October 2007, 15:17
Hi

I have a problem with inserting text into a Qframe.
I have a Qframe where I draw some graphics. I want to
insert some text into the graphics depending on their coordination.
I dont know if it is possible to insert text into the qframe.

Does someone have any idea's?

Thanks in advance

marcel
3rd October 2007, 15:21
How do you draw the graphics? In a paintEvent?
Then you can use the QPainter::drawText functions.

Anyway, why don't you use a graphics view?

Pharell
3rd October 2007, 15:31
How do you draw the graphics? In a paintEvent?
Then you can use the QPainter::drawText functions.

Anyway, why don't you use a graphics view?


yes in a paintevent
thanks for the tip

Anyway, why don't you use a graphics view?

Nou the person who begun with this project had already chosen for qframe and we use qt3 but in future we will use qt4 and its going to be easier i hope.

Pharell
4th October 2007, 10:12
he

i am back with my unsloved problem.

look at this code



int x = e->x();
int y = e->y();
text = QString("test: %1").arg(test->ID());
QPainter* p = new QPainter;
p->setBackgroundColor(white);
p->drawText(x, y, text);

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

marcel
4th October 2007, 10:52
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.

Pharell
5th October 2007, 11:01
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:

void testClass::mouseReleaseEvent(QMouseEvent *e )
{

// myCode;

}

void testClass::paintEvent(QPaintEvent *e)
{
// myCode;
Qpainter Paint(this);
paintText(Paint, e);
}


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.


void testClass::paintText (QPainter &painter, QEvent* e)
{

QMouseEvent* me;
me = (QMouseEvent*) e;

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());
text = QString("%1").arg(pTest->ID());

if (pTest)
{
painter.drawText(x, y, text);
}
}
}


What am I doing wrong? Is there another way to do it?
Can someone please help me?

Thanks in advance!

marcel
5th October 2007, 11:08
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, 11:28
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.

hey brother

thanks 1000 times, it works perfectly now :)

Pharell
8th October 2007, 09:07
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!

marcel
8th October 2007, 09:55
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.