我正在使用Qt 4.7 QPainter繪製一些多邊形,等等到一個小部件。我希望改變座標系使得(0,0)位於我的小部件的中心,並且x/y軸以標準的「笛卡爾」方式運行(即,y增加「向上」並減小「下」)。換句話說,如果你知道我的意思,我希望座標是「數學」,而不是「計算機圖形」。 :-)使用窗口/視口翻轉QPainter y軸
我正在嘗試使用setViewport()和setWindow()來做這件事,而不是自己做數學,因爲它能夠直接用我的座標直接調用繪圖方法。
這裏是我到目前爲止有:
// Setup coordinates
double screenWidth = width();
double screenHeight = height();
double windowWidth = 100.0;
double windowHeight = (screenHeight/screenWidth) * windowWidth;
painter.setViewport(0, 0, screenWidth, screenHeight);
painter.setWindow(-(windowWidth/2.0), -(windowHeight/2.0), windowWidth, windowHeight);
// Draw stuff
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::blue);
painter.drawRect(-10, -10, 20, 20);
現在,這工作得很好,因爲它吸引一個漂亮的藍色方塊在屏幕的中間。問題是,我不得不說,左上角是(-10,-10)。我希望能夠做到這一點(-10,10),因爲這就是笛卡爾座標系中的情況。
我試圖搞亂setWindow/setViewport來得到這個「y軸翻轉」,但無濟於事。這似乎是一件非常簡單/基本的事情,但在淘汰Qt文檔和網頁之後,我無法弄清楚!
謝謝,
克里斯
正確,但是需要提及的是,文本也會翻轉。這大多是不期望的! – bkausbk 2014-03-24 12:50:11
感謝您的更新;) – Stormenet 2014-12-09 17:06:13