如何使用GraphViz库c ++将图形写入png?

时间:2018-02-16 03:03:08

标签: c++ graphviz image-graphviz

它是一个Qt应用程序,但没有Gui。我正在尝试使用GraphViz C ++库创建一个图形。一切都运行良好,除了我无法获得我可以在Windows中打开的.png文件中的输出。我按手册说的那样,没有错误,只是没有生成输出文件。我搜索了每个可能的目录但是徒劳无功。请指导我。

gw.h头文件

#ifndef GW_H
#define GW_H

#include <QApplication>
#include <QDebug>

#include "gvc.h"
#include <iostream>
#include <map>
using namespace std;

struct Node
{
    NODES() {}
    Agnode_t* actualNode;
    char* name;

};

class GRAPHVIZ_WRAPPER {
public:
    GVC_t* graphContext;
    Agraph_t* graph;
    map <char*,Agnode_t*> nodesTable;

    GRAPHVIZ_WRAPPER(int x)
    {
        graphContext = gvContext();
        graph = agopen("Node Graph" , Agdirected , NULL);
    }


    void addNode(char* lable)
    {
        qDebug("Adding Node");
        Agnode_t* node = agnode(graph,lable,TRUE);
        nodesTable.insert(pair<char*,Agnode_t*>(lable,node));
    }

    void addEdge(char* node1 , char* node2, char* lable)
    {

        qDebug("Adding Edge");
        Agnode_t* tailNode = nodesTable[node1];
        Agnode_t* headNode = nodesTable[node2];

        agedge(graph,tailNode,headNode,lable,TRUE);
    }

    void draw()
    {
        gvLayout(graphContext,graph,"dot");
        gvRenderFilename(graphContext,graph,"png","QwertyGrap.pngh");
        gvFreeLayout(graphContext,graph);
    }

    void close()
    {
        agclose(graph);
    }
};




#endif // GW_H

Main.cpp文件

#include "gw.h"
#include <QApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
   // MainWindow w;
    //w.show();

    //w.drawGraph();
    GRAPHVIZ_WRAPPER Mgraph(5);
    Mgraph.addNode("Node1");
    Mgraph.addNode("Node2");
    Mgraph.addNode("Node3");
    Mgraph.addEdge("Node1","Node2","76");
    Mgraph.addEdge("Node1","Node3","86");
    Mgraph.addEdge("Node2","Node1","96");

    Mgraph.draw();
    qDebug("Drawing");
    Mgraph.close();


    return a.exec();
}

输出(如您所见,程序正在运行,没有错误)

enter image description here

0 个答案:

没有答案