Hafnernuss
Hafnernuss

Reputation: 2827

Vertical Row spacing in QTreeWidget

I have a QTreeWidget which is populated with custom widgets. I retrieve the item type from an external API, it may be a text value, a numeric value or whatever. Depending on the type, I provide different controls to the QTreeWidgetItem. For example a QLabel for textual input, a QSpinBox for numeric values and so on.

This is done via the following code:

for (GenApi::INode * poNode : oNodeList)  // iterate over a list of items   which i want to represent in the treewidget 
{
QTreeWidgetItem * poRootItem = new QTreeWidgetItem(poTree); //poTree is a   QTreeWidget
poRootItem->setText(0, poNode->GetDisplayName().c_str());
poTree->addTopLevelItem(poRootItem);                        // add as category

GenApi::NodeList_t oInnerNodes;
poNode->GetChildren(oInnerNodes);

for (GenApi::INode * poInnerNode : oInnerNodes)             // each of those nodes may have innter child nodes
{
    QTreeWidgetItem * poItem = new QTreeWidgetItem();
    CNodeItemBase * poNodeUI = NULL;

    if (GenApi::CIntegerPtr(poInnerNode) != NULL)
        poNodeUI = new CNodeItemInteger(*poInnerNode, poTree);  //CNodeItem... inherits from QWidget and takes the tree as parent

    else if (GenApi::CStringPtr(poInnerNode) != NULL)
        poNodeUI = new CNodeItemString(*poInnerNode, poTree);

    // more possibilities go here....

    if (poNodeUI != NULL)
    {
        poRootItem->addChild(poItem);
        poItem->setText(0, poNodeUI->GetDisplayName().c_str());  // set text of the item
        poTree->setItemWidget(poItem, 1, poNodeUI->m_poControl); // set label/spinbox as widget of the treeitem  
    }
}
}

The code works, but the resulting TreeWidget has a problem:

enter image description here

The resulting TreeWidgetItem has a lot of spacing which makes the TreeWidget hard to read/iterate visually. Is there a fast and easy way to provide something like a QSizePolicy which shrinks the Items? I have tried every combination, but nothing worked so far.

Upvotes: 1

Views: 4025

Answers (1)

jonspaceharper
jonspaceharper

Reputation: 4367

Since you're using widgets with a layout, make sure to call setContentsMargins on the layout with smaller/appropriate values (the default is six pixels on each edge, despite what the docs say).

Upvotes: 2

Related Questions