gpt4 book ai didi

c++ - 你如何在QT中绘制点?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:53:38 24 4
gpt4 key购买 nike

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

你是怎么做到的?

最佳答案

图形 View ,addEllipse

QGraphicsView 可以很好地进行 2D 绘图,并为您提供多种显示方式的选项。它不像 qwt 那样专为绘制科学数据而量身定制,但仅用于显示一堆点、几何或动画以及许多其他东西时效果很好。参见 Qt 的 Graphics View Framework文档和示例。

下面是如何在 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();
}

注意:您可能需要在您的 View 上调用 setSceneRect,否则场景将自动居中。阅读 Qt 文档中对 QGraphicsSceneQGraphicsView 的说明。您可以缩放 View 以显示更多或更少的场景,它可以放入滚动条。我回答了一个相关的 question我在这里展示了更多关于 QGraphicsView 的功能,您可能还想看看。

关于c++ - 你如何在QT中绘制点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7800460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com