使用 OGDF 和 Qt 显示图形

发布于 2024-12-09 02:54:05 字数 207 浏览 0 评论 0原文

我希望实现一个使用和显示简单图形的应用程序。其中一个是一棵树,一个就像一个机器人。

我决定除了 Qt 之外还使用 OGDF,因为我需要布局。但我不太明白这一点...我是否必须自己实现所有绘图/定位功能(例如从 GraphAttributes 获取所有节点和边缘坐标)或者 OGDF 是否为此提供了一些不错的接口? (与 GraphAttributes::writeGML() 一样好)

Im looking to implement an application that is working with and displaying simple graph. One of those is a tree, one is like an automaton.

I decided to use OGDF in addition to Qt, because i need layouting. But Im not quite getting this...do I have to to implement all the drawing/positioning functionality myself (like fetching all the node and edge coordinates from GraphAttributes) or does OGDF provide some nice interface for that? (as nice as GraphAttributes::writeGML())

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

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

发布评论

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

评论(1

⊕婉儿 2024-12-16 02:54:05

我找不到任何好的界面,所以我只是自己获取坐标,但是,这种方法并不完美,因为布局算法返回负坐标(相对于我认为的图形中心点,而不是正常的左上角原点)。我的代码看起来有点像这样:

int nodeWidth = 30, nodeHeight = 30, siblingDistance = nodeWidth + nodeHeight;

ogdf::TreeLayout treeLayout;
treeLayout.siblingDistance(siblingDistance);
treeLayout.call(GA);

int width = GA.boundingBox().width(), height = GA.boundingBox().height();

ui->graphView->scene()->setSceneRect(QRect(0, 0, width+nodeWidth, height+nodeHeight));
cout << "Scene dimensions: " << GA.boundingBox().width() << " x " << GA.boundingBox().height() << endl;

GA.setAllWidth(nodeWidth);
GA.setAllHeight(nodeHeight);

ogdf::edge e;
forall_edges(e,graph){
    ogdf::node source = e->source(), target = e->target();
    int x1 = GA.x(source), y1 = GA.y(source);
    int x2 = GA.x(target), y2 = GA.y(target);
    QPainterPath p;
    p.moveTo(x1 + nodeWidth/2, y1 + nodeHeight/2);
    p.lineTo(x2 + nodeWidth/2, y2 + nodeHeight/2);
    (void) ui->graphView->scene()->addPath(p, QPen(Qt::darkGray), QBrush(Qt::white));
}

ogdf::node n;
forall_nodes(n, graph) {
    double x = GA.x(n);
    double y = GA.y(n);
    double w = GA.width(n);
    double h = GA.height(n);
    QRectF boundingRect(x, y, w, h);
    cout << x << " : " << y << " : " << endl;
    QRadialGradient radialGradient(boundingRect.center(), boundingRect.width());
    radialGradient.setColorAt(1.0, Qt::lightGray);
    radialGradient.setColorAt(0.7, QColor(230,230,240));
    radialGradient.setColorAt(0.0, Qt::white);
    (void) ui->graphView->scene()->addEllipse(boundingRect, QPen(Qt::black), QBrush(QRadialGradient(radialGradient)));
    QGraphicsTextItem *text = ui->graphView->scene()->addText(QString(GA.labelNode(n).cstr()));
    text->setPos(x, y);
}

// clear the graph after it has been displayed
graph.clear();

I couldn't find any nice interface so I am just fetching the coordinates myself, however, this approach is not perfect since the layout algorithm returns negative coordinates (relative to a graph center point I think, instead of the normal topleft origin). My code looks kinda like this:

int nodeWidth = 30, nodeHeight = 30, siblingDistance = nodeWidth + nodeHeight;

ogdf::TreeLayout treeLayout;
treeLayout.siblingDistance(siblingDistance);
treeLayout.call(GA);

int width = GA.boundingBox().width(), height = GA.boundingBox().height();

ui->graphView->scene()->setSceneRect(QRect(0, 0, width+nodeWidth, height+nodeHeight));
cout << "Scene dimensions: " << GA.boundingBox().width() << " x " << GA.boundingBox().height() << endl;

GA.setAllWidth(nodeWidth);
GA.setAllHeight(nodeHeight);

ogdf::edge e;
forall_edges(e,graph){
    ogdf::node source = e->source(), target = e->target();
    int x1 = GA.x(source), y1 = GA.y(source);
    int x2 = GA.x(target), y2 = GA.y(target);
    QPainterPath p;
    p.moveTo(x1 + nodeWidth/2, y1 + nodeHeight/2);
    p.lineTo(x2 + nodeWidth/2, y2 + nodeHeight/2);
    (void) ui->graphView->scene()->addPath(p, QPen(Qt::darkGray), QBrush(Qt::white));
}

ogdf::node n;
forall_nodes(n, graph) {
    double x = GA.x(n);
    double y = GA.y(n);
    double w = GA.width(n);
    double h = GA.height(n);
    QRectF boundingRect(x, y, w, h);
    cout << x << " : " << y << " : " << endl;
    QRadialGradient radialGradient(boundingRect.center(), boundingRect.width());
    radialGradient.setColorAt(1.0, Qt::lightGray);
    radialGradient.setColorAt(0.7, QColor(230,230,240));
    radialGradient.setColorAt(0.0, Qt::white);
    (void) ui->graphView->scene()->addEllipse(boundingRect, QPen(Qt::black), QBrush(QRadialGradient(radialGradient)));
    QGraphicsTextItem *text = ui->graphView->scene()->addText(QString(GA.labelNode(n).cstr()));
    text->setPos(x, y);
}

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