Re: Drop on table and widget
Comment out lines until the crash goes away. That way you know where things are going wrong...
My drop event (to add single files) looks as follows:
Code:
if (event->mimeData()->hasFormat( "text/uri-list" )) {
QList<QUrl> urls = event->mimeData()->urls();
if (urls.isEmpty()) {
return;
}
if(urls.size()>1) {
return;
}
filePath = urls.first().toLocalFile();
if(filePath.isEmpty()) {
return;
}
}
// do something with filePath
}
Re: Drop on table and widget
you could use the model/view framework built in to qt to accept the dropped data into a model. you would have to subclass QStandardItemModel and override the drag/drop functions, then you just need to tell the view to use that model. here is some code modified from the Bangarang project...
Code:
bool MusicItemModel
::dropMimeData(const QMimeData *data, Qt
::DropAction action,
int row,
int column,
const QModelIndex &parent
) {
if (action == Qt::IgnoreAction)
return true;
if (!data->hasFormat("text/uri-list"))
return false;
if (column > 0)
return false;
int beginRow;
if (row != -1) {
beginRow = row;
} else if (parent.isValid()) {
beginRow = parent.row();
} else {
}
QList<QUrl> urls = data->urls();
bool internalMove = false;
if (data->text().startsWith("Row:")) {
rowsToMove
= data
->text
().
split(",",
QString::SkipEmptyParts);
internalMove = true;
}
QList<MusicItem> itemsInserted;
int insertionRow = beginRow;
for (int i = 0; i < urls.count(); i++) {
if (internalMove) {
QString rowEntry
= rowsToMove.
at(i
);
int rowToMove = rowEntry.remove("Row:").toInt();
MusicItem item = itemAt(rowToMove);
itemsInserted << item;
QList<QStandardItem *> rowItems = getRowData(item);
insertRow(insertionRow, rowItems);
insertionRow = insertionRow + 1;
} else {
QString url
= urls.
at(i
).
toString();
if (QFile::exists(url
)) { MusicItem item = Convenience::itemFromUrl(url);
itemsInserted << item;
QList<QStandardItem *> rowItems = getRowData(item);
insertRow(insertionRow, rowItems);
insertionRow = insertionRow + 1;
}
}
}
insertionRow = beginRow;
foreach (MusicItem item, itemsInserted) {
int i = rowFromItemUrl(item.Url);
m_musiclist.insert(insertionRow, item);
m_urllist.insert(insertionRow, item.Url);
if (i != -1){
m_musiclist.removeAt(i);
m_urllist.removeAt(i);
}
insertionRow = insertionRow + 1;
}
return true;
}
Qt
::ItemFlags MusicItemModel
::flags(const QModelIndex &index
) const{
Qt::ItemFlags defaultFlags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
if (index.isValid()) {
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
} else {
return Qt::ItemIsDropEnabled | defaultFlags;
}
}
QMimeData* MusicItemModel
::mimeData(const QModelIndexList
&indexes
) const {
QList<QUrl> urls;
if (index.isValid() && index.column() != 1) {
QUrl url
= QUrl(data
(index, MusicItem
::UrlRole).
toString());
urls << url;
indexList
+= QString("Row:%1,").
arg(index.
row());
}
}
mimeData->setUrls(urls);
mimeData->setText(indexList);
return mimeData;
}
{
types << "text/uri-list" << "text/plain";
return types;
}
Qt::DropActions MusicItemModel::supportedDropActions() const
{
return Qt::MoveAction;
}
hope it helps