【问题标题】:QListview Drag and Drop SlotQListview 拖放槽
【发布时间】:2014-11-19 18:33:08
【问题描述】:

我的 MainWindow 中有一个 QListView 并启用了拖放功能。为此,我想创建一个 Slot 女巫正在监听拖放事件。但是在 QT 文档中我没有找到这个事件的信号。如何创建 Slot 或以某种方式监听拖放事件?

编辑:我只想在 ListView 中使用拖放来重新排序项目,然后只听这个事件。

【问题讨论】:

标签: qt qlistview


【解决方案1】:

拖放的实现需要在所有支持它的小部件中实现拖放操作。

要实现拖动操作,最好的方法是在应该能够拖动的小部件中使用鼠标事件处理程序的重载:

void DragWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
        dragStartPosition = event->pos();
}

void DragWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (!(event->buttons() & Qt::LeftButton))
        return;
    if ((event->pos() - dragStartPosition).manhattanLength()
         < QApplication::startDragDistance())
        return;

    QDrag *drag = new QDrag(this);
    QMimeData *mimeData = new QMimeData;

    mimeData->setData(mimeType, data);
    drag->setMimeData(mimeData);

    Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);
    ...
}

为了能够接收 Drop 事件,需要使用 setAcceptDrops(true) 标记小部件并重载 dragEnterEvent() 和 dropEvent() 事件处理函数。例如:

Window::Window(QWidget *parent)
    : QWidget(parent)
{
    ...
    setAcceptDrops(true);
}

void Window::dropEvent(QDropEvent *event)
{
    textBrowser->setPlainText(event->mimeData()->text());
    mimeTypeCombo->clear();
    mimeTypeCombo->addItems(event->mimeData()->formats());

    event->acceptProposedAction();
}

您可以找到此here 的完整文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多