Qt QTreeWidget related bugs

 

After adding data, QTreeWidget will appear stuck when you click the expansion arrow to expand and contract. This is a bug in the qt kernel. Under normal circumstances, it should respond immediately. Since it is a bug in qt, you can only implement TreeWidget in another way To solve this bug, there are several specific methods, depending on the needs to determine which method is better:

  1. If the TreeWidget required by the requirement does not require a complete TreeWidget, the level of expansion is limited, that is, there are only 2 layers, 3 layers, or 4 layers. The level of the TreeWidget node required by the requirement is relatively limited. You can use a space like QListView QTreeWidget, as long as you control their level of expansion relationship, the type of the custom response is the parent type or child type, etc., to control the size of the displayed item should be indented. If it is a child node, draw a larger indentation, if it is a parent node, make the indentation smaller, so that you can use a QListView-like effect to make a QTreeWidget-like effect.

2. There is another way to use components similar to QTreeWidget. The bugs about the expansion and contraction of this operation can be hidden and replaced with another method. How to hide it, QTreeWidget has an attribute

indentation : int

indentation of the items in the tree view.

This property holds the indentation measured in pixels of the items for each level in the tree view. For top-level items, the indentation specifies the horizontal distance from the viewport edge to the items in the first column; for child items, it specifies their indentation from their parent items.

By default, the value of this property is style dependent. Thus, when the style changes, this property updates from it. Calling setIndentation() stops the updates, calling resetIndentation() will restore default behavior.

This attribute is specially used to deal with the indentation of the item of QTreeWidget. The more item levels, the more indentation value. If the value of this attribute is set to 0, it will not be indented, so the effect can be achieved The expansion and contraction arrows can be hidden. The qt kernel processing is to put the arrow in the size area set by the identification. If it is set to 0, there is no place to put it, it will be invisible, and it will be hidden. Effect, so that the bug of QTreeWidget is hidden, which is equivalent to solving the bug of QTreeWidget. Since the button of expansion and contraction is hidden, how to realize the function of expansion and contraction, you can bind the corresponding click signal and The slot function is enough.

    connect(ui->treeWidget, &QTreeWidget::itemClicked, [=](QTreeWidgetItem* item,int nidex){

        bool bIsChild = item->data(0, Qt::UserRole).toBool();

        if (!bIsChild)

        {

            item->setExpanded(!item->isExpanded());

        }

    });

The code like this can achieve the function of shrinking and hiding. Since the function is reached, the rest is to deal with the display of the shrinking and hiding buttons. By customizing the item, by setting if the item has child nodes , Just paint it up.

After doing this step, basically the effect of most treeWidgets can be achieved, and the bug of QTreeWidget is solved by this method. The effect is roughly like this, and it is almost indented, that is, click the shrink and hide buttons. To achieve the effect of data, there is no indent value. Because it has been set to 0, it will not be indented. You can draw the indent effect by customizing the indent value. How to draw it, the indent is worth the size Depending on the size of the item level, you first need to obtain a custom item level, the key code is as follows:

QTreeWidgetItem *MainWindow::addChildNode(QTreeWidgetItem *parent, int index)
{
    QTreeWidgetItem *pDeptItem = new QTreeWidgetItem();
    // Set Data to distinguish whether Item is a grouping node or a child node, 0 represents a grouping node, 1 represents a child node
    pDeptItem->setData(0, Qt::UserRole, 1);
    pDeptItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    pDeptItem->setCheckState(1, Qt::Unchecked);
    int level = 0;
    DepartNodeItem *departNode = dynamic_cast<DepartNodeItem*>(ui->tree->itemWidget(parent, 0));
    if (departNode) {
        level = departNode->getLevel();
        level ++;

}

}

void EmployeeNodeItem::setLevel(int level)
{
    this->m_level = level;
    this->m_indentation = this->m_level * INDENTATION;
    this->m_headLabelWidth = this->m_indentation + HEAD_LABEL_WIDTH;
    ui->lbHeadPic->setMinimumWidth(m_indentation);

}

Customize the implementation of the level by adding the interface of the custom node, so that the itemwidget can get the current level hierarchy through the custom implementation, and then calculate the indentation value m_indentation .

Then draw according to this indentation value to achieve the indentation effect.

In this way, the function of QTreeWidget is realized and it is equivalent to solving the QTreeWidget bug about stagnation.

Published 85 original articles · praised 18 · 120,000 views

Guess you like

Origin blog.csdn.net/a1317338022/article/details/105585735