PDA

View Full Version : problem translating & rotating text



impeteperry
3rd July 2006, 05:51
Hi, i'm using Qt4.1.4
I have three lines forming a triange.
I want to label these lines s1, s2, s3 at the midpoint of each line, rotated to the angle of the line.
void edpForm::paintEvent(QPaintEvent *event)
{
double angle;
QPainter painter( this );

for( n = 0; n < paintLines.count(); ++n )
{
k = paintLines[n];
temp = k.split( "," );
xs = temp[0].toInt( &ok );
ys = temp[1].toInt( &ok );
xe = temp[2].toInt( &ok );
ye = temp[3].toInt( &ok );
painter.drawLine( xs, ys, xe, ye );
}
for( n = 0; n < paintLabels.count(); ++n )
{
k = paintLabels[n];
temp = k.split( "," );
xs = temp[0].toInt( &ok );
ys = temp[1].toInt( &ok );
angle = temp[2].toDouble( &ok );
painter.translate( xs, ys );
painter.rotate( angle );
painter.drawText( 0, 0, temp[3] );
//painter.drawText( xs, ys, temp[3] );
}
}With the code as shown, I get the label s1, rotated to the angle of the line and at the midpoint of the line. The other two lines are not labeled.

If I option out the 3 lines above the optioned out line and activate the bottom line, I get each line labled at their midpoints, but the labels are not rotated.

This tells me, that the data is OK., so there must be somthing wrong with my code. Help would be appreciated.

I am having a great deal of trouble with the switch from Qt3 to Qt4 dealing with graphics, so I am being very verbose

thanks

munna
3rd July 2006, 05:59
Try this



void edpForm::paintEvent(QPaintEvent *event)
{
double angle;
QPainter painter( this );

for( n = 0; n < paintLines.count(); ++n ) {
k = paintLines[n];
temp = k.split( "," );
xs = temp[0].toInt( &ok );
ys = temp[1].toInt( &ok );
xe = temp[2].toInt( &ok );
ye = temp[3].toInt( &ok );
painter.drawLine( xs, ys, xe, ye );
}

for( n = 0; n < paintLabels.count(); ++n ) {
k = paintLabels[n];
temp = k.split( "," );
xs = temp[0].toInt( &ok );
ys = temp[1].toInt( &ok );
angle = temp[2].toDouble( &ok );

painter.save();

painter.translate( xs, ys );
painter.rotate( angle );
painter.drawText( 0, 0, temp[3] );
//painter.drawText( xs, ys, temp[3] );

painter.restore();
}
}

impeteperry
3rd July 2006, 15:49
Thanks, but adding "painter.repaint" did not make any diference.
I will try to send a screen shotshot, but I don't knowif this is the way to do it.
/home/pete/Programs/pmProgram/snapshot6.png
anyway, below is the code for the"paintEvent" trigger.
void edpForm::slotPaintItems( QStringList list )
{
paintLines = list[0].split( "|" );
paintLabels = list[1].split( "|" );
edpForm::update();
}I tried "repaint" in place of "update" no diference.

I may have 15 or 20 items that I want painted and the combination of items will very depending on where the trigger is called. They will be in order with a "null" string for those not to be printed. Say I want to paint the lines without the labels. Is the procedure I have used here appropiate?

Thanks.

I

munna
3rd July 2006, 15:59
but adding "painter.repaint" did not make any diference.

It is painter.restore and not painter.repaint


I will try to send a screen shotshot, but I don't knowif this is the way to do it.
/home/pete/Programs/pmProgram/snapshot6.png

You can upload your png at http://imageshack.us and then give the link here like:

link


Say I want to paint the lines without the labels. Is the procedure I have used here appropiate?

I think its fine. But you will need to call repaint() in order to see changes immediately.

jacek
3rd July 2006, 16:55
You can upload your png at http://imageshack.us and then give the link here like:
Or simply click "Manage Attachments" button below the editor.

impeteperry
3rd July 2006, 17:37
Thanks, I do have painter.restore in my code. I miss typed it in my post sorry. When I ran the program
for( n = 0; n < paintLabels.count(); ++n )
{
k = paintLabels[n];
temp = k.split( "," );
xs = temp[0].toInt( &ok );
ys = temp[1].toInt( &ok );
angle = temp[2].toDouble( &ok );
painter.translate( xs, ys );
painter.rotate( angle );
painter.drawText( 0, 0, temp[3] );
//painter.drawText( xs, ys, temp[3] );

painter.restore();
}
I got the same image plus I got "Painter::restore(), unbalanced save/restore" warnings for each element painted. Now for the inmage with and without the "painter.restore()".
http://img104.imageshack.us/img104/9200/snapshot60dt.th.png (http://img104.imageshack.us/my.php?image=snapshot60dt.png) and with the code
for( n = 0; n < paintLabels.count(); ++n )
{
k = paintLabels[n];
temp = k.split( "," );
xs = temp[0].toInt( &ok );
ys = temp[1].toInt( &ok );
angle = temp[2].toDouble( &ok );
//painter.translate( xs, ys );
//painter.rotate( angle );
//painter.drawText( 0, 0, temp[3] );
painter.drawText( xs, ys, temp[3] );

//painter.restore();
}
I get the image
http://img214.imageshack.us/img214/9692/snapshot83lt.th.png (http://img214.imageshack.us/my.php?image=snapshot83lt.png) with the "painter.restore()" or with it optioned out. (name warning with it in ) I hope all this helps you.

Thanks for the tip on images, I am not sure on its use, but if after you click on the thumbnail, you click on the image again, you get my drawing.

munna
3rd July 2006, 18:30
I got the same image plus I got "Painter::restore(), unbalanced save/restore" warnings for each element painted.

The warning is because for every save/restore there should be a corresponding restore/save somewhere in the code, which is what missing.

Your code should have been



for( n = 0; n < paintLabels.count(); ++n ) {
k = paintLabels[n];
temp = k.split( "," );
xs = temp[0].toInt( &ok );
ys = temp[1].toInt( &ok );
angle = temp[2].toDouble( &ok );

painter.save(); //this line is missing

painter.translate( xs, ys );
painter.rotate( angle );
painter.drawText( 0, 0, temp[3] );
//painter.drawText( xs, ys, temp[3] );

painter.restore();
}



If you do not save and restore the painter settings for every label you want to draw the translate and rotate are carried on and is applied for the consecutive labels also, which is where you might be having problems.

impeteperry
3rd July 2006, 19:06
It works.
http://img154.imageshack.us/img154/479/snapshot95ec.th.png (http://img154.imageshack.us/my.php?image=snapshot95ec.png)
Pleaase tell me where, I could find this information that demonstrates and explains this Item in simple English. I get the feeling Trolltech has hired some Microsoft programmers.

Onwards, ever onwards.

i was getting ready to abandon Qt.

Thanks.

munna
3rd July 2006, 19:20
If you look at all this logically it is all really very simple. Everything you want to know is there in the Qt Assistant. I think it is one of the best docuement ever written.

You can download this Qt3 (http://www.phptr.com/bookstore/product.asp?isbn=0131240722&rl=1) book and the all major concepts of drawing could still be applied for Qt4. Also, if you want you can buy this Qt4 (http://www.phptr.com/bookstore/product.asp?isbn=0131872494&rl=1) book.

You can also look into Qt Quarterly (http://doc.trolltech.com/qq/index.html) for reference.

By the way, I think Trolls are better than M$ programmers.;)

impeteperry
3rd July 2006, 20:17
If you look at all this logically it is all really very simple. Everything you want to know is there in the Qt Assistant. I think it is one of the best docuement ever written.

You can download this Qt3 (http://www.phptr.com/bookstore/product.asp?isbn=0131240722&rl=1) book and the all major concepts of drawing could still be applied for Qt4. Also, if you want you can buy this Qt4 (http://www.phptr.com/bookstore/product.asp?isbn=0131872494&rl=1) book.

You can also look into Qt Quarterly (http://doc.trolltech.com/qq/index.html) for reference.

By the way, I think Trolls are better than M$ programmers.;)

I'm 80, a structural engineer and inventor. Been programming of one type or another since 1970. The switch in drawing from Qt3 to Qt4 has been the most daunting in my career. It must be the age.
The concept of Signal & Slots is truly a great insovation.

I had no problems in Qt3.

From the screenshats you can tell I march to a differnt drummer. I am looking at the program as a user. The program is a substitute for CAD.
http://img148.imageshack.us/img148/4477/snapshot106bx.th.png (http://img148.imageshack.us/my.php?image=snapshot106bx.png)

I will look at QAssistant furthur. Thanks again. pete