PDA

View Full Version : Drag&Drop with QStandardItemModel



madcat
6th May 2006, 18:57
Hi,
I have a QListView, which is linked to a QStandardItemModel. I'd like to be able to drop files on that QListView. The problem is, the supportedDropActions method is not supported by this object. This is quite strange, since it inherits QAbstractItemModel which provides this function. Is there a way to do drag & drop with QStandardItemModel ?
Thanks in advance

wysota
6th May 2006, 20:03
Subclass QStandardItemModel and reimplement the desired method. BTW. "supportedDropActions" returns Qt::CopyAction by default. So you should be able to drop data on it. Maybe you didn't enable dropping in the view?

madcat
6th May 2006, 21:13
Hi. Thanks for your answer.
Actually, that's more the supportedDropActions which is intersting me, if I subclass QStandardItemModel, what should be the constructor ? Is this correct ? (I'm usually a C coder) :


#include <QtGui>

class PeerListModel : public QStandardItemModel {
Q_OBJECT
public:
PeerListModel (QWidget *parent = 0);
};

PeerListModel::PeerListModel (QWidget *parent = 0) {
QStandardItemModel(this);
}

jacek
6th May 2006, 21:41
Is this correct ?
Not quite, it should be:
#include <QStandardItemModel>

class PeerListModel : public QStandardItemModel
{
Q_OBJECT
public:
PeerListModel( QObject *parent = 0 );
};

PeerListModel::PeerListModel( QObject *parent ) : QStandardItemModel( parent )
{
}
Remember that if the constructor's implementation is in a header file, you should add "inline" keyword in front of it.

madcat
7th May 2006, 15:43
Hi,
actually it is not in the header file, I just pasted both files merged.
Anyway, your solution works just great
Once again, thank you a lot for your help :)