How do you plot points in QT?

前端 未结 4 2216
渐次进展
渐次进展 2021-02-04 20:00

I am writing an application in C++ with QT where you have n points and compute the convex hull of this. However, once this is computed I have no idea how to plot the points and

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-04 20:12

    Graphics View, addEllipse

    QGraphicsView does 2D plotting very well and gives you many options for how to display it. It isn't as tailored for plotting scientific data as much as qwt, but just for showing a bunch of points, or geometry or animations and lots of other things it works very well. See Qt's Graphics View Framework documentation and examples.

    Here is how you plot a bunch of points in a QGraphicsScene and show it in a QGraphicsView.

    #include 
    
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QVector  points;
    
        // Fill in points with n number of points
        for(int i = 0; i< 100; i++)
           points.append(QPointF(i*5, i*5));
    
        // Create a view, put a scene in it and add tiny circles
        // in the scene
        QGraphicsView * view = new QGraphicsView();
        QGraphicsScene * scene = new QGraphicsScene();
        view->setScene(scene);
    
        for(int i = 0; i< points.size(); i++)
            scene->addEllipse(points[i].x(), points[i].y(), 1, 1);
    
        // Show the view
        view->show();
    
        // or add the view to the layout inside another widget
    
        return a.exec();
    }
    

    Note: You will probably want to call setSceneRect on your view, otherwise the scene will just auto-center it. Read the descriptions for QGraphicsScene and QGraphicsView in the Qt Documentation. You can scale the view to show more or less of the scene and it can put scroll bars in. I answered a related question where I show more about what you can do with a QGraphicsView that you may want to look at also.

提交回复
热议问题