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:
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.
由于您使用的是具有布局的窗口小部件,因此请确保使用较小/适当的值(无论文档怎么说,默认为每个边缘六个像素)在布局上调用setContentsMargins
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.