2

我在使用可以通过拖放重新排序的自定义项目实现 QListWidget 时遇到问题。问题是当我在一个项目上快速双击(非常短的拖放)时,该项目有时会从 QListWidget 中消失。

这是我的小部件的构造函数:

ListPopisiDragDrop::ListPopisiDragDrop(QWidget *parent) :
    QListWidget(parent)
{
    setSelectionMode(QAbstractItemView::SingleSelection);
    setDragEnabled(true);
    viewport()->setAcceptDrops(true);
    setDefaultDropAction(Qt::MoveAction);
    setDropIndicatorShown(true);
    setDragDropMode(QAbstractItemView::InternalMove);
}

还有 drop 事件:

void ListPopisiDragDrop::dropEvent(QDropEvent *event){

    int startRow=currentIndex().row();

    QListWidget::dropEvent(event);

    int endRow=currentIndex().row();

    //more code...
}

自定义项目是通过实现 QAbstractItemDelegate 中的 paint() 和 sizeHint() 函数来制作的。

当项目消失的问题发生时,甚至不会调用 dropEvent。

我真的不知道发生了什么以及我是否做错了什么。任何帮助表示赞赏。

谢谢!

编辑:我在 Symbian S60 第 5 版手机上运行该应用程序。

Edit2:如果我将此行添加到构造函数:

setDragDropOverwriteMode(true);

列表中的项目仍然消失,但空行留在原处。

Edit3:我添加了这段代码来看看发生了什么:

bool ListPopisiDragDrop::event(QEvent *e){
    qDebug()<<"new event, type: "<<e->type()<<", listCount: "<<this->count();

    QListWidget::event(e);
}

当调用 drop 事件时,我还打印了“drop event”。这给了我以下输出:

...
[Qt Message] new event, type:  12 , listCount:  2 
[Qt Message] new event, type:  12 , listCount:  2 
[Qt Message] new event, type:  68 , listCount:  2 
[Qt Message] DROPEVENT 
[Qt Message] new event, type:  71 , listCount:  2 
[Qt Message] new event, type:  12 , listCount:  2 
[Qt Message] new event, type:  12 , listCount:  2 
[Qt Message] new event, type:  68 , listCount:  2 
[Qt Message] DROPEVENT 
[Qt Message] new event, type:  71 , listCount:  2 
[Qt Message] new event, type:  12 , listCount:  2 
[Qt Message] new event, type:  12 , listCount:  2 
[Qt Message] new event, type:  12 , listCount:  2 
[Qt Message] new event, type:  68 , listCount:  2 
[Qt Message] new event, type:  12 , listCount:  1 
[Qt Message] new event, type:  12 , listCount:  1 
[Qt Message] new event, type:  1 , listCount:  1
...

如您所见,在事件类型 68 之后,listCount 从 2 变为 1(一项消失)。我仍然没有找到问题所在......

Edit4:即使我不使用自定义项目,我也有相同的行为。仍然无法弄清楚出了什么问题。

Edit5:即使 [1] 中的示例在移动设备上进行测试时也具有相同的行为。Qt版本会不会有问题?我正在为 Symbian 设备版本 4.6.3 使用 Qt ...

[1]http://www.java2s.com/Code/Cpp/Qt/QListWidgetdraganddrop.htm

4

2 回答 2

2

我可以想到这种行为的两个原因:信号 itemDoubleClicked 在您的 QListWidget 中的某处处理并做了一些意外,或者当源和目标相同时,您在 dropEvent 中的“更多代码”会做一些不好的事情(您可以检查 startRow 是否相等在这种情况下 endRow 并且什么都不做)。

编辑:

该程序是否适合您:

#include <QApplication>
#include <QListWidget>

int main(int argc, char **argv)
{
    QApplication a(argc, argv);

    QListWidget lw;

    for(int i = 1; i < 10; ++i)
        lw.addItem(new QListWidgetItem(QString("Item %1").arg(i)));
    lw.setDragEnabled(true); // ***
    lw.viewport()->setAcceptDrops(true); // ***
    lw.setDefaultDropAction(Qt::MoveAction); // ***
    lw.setDropIndicatorShown(true); // ***

    lw.setDragDropMode(QAbstractItemView::InternalMove);

    lw.show();

    a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    a.exec();
}

可以删除带有三颗星的行。这个程序适用于我在 Windows XP 上使用 Qt 4.7.1,用 VS2010 编译。

于 2010-12-27T09:25:12.063 回答
1

曾经在桌面上遇到过同样的问题,SelectionMode、InternalMove 等完全如图所示。也有我自己的视图模型,所以我只是让它以这种方式返回:

Qt::ItemFlags MyModel::flags(const QModelIndex& index) const
{
    if (index.isValid())
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
}

和我一起工作得很好。

于 2015-06-08T11:03:20.007 回答