owensss
owensss

Reputation: 121

Qt4 drop (xml)files from win explorer rejected by TableModel

I got a project that required drag & drop xml files from windows explorer into a TableModel, whatever I tried, that widget rejects(with the annoying block icon) the process and not any functions below are called.
i've tried the following:

  1. reimplement QAbstarctTableView::flags to support drag & drop
  2. remiplement QAbstractTableView::supportedDropActions.
  3. remiplement QAbstractTableView::mimeTypes. return "text/uri-list", "text/plain", "application/xml", "text/xml". (some said that "text/uri-list" shall be enough.)

according to most posts i found in the internet, I shall be able to drag files into the TableView Widget now. That's not true for me. T_T

here's my code.

// set the flags to accept drop & drag
Qt::ItemFlags XMLFileModel::flags(const QModelIndex& index) const {
    Qt::ItemFlags defaultFlags = Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;

    if (index.isValid())
        return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
    else
        return Qt::ItemIsDropEnabled | defaultFlags;
}

.

// drop mime data
bool XMLFileModel::dropMimeData(const QMimeData *data,
    Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
    qDebug() << "Drop Mime data" << endl;

    if (action == Qt::IgnoreAction)
        return true;

    if (! data->hasUrls())
        return false;

    QList<QUrl> urls = data->urls();
    foreach(QUrl url , urls) {
        std::shared_ptr<QFile> file(new QFile(url.path()));
        if (! file->open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QMessageBox::warning(NULL, QString("note"), QString("unable to open file!"));
            return false;
        }
        XMLFilePtr xml(new XMLFile(file->fileName(), file));
        addXMLFile(xml);
    }

    return true;
}

.

Qt::DropActions XMLFileModel::supportedDropActions() const
{
    qDebug () << "supportedDropActions";
    return Qt::CopyAction | Qt::MoveAction;
}

.

// define the acceptable mime type
QStringList XMLFileModel::mimeTypes() const
{
    qDebug () << "mimeTypes";
    QStringList types;
    types << "text/uri-list" << "text/plain" << "application/xml" << "text/xml";
    qDebug() << types;
    return types;
}

. I tried to add QMimeData* mimeData(const QModelIndexList &indexes) const; and now I could drag&drop inside the tableview or even between table views. debug messages print as well.

then I continued to do the test. I wrote a test class:

class test : public QTableView
{
    Q_OBJECT
public:
    explicit test(QWidget *parent = 0);
    virtual void dropEvent(QDropEvent *event);
    virtual void dragEnterEvent ( QDragEnterEvent * event );
    void startDrag ( Qt::DropActions supportedActions );    
};

.

void test::dropEvent(QDropEvent *event) {
    qDebug() << "test";
    qDebug() << event->mimeData()->formats();
    event->accept();
}

void test::dragEnterEvent(QDragEnterEvent *event) {
    qDebug() << "drag enter";
    qDebug() << event->mimeData();
    event->accept();
}

void test::startDrag(Qt::DropActions supportedActions) {
    qDebug() << "true";
}

. and add a new test widget in the mainWindow that accepts the same model.
It amazed me that drag&drop internally or between table views is still working. but when I tried to drop the item from my desktop, none of these three functions are called.

now I am thinking that the problem might be the incompatible mime-type between my OS and Qt D&D framework. I may not return the right mime type

any one suffered and solved this problem or any suggestions? >_<
= = = = = = = =
hey, I got the same problem with the drop site example, too!

Upvotes: 1

Views: 326

Answers (1)

Albert S
Albert S

Reputation: 85

I don't think you need to reimplement any of those functions except for dragEnterEvent and dropEvent. Did you call QWidget::setAcceptDrops(true)? This is important.

Upvotes: 0

Related Questions