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);
}

また、ドロップイベント:

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);
}

ドロップイベントが呼び出されたときに「ドロップイベント」も出力しました。これにより、次の出力が得られます。

...
[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 に変更されます (1 つの項目が消えます)。どこが問題なのか未だに掴めず…

Edit4: カスタム アイテムを使用していない場合でも、同じ動作をしています。何が問題なのかまだわかりません。

Edit5: [1] の例でさえ、モバイル デバイスでテストすると同じ動作をします。Qt のバージョンが問題になる可能性はありますか? Qt for Symbian Devices バージョン 4.6.3 を使用しています...

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

4

2 に答える 2

2

この動作の 2 つの理由を考えることができます: シグナル itemDoubleClicked が QListWidget のどこかで処理され、意図しないことを行うか、またはソースと宛先が同じ場合に dropEvent の「追加コード」が何か悪いことをします (startRow が等しいかどうかを確認できます)。この場合は何もしません)。

編集:

このプログラムはあなたのために働きますか:

#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();
}

星が 3 つの行は削除できます。このプログラムは、VS2010 でコンパイルされた Qt 4.7.1 を搭載した Windows XP で動作します。

于 2010-12-27T09:25:12.063 に答える