Disable certain Rows in QTreeView



  • 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.



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



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



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



  • 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



  • ok what class does your item extend (implement)?



  • 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;
    

    }@



  • 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?



  • 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



  • 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



  • 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.



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



  • 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


Log in to reply
 

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