【问题标题】:QListWidget drag and drop items disappearing from list on SymbianQListWidget拖放项目从Symbian列表中消失
【发布时间】:2010-12-27 03:23:26
【问题描述】:

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

这是我的 Widget 的构造函数:

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 版手机上运行该应用程序。

编辑2: 如果我将此行添加到构造函数:

setDragDropOverwriteMode(true);

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

编辑3: 我添加了这段代码来看看发生了什么:

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(一项消失)。我仍然没有找到问题所在......

编辑4: 即使我不使用自定义项目,我也有相同的行为。还是不知道怎么回事。

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

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

【问题讨论】:

  • type 68 = ChildAdded, 71 = ChildRemoved, 12 = Paint, 1 = Timer

标签: qt drag-and-drop symbian qlistwidget


【解决方案1】:

我可以想到这种行为的两个原因:信号 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 编译。

【讨论】:

  • 感谢您的回答。我正在处理 itemDoubleClicked 信号,但我不会在任何地方删除任何内容。此外,问题发生时不会调用 itemDoubleClicked,也不会触发 dropEvent。如果 startRow 和 endRow 相等,我什至什么都不做......我会在一分钟内用更多信息编辑我的问题。
  • 是的,这个程序在电脑上运行良好,我的也一样,但是在手机上运行时它们都有这种奇怪的行为。我会尝试更新到 Qt 4.7.1
  • 你没有提到这个问题只是在手机上。也许您应该检查 Qt 错误跟踪器?
【解决方案2】:

曾经在桌面上遇到过同样的问题,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;
}

和我合作得很好。

【讨论】:

    猜你喜欢
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    相关资源
    最近更新 更多