6
我正在实施一个正在使用并显示简单图形的应用程序。其中之一是一棵树,就像一个自动机。使用OGDF和Qt显示图形
我决定除了Qt之外还使用OGDF,因为我需要布局。但我没有得到这个......我必须自己实现所有绘图/定位功能(比如从GraphAttributes获取所有节点和边缘坐标)还是OGDF提供了一些很好的界面? (和GraphAttributes :: writeGML()一样好)
我正在实施一个正在使用并显示简单图形的应用程序。其中之一是一棵树,就像一个自动机。使用OGDF和Qt显示图形
我决定除了Qt之外还使用OGDF,因为我需要布局。但我没有得到这个......我必须自己实现所有绘图/定位功能(比如从GraphAttributes获取所有节点和边缘坐标)还是OGDF提供了一些很好的界面? (和GraphAttributes :: writeGML()一样好)
我找不到任何漂亮的界面,所以我只是自己提取坐标,但是这种方法并不完美,因为布局算法返回的是负坐标(相对于图形中心点我认为,而不是正常的源头)。我的代码看起来有点像这样:
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();
非常感谢!正如我现在看到的,我走在了正确的道路上。有示例代码是非常有帮助的!你对OGDF有经验吗? – TeaOverflow
哦,并且节点的负坐标与QGraphicScene很好地工作,因为它的'0,0'似乎也在它的中心。 – TeaOverflow