QTreeWidgetItem



  • I read source code of method QTreeWidgetItem::setFlags(Qt::ItemFlags)
    and there are written:
    @if (!child->d->disabled) { // if not explicitly disabled@
    but how can I explicitly disable item?



  • How about QTreeWidgetItem::setDisabled ?



  • No, it is calling method setFlags, but i think I get it, it is when in source code they wrote:
    @d->disabled = !enable;@



  • now, I have another problem, I subclassed QTreeWidgetItem, and the method setFlags(), it is very simple, I only want to set itemFlags, and dont set it to children, the source code of QT:

    @void QTreeWidgetItem::setFlags(Qt::ItemFlags flags)
    {
    const bool enable = (flags & Qt::ItemIsEnabled);
    const bool changedState = bool(itemFlags & Qt::ItemIsEnabled) != enable;
    const bool changedExplicit = d->disabled != !enable;

    d->disabled = !enable;
    
    if (enable && par && !(par->itemFlags & Qt::ItemIsEnabled)) // inherit from parent
        itemFlags = flags & ~Qt::ItemIsEnabled;
    else // this item is explicitly disabled or has no parent
        itemFlags = flags;
    
    if (changedState && changedExplicit) { // if the propagate the change to the children
        QStack<QTreeWidgetItem*> parents;
        parents.push(this);
        while (!parents.isEmpty()) {
            QTreeWidgetItem *parent = parents.pop();
            for (int i = 0; i < parent->children.count(); ++i) {
                QTreeWidgetItem *child = parent->children.at(i);
                if (!child->d->disabled) { // if not explicitly disabled
                    parents.push(child);
                    if (enable)
                        child->itemFlags = child->itemFlags | Qt::ItemIsEnabled;
                    else
                        child->itemFlags = child->itemFlags & ~Qt::ItemIsEnabled;
                    child->itemChanged(); // ### we may want to optimize this
                }
            }
        }
    }
    itemChanged();
    

    }@

    and I only want:

    @void MyTreeItem::setFlags(Qt::ItemFlags flags) {
    itemFlags = flags;
    emit emitDataChanged(); //it calls itemChanged();
    }@

    but when I disable MyTreeItem, it isnt really disabled :(



  • QTreeWidgetItem::setFlags isn't virtual, so you can't override it. All you're doing is hiding the base class implementation in your derived class. This leads to the situation that which function is called is dependant on what type the pointer has by which the function is called, not what type the actual pointed to object has. Since the tree widget internally works with the base class (and QTreeWidgetItem itself, too), they all will call the base class implementation of setFlags.

    Basically, don't reimplement non-virtual functions if you can't guarantee that other classes won't access that function by base class pointers (or if that doesn't matter for you).

    You can subclass QAbstractItemModel that has the behaviour you want.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.