Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use class <code>QMatrix</code>. It specifies 2D transformations. <code>QMatrix</code> is set to <code>QPainter</code>. </p> <p>But remember, in your case, if you convert your widget's coords to <em>Cartesian coords</em>, you will have to put first point at <strong>(-10,-10)</strong> <em>(not at <strong>(-10,10)</strong> as you did mentioned)</em> to draw a rect, which has center at <strong>(0,0)</strong>, because Y-axis now grows <em>up</em> and X-Axis now grows <em>right</em>.</p> <p>All you need is to transform your coord system this way: </p> <ul> <li><p>translate <em>origin</em> from <strong>(0,0)</strong> to the middle of the widget:<br> <img src="https://i.stack.imgur.com/IihZ2.png" alt="alt text"> </p></li> <li><p>scale <em>Y-axis</em> by <strong>-1</strong> factor:<br> <img src="https://i.stack.imgur.com/UEmk8.png" alt="alt text"></p></li> </ul> <p>Here is the code, typed in <code>paintEvent()</code> function of a widget: </p> <pre><code>QPainter pn( this ); int w_2 = width() / 2; int h_2 = height() / 2; { // X- and Y-Axis drawing pn.setPen( Qt::blue ); pn.drawLine( 0, h_2, width(), h_2); // X-Axis pn.drawLine( w_2, 0 , w_2, height() ); // Y-Axis } QMatrix m; m.translate( w_2, h_2 ); m.scale( 1, -1 ); pn.setMatrix( m ); pn.setPen( Qt::NoPen ); pn.setBrush( QBrush( Qt::blue, Qt::Dense4Pattern ) ); pn.drawRect( -10, -10, 20, 20 ); </code></pre> <p>result:<br> <img src="https://i.stack.imgur.com/6Zw18.png" alt="alt text"></p> <p><strong>update apr 07, 2014</strong></p> <p>This question was asked a long time ago and many things have changed since. For those asking themselves the same question today (beginnings of 2014) then my personal answer is that since Qt 4.3 it is possible to solve problem with text flipping more easier.</p> <p>You are right. Text also gets filpped because it is drawn with the same painter. You can draw text at the end, when all flipped drawings are done, if it is possible. This method is not convinient because of new calculations of texts position. Also you will need to drop settings for painter. Now I would recommend you to use <code>QGraphicsView</code>, because of huge support of 2D painting. Also for each <code>QGraphicsItem</code> <code>ItemIgnoresTransformations</code> flag can be set, which allows it to ignore inherited transformations (i.e., its position is still anchored to its parent, but the parent or view rotation, zoom or shear transformations are ignored). This flag is useful for keeping text label items horizontal and unscaled, so they will still be readable if the graphics view is transformed</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload