Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, November 2007
Custom mimeData in drag and drop using model/view classes


Message 1 in thread

Hi all.
	I've got a problem for which I need clarification. I've got two models 
displayed by a ListView and a TableView, respectively, and I'd like to 
drag elements from the listview into cell's in the tableview. Here's 
some abbreviated code of what I've got so far:

//FileItem
class FileItem; //A regular non-qt class

class FileModel : public QAbstractListModel {...};

// The flags method for the FileModel:
Qt::ItemFlags FileModel::flags(const QModelIndex &index) const {
	if (!index.isValid())
		return Qt::ItemIsEnabled;
	return QAbstractListModel::flags(index) | Qt::ItemIsDragEnabled;
}

//The table
class MyTableItem; //Also a regular non-qt class

class MyTableModel : public QAbstractTableModel{...};


The setup of the views:

	//Setup FileList
	QListView *listView = QListView(this)
	listModel *fileModel = new FileModel();
	listView->setModel(fileModel);
	listView->setSelectionMode(QAbstractItemView::SingleSelection);
	listView->setDragEnabled(true);

	//Setup tableView
	QTableView *tableView = new QTableView(this);
	MyTableModel *tableModel = new MyTableModel();
	tableView->setModel(m);
	tableView->setSelectionMode(QAbstractItemView::SingleSelection);
	tableView->setDragEnabled(true);
	tableView->setAcceptDrops(true);
	tableView->setDropIndicatorShown(true);

With this code it's possible to drag and drop items from the listView to 
the tableView. A mimeData object, with format 
"application/x-qabstractitemmodeldatalist",  is even recieved in

MyTableModel::dropMimeData(...);

So far, so good. Now I'd like to add some custom data to the mimeData, 
so I extend the model for the listView with

QStringList FileModel::mimeTypes() const{
	QStringList types;
	types<<"application/x-myowncustomdata";
	return types;
}

and

QMimeData *FileModel::mimeData(const QModelIndexList &indexes) const{
	QByteArray encodedData;
	... encode the data ...
	mimeData->setData("application/x-myowncustomdata", encodedData);
	return mimeData;
}

but now the tableView will not accept the dragged/dropped items. (The 
mouse-pointer turns into a crossed out circle.) It does not matter what 
mimeType I use, it even fails on "text/plain" data.

Have I missed a crucial point in the documentation or to set some 
required properties in the views, mimeData or some place else?

The code has been tested on Fedora7 using qt4.3.2-1.x86_64 and windows 
XP using Qt4.1.2.

/Allan

--
 [ signature omitted ] 

Message 2 in thread

Can you post your dragEnterEvent and dropEvent methods for your table 
view? My initial guess would be that you aren't accepting the mime data 
format you created ("application/x-myowncustomdata").

chris burke
phone: 617-621-0060 x195
email: cburke@xxxxxxx
skype: chris.burke0



Allan Rasmusson wrote:
> Hi all.
>     I've got a problem for which I need clarification. I've got two 
> models displayed by a ListView and a TableView, respectively, and I'd 
> like to drag elements from the listview into cell's in the tableview. 
> Here's some abbreviated code of what I've got so far:
>
> //FileItem
> class FileItem; //A regular non-qt class
>
> class FileModel : public QAbstractListModel {...};
>
> // The flags method for the FileModel:
> Qt::ItemFlags FileModel::flags(const QModelIndex &index) const {
>     if (!index.isValid())
>         return Qt::ItemIsEnabled;
>     return QAbstractListModel::flags(index) | Qt::ItemIsDragEnabled;
> }
>
> //The table
> class MyTableItem; //Also a regular non-qt class
>
> class MyTableModel : public QAbstractTableModel{...};
>
>
> The setup of the views:
>
>     //Setup FileList
>     QListView *listView = QListView(this)
>     listModel *fileModel = new FileModel();
>     listView->setModel(fileModel);
>     listView->setSelectionMode(QAbstractItemView::SingleSelection);
>     listView->setDragEnabled(true);
>
>     //Setup tableView
>     QTableView *tableView = new QTableView(this);
>     MyTableModel *tableModel = new MyTableModel();
>     tableView->setModel(m);
>     tableView->setSelectionMode(QAbstractItemView::SingleSelection);
>     tableView->setDragEnabled(true);
>     tableView->setAcceptDrops(true);
>     tableView->setDropIndicatorShown(true);
>
> With this code it's possible to drag and drop items from the listView 
> to the tableView. A mimeData object, with format 
> "application/x-qabstractitemmodeldatalist",  is even recieved in
>
> MyTableModel::dropMimeData(...);
>
> So far, so good. Now I'd like to add some custom data to the mimeData, 
> so I extend the model for the listView with
>
> QStringList FileModel::mimeTypes() const{
>     QStringList types;
>     types<<"application/x-myowncustomdata";
>     return types;
> }
>
> and
>
> QMimeData *FileModel::mimeData(const QModelIndexList &indexes) const{
>     QByteArray encodedData;
>     ... encode the data ...
>     mimeData->setData("application/x-myowncustomdata", encodedData);
>     return mimeData;
> }
>
> but now the tableView will not accept the dragged/dropped items. (The 
> mouse-pointer turns into a crossed out circle.) It does not matter 
> what mimeType I use, it even fails on "text/plain" data.
>
> Have I missed a crucial point in the documentation or to set some 
> required properties in the views, mimeData or some place else?
>
> The code has been tested on Fedora7 using qt4.3.2-1.x86_64 and windows 
> XP using Qt4.1.2.
>
> /Allan
>
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ] 

Message 3 in thread

Chris Burke wrote:
> Can you post your dragEnterEvent and dropEvent methods for your table 
> view? My initial guess would be that you aren't accepting the mime 
> data format you created ("application/x-myowncustomdata").
>
> chris burke
> phone: 617-621-0060 x195
> email: cburke@xxxxxxx
> skype: chris.burke0
>
>
Ah... it makes sense now. What I've been using is the QTableView itself; 
I haven't specialized it. The reason was that the functionality of the 
QTableView is exactly what I want, just with a custom mimetype for drag 
and drop. I figured QTableView accepts ANY mime-type and that people 
limit this through specialization. Apparently it works the other way around.

Since my initial post I've specialized the QTableView in order to make 
the dragEnterEvent, dropEvent and also the dragMoveEvent accept the 
mimeType explicitly. Now it's working besides I seem to have to 
implement the indication of drop position identical to one in QTableView.

/Allan

--
 [ signature omitted ] 

Message 4 in thread

On torsdag den 1. November 2007, Allan Rasmusson wrote:
> Hi all.
> 	I've got a problem for which I need clarification. I've got two models
> displayed by a ListView and a TableView, respectively, and I'd like to
> drag elements from the listview into cell's in the tableview.

Another way to solve this is to add your custom data to be something you can 
put inside a QVariant, and implement operator << and >> for QDataStream, and 
then I think you should be able to use the existing drag and drop code.

I don't know if this trick really works, but I can't see why it shouldn't. You 
might need to do a bit more research to figure out how the drag encoding and 
decoding of the variants in the views before it works.

Bo.

> Here's 
> some abbreviated code of what I've got so far:
>
> //FileItem
> class FileItem; //A regular non-qt class
>
> class FileModel : public QAbstractListModel {...};
>
> // The flags method for the FileModel:
> Qt::ItemFlags FileModel::flags(const QModelIndex &index) const {
> 	if (!index.isValid())
> 		return Qt::ItemIsEnabled;
> 	return QAbstractListModel::flags(index) | Qt::ItemIsDragEnabled;
> }
>
> //The table
> class MyTableItem; //Also a regular non-qt class
>
> class MyTableModel : public QAbstractTableModel{...};
>
>
> The setup of the views:
>
> 	//Setup FileList
> 	QListView *listView = QListView(this)
> 	listModel *fileModel = new FileModel();
> 	listView->setModel(fileModel);
> 	listView->setSelectionMode(QAbstractItemView::SingleSelection);
> 	listView->setDragEnabled(true);
>
> 	//Setup tableView
> 	QTableView *tableView = new QTableView(this);
> 	MyTableModel *tableModel = new MyTableModel();
> 	tableView->setModel(m);
> 	tableView->setSelectionMode(QAbstractItemView::SingleSelection);
> 	tableView->setDragEnabled(true);
> 	tableView->setAcceptDrops(true);
> 	tableView->setDropIndicatorShown(true);
>
> With this code it's possible to drag and drop items from the listView to
> the tableView. A mimeData object, with format
> "application/x-qabstractitemmodeldatalist",  is even recieved in
>
> MyTableModel::dropMimeData(...);
>
> So far, so good. Now I'd like to add some custom data to the mimeData,
> so I extend the model for the listView with
>
> QStringList FileModel::mimeTypes() const{
> 	QStringList types;
> 	types<<"application/x-myowncustomdata";
> 	return types;
> }
>
> and
>
> QMimeData *FileModel::mimeData(const QModelIndexList &indexes) const{
> 	QByteArray encodedData;
> 	... encode the data ...
> 	mimeData->setData("application/x-myowncustomdata", encodedData);
> 	return mimeData;
> }
>
> but now the tableView will not accept the dragged/dropped items. (The
> mouse-pointer turns into a crossed out circle.) It does not matter what
> mimeType I use, it even fails on "text/plain" data.
>
> Have I missed a crucial point in the documentation or to set some
> required properties in the views, mimeData or some place else?
>
> The code has been tested on Fedora7 using qt4.3.2-1.x86_64 and windows
> XP using Qt4.1.2.
>
> /Allan
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/



-- 
 [ signature omitted ]