2011-03-06 212 views
0

要避免使用QStandardItemModel作为模型的QListView中的重复项的方法是什么?数据添加与拖动&下降,所以我试图覆盖QStandardItemModel :: dropMimeData,这似乎有点奇怪,因为我需要重写QStandardItemModel :: mimeData(和重新编码数据/ decodeData)以及。这必须更容易!QListView&QStandardItemModel - 防止重复

回答

0

好吧,我设法通过重写而QListView :: dataChanged,检查是否有与在模型中的Qt :: DisplayRole相同数据的多个项目下降和删除后,一个解决这个如果有的话。基本上,它看起来像这样:

void MyListView::dataChanged(QModelIndex topLeft, QModelIndex bottomRight) 
{ 
    // there can be only one item dragged at once in my use case 
    if(topLeft == bottomRight) 
    { 
     QStandardItemModel* m = static_cast<QStandardItemModel*>(model()); 
     // if theres already another item with the same DisplayRole... 
     if(m->findItems(topLeft.data().toString()).count() > 1) 
     { 
      // ... we get rid of it. 
      model()->removeRow(topLeft.row()); 
     } 
    } 
    else 
    { 
     // let QListView decide 
     QListView::dataChanged(topLeft, bottomRight); 
    } 
} 

这是迄今为止并不完美(例如,如果你可以一次降一个以上的项目),但它的工作原理对于简单的用例。