c – Qt从QTableWidget拖放到文件夹

我有自定义QTableWidget,我怎样才能将Drag& Drop从表索引转移到文件夹?我有QList集合和归档文件.

此代码非常适合应用程序

void ArchiveQTableWidget::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Return && currentRow() > -1)
        enterClicked(currentIndex());
    QTableWidget::keyPressEvent(event);
}

void ArchiveQTableWidget::dragEnterEvent(QDragEnterEvent *event)
{
    if (event->mimeData()->hasUrls()) {
        event->acceptProposedAction();
    }
}

void ArchiveQTableWidget::dragMoveEvent(QDragMoveEvent *event)
{
    event->acceptProposedAction();
}

void ArchiveQTableWidget::dragLeaveEvent(QDragLeaveEvent *event)
{
    event->accept();
}

void ArchiveQTableWidget::dropEvent(QDropEvent *event)
{
    foreach (const QUrl &url, event->mimeData()->urls()) {
        QString fileName = url.toLocalFile();
        qDebug() << "Dropped file:" << fileName;
    }
}

void ArchiveQTableWidget::startDrag(Qt::DropActions supportedActions)
{
    StreamQMimeData mimeData = new StreamQMimeData;
    connect(mimeData, SIGNAL(dataRequested(QString)),
            this, SLOT(createData(QString)), Qt::DirectConnection);
    QDrag *drag = new QDrag(this);
    drag->setMimeData(mimeData);
    drag->exec(Qt::CopyAction);
}

最佳答案 您需要调用QMimeData :: setUrls(QList< QUrl>).

点赞