首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用OGDF和Qt显示图形

使用OGDF和Qt显示图形
EN

Stack Overflow用户
提问于 2011-10-11 18:34:27
回答 1查看 2.7K关注 0票数 6

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

除了Qt,我决定使用OGDF,因为我需要布局。但是我不太理解this...do,我必须自己实现所有的绘图/定位功能(比如从GraphAttributes中获取所有的节点和边坐标),或者OGDF会为此提供一些很好的接口吗?(和GraphAttributes::writeGML()一样好)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-03 21:09:09

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

代码语言:javascript
复制
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();
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7724645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档