Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeWidgetItem
Forum Updated to NodeBB v4.3 + New Features

QTreeWidgetItem

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Beka
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerManu
      wrote on last edited by
      #2

      How about QTreeWidgetItem::setDisabled ?

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Beka
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Beka
          wrote on last edited by
          #4

          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 :(

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DerManu
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved