Important: Please read the Qt Code of Conduct -

How to integrate QGraphicsPolygonItem with QtXML.

  • Hello Everyone,
    Need help for my project. I am working on import and export of XML file. For this I am using Qt XML. My Task is to read XML file so, I am trying for XML parser which will convert it into tree structure and further this generated tree structure I have to connect with the symbols like circle, squares etc. theses symbols are already designed by using QGraphicsPolygonItem class but I am getting how can I connect generated tree structre which is repsentation of my xml with these grahical element.

    Thank you so much in advance

  • Lifetime Qt Champion


    Well, technically it's up to you to parse that XML and generate QGraphicsPolygonItem from the data you get. There's not much more to it unless I misunderstood your question.

  • @SGaist but how to generate QGraphicsPolygonItem?? I am am able to parse the XML file
    following is the code for reading XML file just a little example this code I got and understood it. But now suppose I want to connect root element with the one of the graphical element like square. How to do that? please help with little code if you can just as example

    //DOM methode of reading XML file

    void ListElements(QDomElement root, QString tagname, QString attribute)
    QDomNodeList items = root.elementsByTagName(tagname);

    qDebug()<<"Total items = " << items.count();
    for(int i=0; i<items.count(); i++){
        QDomNode itemnode =;
        //convert to element
            QDomElement itemele = itemnode.toElement();


    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    QDomDocument document;
    // Load the XML file or open the XML file
    QFile file("C:/Users/Shri/Desktop/QT/Myxmlexample/writexml.xml");
    if(! |QIODevice::Text)){
        qDebug() << "Failed to open the file";
        return -1;
            qDebug()<<"Failed to get the content";
            return -1;
    //get the root elements
    QDomElement root = document.firstChildElement();
    //List the book element
    qDebug()<<"\r\n More Advance \r\n";
    QDomNodeList books = root.elementsByTagName("Book");
    for(int i=0; i<books.count();i++)
        QDomNode booknode =;
        //convert to an element
            QDomElement book = booknode.toElement();
            qDebug()<<"Chapter in " << book.attribute("Name");
    qDebug()<<"End Success";
    return a.exec();


  • @SGaist This is GUI application on touch screen

  • Lifetime Qt Champion

    Being on a touch screen or desktop has no influence here.

    In order to help get started (except taking a look at the Graphics View Framework documentation)
    What do you get from your XML ? Geometries ? Coordinates ? Do you want to represent an arbitrary piece of data with some sort of picture ? e.g. a node represents a book so you want to draw a book to represent it on your scene ?

  • @SGaist Thank you so much.

    For example: XML contain text data like root element and then inside that some other element and so on. When I read XML file and parse it, it store it into tree data structure with the help of DOM parser. Now after this I have root element store somewhere and also complete hierarchy is build accordingly into tree. On the other hand I have Graphical element which are just dummy symbols like circle (While I called set, this is just a name of symbol), Square (Which is represent tuples) etc. But these symbol have no identity or semantics now and Semantics is in XML which I save into Tree so I want to some how connect this two so that When I import XML file then this symbols connect with each other according to the data in XML.

    I hope you understand what I want to say.. I am sorry I am bad in explaining my concept but I tried.
    Many Thanks once again

  • Lifetime Qt Champion

    Yes I do.

    For the linking part, take a look at the "Elastic Nodes" example.

    As for translating your XML, you can create a subclass of QGraphicsItem which will contain the type of data it represent and draw itself accordingly.

  • @SGaist Thank you so much. Can you please give me little example any code so that I can understand it better as I am new in programming and trying to do it at my level

    Many Thanks

  • Lifetime Qt Champion

  • @mrjj Thank you I will look into it

Log in to reply