def _create_item(self, nid, node, obj, index=None):
        """Create  a new TreeWidgetItem as per word_wrap policy.

        Index is the index of the new node in the parent:
        None implies append the child to the end.

        """
        if index is None:
            cnid = QtWidgets.QTreeWidgetItem(nid)
        else:
            cnid = QtWidgets.QTreeWidgetItem()
            nid.insertChild(index, cnid)

        cnid.setText(0, node.get_label(obj))
        cnid.setIcon(0, self._get_icon(node, obj))
        cnid.setToolTip(0, node.get_tooltip(obj))

        color = node.get_background(obj)
        if color:
            cnid.setBackground(0, self._get_brush(color))
        color = node.get_foreground(obj)
        if color:
            cnid.setForeground(0, self._get_brush(color))

        return cnid
    def _insert_node(self, nid, index, node, obj):
        """Inserts a new node before a specified index into the children of
        the specified node.

        """

        cnid = self._create_item(nid, node, obj, index)

        has_children = self._has_children(node, obj)
        self._set_node_data(cnid, (False, node, obj))
        self._map.setdefault(id(obj), []).append((node.get_children_id(obj), cnid))
        self._add_listeners(node, obj)

        # Automatically expand the new node (if requested):
        if node.allows_children(obj):
            if has_children and node.can_auto_open(obj):
                cnid.setExpanded(True)
            else:
                # Qt only draws the control that expands the tree if there is a
                # child.  As the tree is being populated lazily we create a
                # dummy that will be removed when the node is expanded for the
                # first time.
                cnid._dummy = QtWidgets.QTreeWidgetItem(cnid)

        # Return the newly created node:
        return cnid