如果使用Qt :: DecorationRole,则拖放时崩溃

时间:2016-05-31 16:12:11

标签: c++ qt drag-and-drop model-view

以下实施工作正常:

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/debug/lib/x86_64-linux-gnu/libc-2.23.so 89 0x7ffff640201a
  2 __gnu_cxx :: __ verbose_terminate_handler()0x7ffff6a3984d
  3 ?? 0x7ffff6a376b6 4 std :: terminate()0x7ffff6a37701
  5 __cxa_throw 0x7ffff6a37919 6 qBadAlloc()0x7ffff6dba0e2
  7 QAbstractItemModel :: decodeData(int,int,QModelIndex const&amp;,   QDataStream&amp;)0x7ffff6f5bb28
  8 QAbstractListModel :: dropMimeData(QMimeData const *,Qt :: DropAction,   int,int,QModelIndex const&amp;)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

1 个答案:

答案 0 :(得分:0)

从Qt安装程序安装Qt 5.6.1后,拖放操作停止了程序崩溃。或许,来自Ubuntu存储库的Qt包中存在一个错误。