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. Disable certain Rows in QTreeView
Forum Updated to NodeBB v4.3 + New Features

Disable certain Rows in QTreeView

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 7.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.
  • A Offline
    A Offline
    ankush205
    wrote on last edited by
    #1

    I have implemented a directory structure..using QAbstractItemModel and QtreeView

    now I want to disable certain rows like when i click on those rows no signal should be triggered..I read abt Qt::ItemFlags how can i use it to set these
    i call connect(treeview,SIGNAL(clicked(QModelIndex)),this,SLOT(fileselection(QModelIndex)));
    so in certain rows these signals should not be triggered ie disable those rows.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      khryleption
      wrote on last edited by
      #2

      You could remove the flag "Qt::ItemIsSelectable" for examples:
      yourItem->setFlags(yourItem->flags&~Qt::ItemIsSelectable);

      1 Reply Last reply
      0
      • A Offline
        A Offline
        arsinte_andrei
        wrote on last edited by
        #3

        do you want those rows to be visible or to hide them? Do you want them selectable or not? (selectable without signal on them...)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          ankush205
          wrote on last edited by
          #4

          @arisinte I want them to be displayed ..yes selectable without signal on them

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ankush205
            wrote on last edited by
            #5

            khryleption i have implemted the class of Item myself

            @int TreeView::columnCount(const QModelIndex &parent) const
            {
            if (parent.isValid())
            return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
            else
            return rootItem->columnCount();
            }

            QVariant TreeView::data(const QModelIndex &index, int role) const
            {
            if (!index.isValid())
            return QVariant();

             if (role != Qt::DisplayRole)
                 return QVariant();
            
             TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
            
             return item->data(index.column());
            

            }

            Qt::ItemFlags TreeView::flags(const QModelIndex &index) const
            {
            if (!index.isValid())
            return 0;

             return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
            

            }

            QVariant TreeView::headerData(int section, Qt::Orientation orientation,
            int role) const
            {
            if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
            return rootItem->data(section);
            return QVariant();
            }

            QModelIndex TreeView::index(int row, int column, const QModelIndex &parent) const
            {
            if (!hasIndex(row, column, parent))
            return QModelIndex();

             TreeItem *parentItem;
            
             if (!parent.isValid())
                 parentItem = rootItem;
             else
                 parentItem = static_cast<TreeItem*>(parent.internalPointer());
            
             TreeItem *childItem = parentItem->child(row);
             if (childItem)
                 return createIndex(row, column, childItem);
             else
                 return QModelIndex();
            

            }

            QModelIndex TreeView::parent(const QModelIndex &index) const
            {
            if (!index.isValid())
            return QModelIndex();

             TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
             TreeItem *parentItem = childItem->parent();
            
             if (parentItem == rootItem)
                 return QModelIndex();
            
             return createIndex(parentItem->row(), 0, parentItem);
            

            }

            int TreeView::rowCount(const QModelIndex &parent) const
            {
            TreeItem *parentItem;
            if (parent.column() > 0)
            return 0;

             if (!parent.isValid())
                 parentItem = rootItem;
             else
                 parentItem = static_cast<TreeItem*>(parent.internalPointer());
            
             return parentItem->childCount();
            

            }@

            Howcan i implement that methd

            1 Reply Last reply
            0
            • A Offline
              A Offline
              arsinte_andrei
              wrote on last edited by
              #6

              ok what class does your item extend (implement)?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ankush205
                wrote on last edited by
                #7

                Sorry forgot to mention that it implements QAbstractItemModel
                and the code for treeitem is
                #include <QtCore/QStringList>

                @#include "treeitem.h"

                TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
                {
                parentItem = parent;
                itemData = data;
                }

                TreeItem::~TreeItem()
                {
                qDeleteAll(childItems);
                }

                void TreeItem::appendChild(TreeItem *item)
                {
                childItems.append(item);
                }

                TreeItem *TreeItem::child(int row)
                {
                return childItems.value(row);
                }

                int TreeItem::childCount() const
                {
                return childItems.count();
                }

                int TreeItem::columnCount() const
                {
                return itemData.count();
                }

                QVariant TreeItem::data(int column) const
                {
                return itemData.value(column);
                }

                TreeItem *TreeItem::parent()
                {
                return parentItem;
                }

                int TreeItem::row() const
                {
                if (parentItem)
                return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));

                 return 0;
                

                }@

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  arsinte_andrei
                  wrote on last edited by
                  #8

                  in Doc I see that Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex & index) const [virtual]
                  is virtual so you can reimplemented and set the flag acourdingly to your will for each item based on QModelIndex
                  have you tried this?

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    arsinte_andrei
                    wrote on last edited by
                    #9

                    from DOC @Items can be queried with flags() (see Qt::ItemFlag) to see if they can be selected, dragged, or manipulated in other ways.@
                    see more "here":http://qt-project.org/doc/qt-5/qabstractitemmodel.html#details

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      ankush205
                      wrote on last edited by
                      #10

                      If i update my flag method to
                      @Qt::ItemFlags TreeView::flags(const QModelIndex &index) const
                      {
                      if (!index.isValid())
                      return 0;

                      return ~Qt::ItemIsSelectable & ~Qt::ItemIsEnabled ;
                      }@

                      It doesnot allow me to form a tree bt i emits a signal when i press over it..

                      ~Qt::ItemIsSelectable & Qt::ItemisEnabled allows me to form tree and also emit signal so basically ItemisSelectable flag is not making any effect

                      If u see my code i have e implmented that method

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        khryleption
                        wrote on last edited by
                        #11

                        Maybe you could use the data (QAbstractItemModel::data with role = Qt::userRole) of an item, and when the slot is called check the data of the selected item and then return or continue your operation depending the item data.

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          ankush205
                          wrote on last edited by
                          #12

                          yeah Exactly is this what i Did and got Output Thank you

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            arsinte_andrei
                            wrote on last edited by
                            #13

                            What he mean is that when you you do the processing of the signal emitted in the slot you do nothing if the item data is not what you want. Also you can rewrite the function that emir that signal and emitted only when the data is as 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