[SOLVED] QDomNode is always null
-
I wrote some method which returns QDomNode :
@
private: QDomNode createNode(QStandardItem * item) {if (item == NULL) return QDomNode(); QDomNode node; node.toElement().setTagName(item->text()); // write attributes QStandardItem * parent = item->parent(); if (parent == NULL) parent = invisibleRootItem(); for (int i = 1; i < columnCount(); i++) { QStandardItem * columnItem = parent->child(item->row(), i); if (columnItem == NULL) continue; QString sValue = columnItem->text(); if (sValue.isEmpty()) continue; node.toElement().setAttribute(headerData(i,Qt::Horizontal,Qt::DisplayRole).toString(), sValue); } int childCount = item->rowCount(); for (int i = 0; i < childCount; i++) { QDomNode childNode = createNode(item->child(i)); if (childNode.isNull()) continue; node.appendChild(childNode); } return node;
}
@
But it always returns a null node, no matter of arguments. What am I doing wrong ? -
because you use lines like these:
@
node.toElement()...
@You should create a QDomElement and do all your work directly on it instead a QDomNode and convert it every time.
Currently you only call your methods on on converted elements (from your intitial node) ... which get discarded right in the next line ... -
You are not creating a QDomNode using QDomDocument::createElement() or similar functions. Since the stack variable QDomNode node is null, toElement() also returns a null. Create a valid node element first using QDomDocument variable for which the node is to be created.
@ QDomDocument doc;
QDomElement elem = doc.createElement("element"); @
Pass the doc element or create a new one as required. Refer QDomDocument and QDomNode and QDomElement documentation