How to set QGraphicsScene/View to a specific coordinate system

南楼画角 提交于 2019-12-03 11:07:02

问题


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

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

Many thanks,

Carlos.


回答1:


Use QGraphicsScene::setSceneRect() like so:

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

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.

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();
}


来源:https://stackoverflow.com/questions/10443894/how-to-set-qgraphicsscene-view-to-a-specific-coordinate-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!