PDA

View Full Version : Drag and drop inside and outside a Widget



utopia500
3rd May 2010, 13:42
Hello Everybody,

I have written a program that permit drag and drop in a TreeView.
I have written a model like one written in internet



class ModelBookNodeDescModel: public QAbstractItemModel
{
public slots:
void saveToXml();
public:
ModelBookNodeDescModel(std::string full_path, QObject *parent = 0);
~ModelBookNodeDescModel();

void setRootNode(ut::types::tModelBookNodeDesc *node);
QModelIndex exportRootIndex();
static void setParentOfAllNodes(ut::types::tModelBookNodeDesc *node);

QModelIndex createQModelIndex(ut::types::tModelBookNodeDesc &item);

QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &child) const;

int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;

QVariant data(const QModelIndex &index, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;

bool insertRows(int position, int rows, std::vector<ut::types::tModelBookNodeDesc*> &tab ,const QModelIndex &parent = QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &parent = QModelIndex());
bool setData(const QModelIndex &index, const ut::types::_tModelDesc &value,
int role = Qt::EditRole);


static bool parentHasChild(ut::types::tModelBookNodeDesc *&parent,
ut::types::tModelBookNodeDesc *&resultParent,
ut::types::tModelBookNodeDesc *&child,
int& recurse);


void addChild(ut::types::tModelBookNodeDesc* parent, ut::types::tModelBookNodeDesc child);
void deleteItem(ut::types::tModelBookNodeDesc *item);
void insertParent(ut::types::tModelBookNodeDesc *child, ut::types::tModelBookNodeDesc parent);
void setSelectedItem(ut::types::tModelBookNodeDesc *item);

protected:
ut::types::tModelBookNodeDesc *nodeFromIndex(const QModelIndex &index) const;
ut::types::tModelBookNodeDesc *rootNode;
ut::types::tModelBookNodeDesc *parentRootNode;
ut::types::tModelBookNodeDesc *grandParentRootNode;


public:
mutable QTreeView* m_view;

ut::types::tModelBookNodeDesc *selectedItem;

};



class DragDropModel : public ModelBookNodeDescModel
{
Q_OBJECT

public:
DragDropModel(const QString &full_path, QObject *parent = 0);

Qt::ItemFlags flags(const QModelIndex &index) const;

bool dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &parent);
QMimeData *mimeData(const QModelIndexList &indexes) const;
QStringList mimeTypes() const;
Qt::DropActions supportedDropActions() const;
void removeSelectedIndex();

public slots:
void markIndex(const QModelIndex& index);


protected:
QModelIndex *selectedIndex;
mutable std::vector<ut::types::tModelBookNodeDesc *> nodesToDelete;
mutable ut::types::tModelBookNodeDesc *newParent;

};



with drag and drop inside the tree view that uses this model there is no problem.

the problem begins when I want to drag an object from outside the view into an element from the view.

the following code replaces the QtreeView witch receive the model



class DragableDoubleTreeView : public QTreeView
{
Q_OBJECT
public:
DragableDoubleTreeView(QWidget *parent = 0);
void setDoubleTreeView(DoubleTreeView* view);

public slots:
void setSelected(const QModelIndex &index);

protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);

DoubleTreeView *m_doubleTreeView;

private:
void startDrag();
QModelIndex *m_modelIndex;



QPoint startPos;
};


this portion of code interact negatively with the preceding drag and drop model and it does permit to had an element
to the list but not an element into a parent element of the list.



if someone has a solution it would thanks him gratefully

Best Regards.
Utopia500