Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTreeWidgetItem: Modifying separation between item columns



  • Hi.

    I don't even know how to ask this. So please, check this image:

    STEd.png

    Notice the variable separation between item column contents, highlighted in boxed red arrows.

    This is automatically set the way all the values in the 2nd column start at the same point.

    Well, check this other image and tell me if the following is possible:

    STEd-different.png

    I.e., a fixed separation, depicted as a red box, between column values.

    I know I could just place the whole text in a single column, and use some spaces to give a false separation, BUT, I don't think this will work for proportional fonts. AND, more important, I need to add some decoration to the first column values ("message", "to", "first", etc.), like codebeautify.org does it:

    CodeBeautify.png

    Any ideas??

    I'm attaching my current code for reference...

    void STEdWindow::createXMLTreeView(QTreeWidgetItem *trwParent,QDomNode domChild) {
        QTreeWidgetItem *trwChild;
        trwParent->setExpanded(true);
        while(!domChild.isNull()) {
            if(QDomDocument::NodeType::ElementNode==domChild.nodeType()) {
                trwChild=new QTreeWidgetItem(trwParent);
                trwChild->setText(0,domChild.nodeName());
                trwParent->addChild(trwChild);
                this->createXMLTreeView(trwChild,domChild.firstChild());
            }
            else if(QDomDocument::NodeType::TextNode==domChild.nodeType())
                trwParent->setText(1,domChild.nodeValue().trimmed());
            domChild=domChild.nextSibling();
        }
    }
    

    The previous thing is used like this:

            QDomDocument domDoc;
            // Set domDoc's Content to any valid XML
            QTreeWidget *trwTreeView=new QTreeWidget(this);
            trwTreeView->setHeaderHidden(true);
            trwTreeView->setColumnCount(2);
            QTreeWidgetItem *trwItem=new QTreeWidgetItem(trwTreeView);
            trwItem->setText(0,domDoc.documentElement().tagName());
            this->createXMLTreeView(trwItem,domDoc.documentElement().firstChild());
    

    Any help would be greatly appreciated. =)


  • Lifetime Qt Champion

    Hi
    Im don't think its possible to get columns to start in different
    positions pr row outside of creating a new view.

    However, you could use a Delegate and handling the text
    painting yourself and draw it as shown using the data from the model
    It could even allow editing if done correctly.



  • @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:

    STEd-result.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.


Log in to reply