2016-05-31 268 views
0

以下實現正常工作的拖放和下降:崩潰而如果Qt的:: DecorationRole使用

ListModel::ListModel(QObject *parent) : QAbstractListModel(parent) {} 

QVariant ListModel::data(const QModelIndex &index, int role) const { 
    QVariant result; 
    if (index.row() > -1 && index.row() < items.size()) { 
     if (role == Qt::DisplayRole || role == Qt::EditRole) 
      result = items.at(index.row()); 
//  else if (role == Qt::DecorationRole) 
//   result = qApp->style()->standardIcon(QStyle::SP_DirIcon); 
    } 
    return result;} 

Qt::ItemFlags ListModel::flags(const QModelIndex &index) const { 
    Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled; 
    if (!index.isValid()) flags = flags | Qt::ItemIsDropEnabled; 
    return flags;} 

bool ListModel::insertRows(int row, int count, const QModelIndex &parent) { 
    if (count < 1 || row < 0 || row > rowCount(parent)) 
     return false; 
    beginInsertRows(QModelIndex(), row, row + count - 1); 
    for (int i = 0; i < count; ++i) 
     items.insert(row, QString()); 
    endInsertRows(); 
    return true;} 

bool ListModel::removeRows(int row, int count, const QModelIndex &parent) { 
    if (count < 1 || row < 0 || (row + count) > rowCount(parent)) 
     return false; 
    beginRemoveRows(QModelIndex(), row, row + count - 1); 
    for (int i = 0; i < count; ++i) 
     items.removeAt(row); 
    endRemoveRows(); 
    return true;} 

int ListModel::rowCount(const QModelIndex &parent) const { 
    if (parent.isValid()) return 0; 
    return items.count();} 

bool ListModel::setData(const QModelIndex &index, const QVariant &value, int role) { 
    const int indexRow = index.row(); 
    if (indexRow > -1 && indexRow < items.count()) { 
     if (role == Qt::DisplayRole || role == Qt::EditRole) { 
      items.replace(indexRow, value.toString()); 
      emit dataChanged(index, index, QVector<int>() << role); 
      return true;}} 
    return false;} 

Qt::DropActions ListModel::supportedDropActions() const { 
    return Qt::MoveAction;} 

,但只要這兩條線中的數據()是註釋掉的拖墜的項目開始崩潰該應用:

0 __GI_raise /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.23.so 54 0x7ffff6400418
1 __GI_abort/usr/lib中/調試/ LIB/x86_64-linux-gnu/libc-2.23.so 89 0x7ffff640201a
2 __gnu_cxx :: __ verbose_terminate _handler()0x7ffff6a3984d
3 ?? 0x7ffff6a376b6 4的std ::終止()0x7ffff6a37701
5 __cxa_throw 0x7ffff6a37919 6 qBadAlloc()0x7ffff6dba0e2
7化QAbstractItemModel :: decodeData(INT,INT,QModelIndex常量&, QDataStream &)0x7ffff6f5bb28
8 QAbstractListModel :: dropMimeData(QMimeData常量*,Qt的::的dropAction, INT,INT,QModelIndex常量&)0x7ffff6f5c8af
9 QAbstractItemView中:: dropEvent(QDropEvent *)0x7ffff7affc6c

這裏有一個小程序,玩:

int main(int argc, char *argv[]) { 
    QApplication app(argc, argv); 
    QMainWindow *mainWindow = new QMainWindow; 
    QListView *listView = new QListView; 
    ListModel *listModel = new ListModel; 
    listView->setSelectionMode(QAbstractItemView::ExtendedSelection); 
    listView->setDragDropMode(QAbstractItemView::InternalMove); 
    listModel->insertRows(0, 3); 
    listModel->setData(listModel->index(0), "alpha", Qt::DisplayRole); 
    listModel->setData(listModel->index(1), "beta", Qt::DisplayRole); 
    listModel->setData(listModel->index(2), "gamma", Qt::DisplayRole); 
    listView->setModel(listModel); 
    mainWindow->setCentralWidget(listView); 
    mainWindow->show(); 
    return app.exec();} 

的Kubuntu 16.04 64位的Qt 5.5.1

+0

看起來像一個bug – theorist

+0

是的,這是一個錯誤:https://bugreports.qt.io/browse/QTBUG-53750 – theorist

回答

0

我從Qt的安裝程序的拖墜停止崩潰的程序Qt安裝5.6.1之後。也許,Ubuntu存儲庫中的Qt包存在一個錯誤。