Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// First, in the treeview i drop a directory (all files are show, line by line, like QFilseSystem do)...
// and then, i drop a file (who is a file contain inside the directroy i droped last)...
// i show two messageBox, the first for said it MATCH the target... by the index at column 4...
// the second For give the name of the file who is contain inside the matched index at column 4 (index returned by the search function)
// BUT... the second messageBox not show the correct content, but the first parent content at column 4
// WHY ?
// part of files_model.cpp
QModelIndex Files_Model::search(QString text,
int column,
QModelIndex parent) {
QMessageBox msgBox;
QModelIndex target_index, target_parent;
QString target_data;
for(int i; i < this->rowCount(parent); i++) {
target_index = this->index(i ,column , parent);
target_parent = this->index(i, 0, parent);
target_data = target_index.data().toString();
if(target_data == text) {
msgBox.setText(QString("M A T C H\ntarget:\n%1\ndata:\n%2").arg(text).arg(target_data));
msgBox.exec();
return target_index; }
if(this->hasChildren(parent)) {
search(text, column, target_parent);
} }
}
QString Files_Model::Check_doubles_for(const QFileInfo &file_info) {
QString file_find;
QMessageBox msgbox;
QString search_file = file_info.absoluteFilePath();
if (this->hasChildren()) {
QModelIndex idx_find = search(search_file, 4, this->index(-1,0));
if(idx_find.isValid()) {
file_find = idx_find.data().toString();
msgbox.setText(QString("Ce fichier existe déjà dans vôtre liste :\n%1")
.arg(file_find));
msgbox.exec();
}
}
return file_find;
}
bool Files_Model::dropMimeData(const QMimeData *data,
Qt::DropAction action,
int row, int column,
const QModelIndex &parent) {
QStringList formatList;
formatList = data->formats();
if (action == Qt::IgnoreAction)
return true;
if (!data->hasFormat("text/uri-list"))
return false;
if (column > 0) // interdit de coller ailleurs que dans la première colonne
return false;
if (parent.isValid()) // interdit de coller à l'intérieur d'un arbre
return false;
QList<QUrl> urlList;
QList<QStandardItem*> item;
urlList = data->urls(); // retrieve list of urls
foreach(QUrl url, urlList) { // pour chaque entrée d'une sélection multiple
QFileInfo file_info(url.toLocalFile());
QString find_double = Check_doubles_for(file_info);
if (!find_double.isEmpty()) // accès interdit aux doublons
return false;
.
.
.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment