PDA

View Full Version : QTreeView and d&d



FuturePrimitive
10th November 2011, 09:42
Hello

Im facing a little problem regarding QTreeView and drag&drop.

I want to restrict drop only in between the items. I found a good working suggestion on forum so this was not a big deal.

The problem is that i want to restrict items being dropped into different columns, lets say i have file, size and date column and i dont want the file item end up in the date or size column.

right now im using this inherited model class based on the suggestion i mentioned before.



class Model : public QStandardItemModel
{
public:
Model(int rows, int cols, QObject* parent = 0) : QStandardItemModel(rows, cols, parent)
{
}

Qt::ItemFlags flags(const QModelIndex& inIndex) const
{

if(!inIndex.isValid() )
return Qt::ItemIsDropEnabled;


return Qt::ItemIsDragEnabled | Qt::ItemIsSelectable | Qt::ItemIsEnabled ;
}
};


Is there a way to do this without completely new customized QTreeView?

Or maybe is there better component for this particular purpose than QTreeView?



thanks a lot!

FuturePrimitive
10th November 2011, 20:27
Ok i sorted it out and in case someone is facing the same problem.

i used the code in previous post for the model of treeview and made a sub-class of QTreeView with this dropEvent:



void CTreeView::dropEvent(QDropEvent *event)
{
if(event->pos().x() > this->columnWidth(0))
event->setPoint(QPoint(this->columnWidth(0)/2,event->pos().y()));

QTreeView::dropEvent(event);
}


this is restriction to first column and any attempst to drop into other columns will be sent back to first column

I hope this helps to someone :)