@mrjj Thanks for your reply. Wow, that sounds complex. I'm basically a newbie in Qt (and for practical purposes, in C++ as well). I don't feel skilled enough to do that.
But I found a workaround in the exact things I didn't want to do.
I could not find a way to format part of the text in a column. Tried HTML/Style sheets, but the available tags and properties are not enough for me. So I "packed" two labels in a widget and put that inside each each item widget in a tree with one single column. Works marvels but looks redundant and amateurish.
Anyway, this is the code I'm using now:
void STEdWindow::createXMLTreeView(QTreeWidget *trwTreeView,
QTreeWidgetItem *trwParent,
QDomNode domChild,
QString sElementCSS,
QString sTextCSS) {
QTreeWidgetItem *trwChild;
QLabel *lblName;
QLabel *lblValue;
QHBoxLayout *layChild;
QWidget *wgtChild;
while(!domChild.isNull()) {
if(QDomDocument::NodeType::ElementNode==domChild.nodeType()) {
if(trwParent==nullptr) {
trwChild=new QTreeWidgetItem(trwTreeView);
trwTreeView->setHeaderHidden(true);
}
else {
trwChild=new QTreeWidgetItem(trwParent);
trwParent->addChild(trwChild);
trwParent->setExpanded(true);
}
lblName=new QLabel(trwTreeView);
lblValue=new QLabel(trwTreeView);
layChild=new QHBoxLayout(trwTreeView);
wgtChild=new QWidget(trwTreeView);
lblName->setText(domChild.nodeName());
lblName->setSizePolicy(QSizePolicy::Policy::Fixed,
QSizePolicy::Policy::Fixed);
lblName->setStyleSheet(sElementCSS);
lblValue->setSizePolicy(QSizePolicy::Policy::Preferred,
QSizePolicy::Policy::Fixed);
lblValue->setStyleSheet(sTextCSS);
layChild->addWidget(lblName);
layChild->addSpacing(DEF_TREE_NODE_SPACING);
layChild->addWidget(lblValue);
layChild->addStretch();
layChild->setContentsMargins(DEF_TREE_NODE_SPACING,
DEF_TREE_NODE_SPACING,
DEF_TREE_NODE_SPACING,
DEF_TREE_NODE_SPACING);
wgtChild->setLayout(layChild);
trwTreeView->setItemWidget(trwChild,0,wgtChild);
this->createXMLTreeView(trwTreeView,
trwChild,domChild.firstChild(),
sElementCSS,
sTextCSS);
}
else if(QDomDocument::NodeType::TextNode==domChild.nodeType()) {
lblValue=qobject_cast<QLabel *>(trwTreeView->itemWidget(trwParent,0)->layout()->itemAt(2)->widget());
lblValue->setText(domChild.nodeValue().trimmed());
}
domChild=domChild.nextSibling();
}
}
Used like this:
QDomDocument domDoc;
// Set domDoc's Content to any valid XML
QTreeWidget *trwTreeView=new QTreeWidget(this);
this->createXMLTreeView(trwTreeView,
nullptr,
domDoc.documentElement(),
"padding: 10px; border: 1px solid; border-radius: 10px",
"font-style: italic");
And with DEF_TREE_NODE_SPACING 10, I get this:
[image: 4671c55b-605a-43b5-9ade-386569908da7.png]
BTW, that long line, the cast...I think it's an abomination haha...Is there a correct way to do that? I.e., getting the 2nd label in the parent item widget.