Results 1 to 15 of 15

Thread: Question on QPainter setWindow use

  1. #1
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Question on QPainter setWindow use

    I'm running Qt4. I want to use QPainter to display an engineer drawing where dimensions go from the lower left to upper right of the screen. Can I, and if so how do I, set the "windowRect" to accomplish this with "painter.setWindow( windowRect )" function.

    In other words, say I have a line(0,0, 10000, 70000) which I want to display from the lower left to the upper right of the screen.

    In Qt3, I adjusted my data to accomodate the screen, now I want the screen to accomodate my data.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question on QPainter setWindow use

    I am not sure but I think scale(-1,-1) should do the job. Try it out.

  3. The following user says thank you to munna for this useful post:

    impeteperry (27th July 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question on QPainter setWindow use

    I will try it out, thanks

    Shortly after i posted the tread, I tried
    Qt Code:
    1. x1 = (int)(-0.04 * factor * pX);
    2. y1 = (int)(factor * pY);
    3. x2 = (int)(factor * pX);
    4. y2 = (int)(factor * pY * -1);
    5. myWindowRect.setRect(x1, y1, x2, y2);
    To copy to clipboard, switch view to plain text mode 
    and it seemed to work and it seemed logical. but I will have to spend time testing it.

    Thanks

  5. #4
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question on QPainter setWindow use

    Hi, Except for the pen functions, the lines display correctly, but the text won't display
    Qt Code:
    1. void edpForm::paintEvent(QPaintEvent * /*event*/)
    2. {
    3. double angle;
    4. QPainter p( this );
    5.  
    6. if(paintLines.count () > 0)
    7. {
    8. QPen pen;
    9. pen.setBrush(Qt::blue);
    10.  
    11. //pen.setWidth(1);
    12.  
    13. //QVector<qreal> dashes;
    14. //qreal space = 4;
    15. //dashes << 10 << space << 10 << space << 10 << space << 10;// << space << 10;
    16. //dashes << 1 << space << 3 << space << 9 << space << 27; //<< space << 9;
    17.  
    18. //pen.setDashPattern(dashes);
    19.  
    20. p.setPen(pen);
    21.  
    22. p.setViewport(viewportRect);
    23. p.setWindow(windowRect);
    24. p.setClipRect(clippingRect);
    25.  
    26. for( n = 0; n < paintLines.count(); ++n )
    27. {
    28. k = paintLines[n];
    29. temp = k.split( "," );
    30. xs = temp[0].toInt( &ok );
    31. ys = temp[1].toInt( &ok );
    32. xe = temp[2].toInt( &ok );
    33. ye = temp[3].toInt( &ok );
    34. p.drawLine( xs, ys, xe, ye );
    35. /// start of labeling.
    36. xc = (xs + xe) / 2;
    37. yc = (ys + ye) / 2;
    38. if( xs == xe ) angle = pi/2;
    39. else if( ys == ye) angle = 0;
    40. else angle = atan((double)(ye-ys)/(xe-xs));
    41. angle = (angle * 180)/pi;
    42. p.save();
    43. p.translate( xs, ys );
    44. p.rotate( angle );
    45. k = "s" + h.setNum(n + 1);
    46. p.drawText(0, 0, k);
    47. p.restore();
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 
    What am I missing?

    thanks

  6. #5
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question on QPainter setWindow use

    In addition the following does not work
    Qt Code:
    1. if(paintErase == true) { p.eraseRect(viewportRect); return; }
    To copy to clipboard, switch view to plain text mode 
    I have also tried it with the "windowRect" and "clippingRect". Same thing, NOTHING!!. The paintEvent is triggered OK however

    I can draw and clip lines, but that is all that works.

    Is there a tutorial or example for displaying a "Drawing" where "0,0" is the lower left corner of the screen?

    p.drawText(25,30,"This displays"). works before any of the "transforms" are implemented.

    thanks

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question on QPainter setWindow use

    Quote Originally Posted by munna
    I am not sure but I think scale(-1,-1) should do the job. Try it out.
    I'd say (1,-1) as you don't want to negate the horizontal axis. But this won't be enough. You will have all texts upside down.

  8. The following user says thank you to wysota for this useful post:

    impeteperry (31st July 2006)

  9. #7
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question on QPainter setWindow use

    You gave me that suggestion before, but I wasn't sure on how to use it and as a result got noware with it. If you could give me a clue on how to use it I would appreciate it.

    I can't have distorted text. The lines are all in the first or fourth quadrant and the text has to be "above" the line at mid length and oriented to the slope of the line.

    In Qt3, I did all the transforms, rotations etc. on the data before I painted.It. I was under the impression that Qt4 did that with the new painter functions. Either I am doing somthing wrong or Qt4 can't handle it.

    When I got the lines to display properly, (except for pen functions) I thought I was home free.

    Thanks.

  10. #8
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question on QPainter setWindow use

    I was able to use the (1,-1), but I can't have upside down text, however I will spend some time with it. anyway.

    Thanks.

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question on QPainter setWindow use

    Quote Originally Posted by impeteperry
    In Qt3, I did all the transforms, rotations etc. on the data before I painted.It. I was under the impression that Qt4 did that with the new painter functions.
    To be honest, Qt3 had the same abilities in this aspect as Qt4. Only the way pens are handled was changed in Qt4.

    Either I am doing somthing wrong or Qt4 can't handle it.

    When I got the lines to display properly, (except for pen functions) I thought I was home free.

    Thanks.
    I'd say you should translate the coordinates by the height of the window. This way you'll get (0,0) in the bottom left corner of your screen with the y axis pointing down. Now if you use negative vertical coordinates, you'll get what you want (so draw at (10, -200) instead of (10, 200)). You could also scale the painter by -1 in vertical direction then, but your texts would still be upside down.

  12. #10
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question on QPainter setWindow use

    Thanks.
    The program I am trying to write in Qt is a re-write of a DOS program where I transformed my data to fit the screen perameters, so I continued with that mind set and it worked.

    With Qt4, there were enough changes in QPainter to make me dig deeper into painter. Without any simple CAD type tutorials, demos or examples where "0,0" is at the lower left, I thought the "setWindow" function would do in painter what I had done in my code.

    You suggested translating the "y" coordinates which I think I had in the following code
    Qt Code:
    1. pRatio = ((double)pX/pY); /// aspect ratio of drawing frame pixels
    2.  
    3. x1 = (int)(-0.04 * maxX); /// arbitrary 4% left border
    4. y1 = (int)(1.05 * maxY); /// arbitrary 5% top border
    5.  
    6. xWidth = 1.08 * maxX; /// arbitrary 4% left and right borders
    7. yHeight = 1.1 * maxY; /// arbitrary 5% top and bottom borders
    8. dRatio = (xWidth/yHeight); /// aspect ratio of drawing dimentions
    9. if( pRatio < dRatio ) /// modify the width or height to suit
    10. yHeight *= pRatio;
    11. else
    12. xWidth *= pRatio;
    13.  
    14. myViewportRect.setRect(0, 0, pX, pY);
    15. myWindowRect.setRect(x1, y1, (int)xWidth, -(int)yHeight);
    16. myClippingRect.setRect(0, (int)maxY, (int)(maxX+1000.0), -(int)(maxY+1000.0));
    To copy to clipboard, switch view to plain text mode 
    which draws the lines properly, including "clipping".
    Now why don't the "pen" (except the "setBrush") and "text" functions work?
    I can "drawText" on the screen ok before the "setWindow", but not after.

    Note: All the data are integers. (1000* inches or 1000*millemeters) to avoid floating point comparison errors.

  13. #11
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question on QPainter setWindow use

    I'm back.
    I have solved most of my problems. They were two fold. I needed another p.scale(1, -1) to right my font and had to make my font size a function of my "windowRect".

    Right now, I can't find a clue on how to rotate retangles
    Qt Code:
    1. if(paintColumns.count() > 0)
    2. {
    3. p.setViewport(viewportRect);
    4. p.setWindow(windowRect);
    5. p.setClipRect(windowRect);
    6. for( n = 0; n < paintColumns.count(); ++n )
    7. {
    8. k = paintColumns[n];
    9. temp = k.split( "," );
    10. xs = temp[0].toInt(&ok);
    11. ys = temp[1].toInt(&ok);
    12. xe = temp[2].toInt(&ok);
    13. ye = temp[3].toInt(&ok);
    14. angle = temp[4].toDouble(&ok);
    15. p.save();
    16. p.translate( xs, ys );
    17. //p.rotate( angle );
    18. p.fillRect(-xe/2, -ye/2, xe, ye, Qt::black);
    19. //p.rotate( angle );
    20. p.restore();
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 
    Neither of the p.rotates do anything.
    Any suggestions ?
    Thanks

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question on QPainter setWindow use

    Quote Originally Posted by impeteperry
    Neither of the p.rotates do anything.
    What is the value of angle variable?

  15. #13
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question on QPainter setWindow use

    Quote Originally Posted by jacek
    What is the value of angle variable?
    They vary, but always in 1st or 4th quadrant and are in Radians

  16. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question on QPainter setWindow use

    Quote Originally Posted by impeteperry
    They vary, but always in 1st or 4th quadrant and are in Radians
    Did you try degrees?

  17. #15
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question on QPainter setWindow use

    Quote Originally Posted by jacek
    Did you try degrees?
    No, but I did now and it worked
    thanks

Similar Threads

  1. [qt4,win,g++] QPainter on a QGLWidget
    By jh in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2008, 06:29
  2. Replies: 7
    Last Post: 20th March 2006, 20:03
  3. ERROR:: QPainter: Internal error; no available GC
    By Krishnacins in forum Qt Programming
    Replies: 2
    Last Post: 8th March 2006, 06:05
  4. 2 Questions about QPainter
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 22nd February 2006, 15:08
  5. passing a QPainter object to widgets
    By vratojr in forum Qt Programming
    Replies: 9
    Last Post: 11th January 2006, 15:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.