QT中如何绘制点?

发布于 2024-12-10 10:52:18 字数 121 浏览 0 评论 0 原文

我正在用 QT 用 C++ 编写一个应用程序,其中有 n 个点并计算其凸包。然而,一旦计算出来,我不知道如何绘制点并绘制船体的边界。制作菜单按钮等很简单,但我不确定我是否知道执行此操作的工具。

你如何做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唔猫 2024-12-17 10:52:18

图形视图,addEllipse

QGraphicsView 可以很好地进行 2D 绘图,并为您提供了许多如何显示它的选项。它不像 qwt 那样适合绘制科学数据,而只是为了显示一堆点、几何图形或动画以及许多其他东西而工作得很好。请参阅 Qt 的图形视图框架文档和示例。

以下是如何在 QGraphicsScene 中绘制一堆点并将其显示在 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();
}

注意:您可能需要在视图上调用setSceneRect,否则场景将自动居中。阅读 Qt 文档中对 QGraphicsSceneQGraphicsView 的描述。您可以缩放视图以显示更多或更少的场景,并且可以放入滚动条。我回答了相关的 问题,我将在其中详细介绍您可以使用 QGraphicsView 执行哪些操作您可能还想看看。

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.

夜无邪 2024-12-17 10:52:18

您只需创建一个派生自 QWidget 的自定义类,并在其中重写 void PaintEvent(QPaintEvent* event) 方法。您可以将点放入某种点列表中,即 std::vectorQList,然后使用 Polyline 方法绘制它。例如:

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

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()));
}
沒落の蓅哖 2024-12-17 10:52:18

有一个图表库 qwt,它提供 Qt 小部件用于 - 呃 - 图表目的。

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

困倦 2024-12-17 10:52:18

Qt Charts、QML 或 GraphicsView

这将是我的 QGraphics View 示例的更新,但它有点长,而且它确实是回答问题的完全不同的方法。

Qt Charts(自 2016 年起提供 LGPL)是无需第三方库即可实现此目的的好方法。

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

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

对于凸包具体示例,您可能想要QAreaSeries 图表。

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

希望有帮助。

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/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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文