1. 程式人生 > >QTreeWidget新增自定義的QTreeWidgetItem項的正確方法

QTreeWidget新增自定義的QTreeWidgetItem項的正確方法

例程1:

QTreeWidget *parentTree = itor.value().type == 2 ? ui.PriceTreeWidget : ui.CustomTreeWidget;

auto pItem = new QTreeWidgetItem(parentTree);

if(pItem)
{
  QString name = itor.value().labelName;
  auto pItemWidget = new CTagItemWidget(itor.value().id, name, parentTree);
  pItemWidget->setItemCount(itor.value().count);
  connect(pItemWidget, SIGNAL(sgItemClicked(CTagItemWidget*, TagItemOperatorType)), this, 
  SLOT(OnItemClicked(CTagItemWidget*, TagItemOperatorType)));

if(pItemWidget)
 {
   parentTree->addTopLevelItem(pItem);
   parentTree->setItemWidget(pItem, 0, pItemWidget);
 }

例程2:

if (pMySelf->getOrgId() == organID)
 {
    ui.treeWidgetChatMemberList->insertTopLevelItem(0, pOrganItem); //插入父節點
 }
 else
 {
    ui.treeWidgetChatMemberList->addTopLevelItem(pOrganItem); //追加父節點
 }
  ui.treeWidgetChatMemberList->setItemWidget(pOrganItem, 0, pOrganItemWidget); //把自定義的小部件加入到樹形項中