How do you plot points in QT?

戏子无情 提交于 2019-12-31 17:14:03

问题


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 draw the border of the hull. Making menu buttons and such is simple enough, but I'm not sure I know the tools to do this.

How do you do this?


回答1:


You can just create a custom class deriving from QWidget where you override the void paintEvent(QPaintEvent* event) method. In that you put the points into some sort of point list, either std::vector<QPoint> or QList<QPoint> and then paint it with a Polyline method. For instance:

void Foo::paintEvent(QPaintEvent* event)
{
  QPainter painter(this);
  std::vector<QPoint> points;
  // Fill points with the points
  painter.drawPolyLine(points.data(), static_cast<int>(points.size()));
}



回答2:


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 <QtGui/QApplication>

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QPointF>
#include <QVector>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QVector <QPointF> 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.




回答3:


There is a charting library, qwt, that provides Qt widgets for - erm - charting purposes.




回答4:


Qt Charts, QML or GraphicsView

This was going to be an update to my QGraphics View example, but it got kind of long, and it really is a completely different method to answer the question.

Qt Charts (LGPL available since 2016) is a great way to do this without needing a third party library.

  • https://doc.qt.io/qt-5/qtcharts-linechart-example.html
  • http://blog.qt.io/blog/2016/01/18/qt-charts-2-1-0-release/
  • https://doc.qt.io/qt-5/qtcharts-overview.html

https://doc.qt.io/qt-5/qlineseries.html#QLineSeries

QLineSeries* series = new QLineSeries();
series->append(0, 6);
series->append(2, 4);
...
chart->addSeries(series);

For the convex hull example specifically, you probably want the QAreaSeries chart.

https://doc.qt.io/qt-5/qtcharts-areachart-example.html
https://doc.qt.io/qt-5/qareaseries.html

QLineSeries *series0 = new QLineSeries();
QLineSeries *series1 = new QLineSeries();
*series0 << QPointF(1, 5) << QPointF(3, 7) << QPointF(7, 6) << QPointF(9, 7) << QPointF(12, 6)
         << QPointF(16, 7) << QPointF(18, 5);
*series1 << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF(8, 2) << QPointF(12, 3)
         << QPointF(16, 4) << QPointF(18, 3);
QAreaSeries *series = new QAreaSeries(series0, series1);

Hope that helps.



来源:https://stackoverflow.com/questions/7800460/how-do-you-plot-points-in-qt

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