如何将 QGraphicsScene/View 设置为特定坐标系 [英] How to set QGraphicsScene/View to a specific coordinate system

查看:100
本文介绍了如何将 QGraphicsScene/View 设置为特定坐标系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 QGraphicsScene 中绘制多边形,但多边形具有纬度/经度位置.在等距柱状投影中,坐标来自:

I want to draw polygons in a QGraphicsScene but where the polygons has latitude/longitude positions. In a equirectangular projection the coordinates goes from:

                       ^
                      90
                       |
                       |
-180----------------------------------->180
                       |
                       |
                     -90

如何将 QGraphicsScene/QGraphicsView 设置为这样的投影?

How can I set the QGraphicsScene / QGraphicsView to such projection?

非常感谢,

卡洛斯.

推荐答案

使用 QGraphicsScene::setSceneRect() 像这样:

scene->setSceneRect(-180, -90, 360, 180);

如果您担心垂直轴被错误地翻转,您有几种解决方法.一种方法是在进行任何涉及 y 坐标的计算时简单地乘以 -1.另一种方法是垂直翻转 QGraphicsView,使用 view->scale(1, -1) 以便正确显示场景.

If you're concerned about the vertical axis being incorrectly flipped, you have a few options for how to deal with this. One way is to simply multiply by -1 whenever you make any calculation involving the y coordinate. Another way is to vertically flip the QGraphicsView, using view->scale(1, -1) so that the scene is displayed correctly.

下面是一个使用后一种技术的工作示例.在示例中,我将 QGraphicsScene 子类化,以便您可以在视图中单击,并且自定义场景将使用 qDebug() 显示单击位置.实际上,您实际上并不需要继承 QGraphicsScene.

Below is a working example that uses the latter technique. In the example, I've subclassed QGraphicsScene so that you can click in the view, and the custom scene will display the click position using qDebug(). In practice, you don't actually need to subclass QGraphicsScene.

#include <QtGui>

class CustomScene : public QGraphicsScene
{
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        qDebug() << event->scenePos();
    }
};

class MainWindow : public QMainWindow
{
public:
    MainWindow()
    {
        QGraphicsScene *scene = new CustomScene;
        QGraphicsView *view = new QGraphicsView(this);
        scene->setSceneRect(-180, -90, 360, 180);
        view->setScene(scene);
        view->scale(1, -1);
        setCentralWidget(view);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

这篇关于如何将 QGraphicsScene/View 设置为特定坐标系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆