QListView scrolls up on list item drop.



  • Hi guys.

    I read a couple of notes about enabling items reordering in the QListView. I reimplemented flags() and supportedDropActions() methods and it seems to be working. I can take an item from the list and drop it in another position and it moves their correctly except the listview itself scrolls to the very top as soon as I drop the item and the wrong item get's selected. Why doesn't it remember where it was before the drop? What I can do to solve this? And also where I should specify which item must be selected?

    Thanks for any help!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    As for your problem, can you provide a minimal sample that reproduce the behavior ?

    Also, what version of Qt are you using ? On which OS ?



  • It's Qt 5.2.0 on Windows 7. I'm using Ct Creator.
    What i did is:
    1) Added a List View using the disigner
    2) Then I set it properties:
    dragEnabled - true
    dragDropOverWriteMode - false
    dragDropOverMode - InternalMove
    defaultDropAction - MoveAction
    Scroll Modes - perItem
    movement - free or snap;

    *3) *I created a new class named PlayListModel inherited from QStingListModel
    @class PlayListModel : public QStringListModel
    {
    Q_OBJECT

    public:
    PlayListModel(QObject *parent) : QStringListModel(parent)
    {

    }
    Qt::ItemFlags flags(const QModelIndex &index) const;
    Qt::DropActions supportedDropActions() const    {        return Qt::CopyAction | Qt::MoveAction;    }
    int rowCount(const QModelIndex &parent) const;
    

    };

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

    int PlayListModel::rowCount(const QModelIndex &parent) const
    {
    if(parent.isValid())
    {
    return 0;
    }
    return stringList().length();
    }@

    *4) *In my mainwindow function I populate my model class with items
    @for(int i = 0; i < 25; i++)
    {
    QModelIndex index = playListsModel->index(i);
    playListsModel->setData(index, "Item"+QString::number(i+1));
    }@

    and then assign it to List View's model.

    @ui->listsView->setModel(playListsModel); @

    Run the program and get such a behaviour.



  • Interesting. It happens only when the list widget or list view are created in the designer. If I create it in code and just
    @setDragDropMode(QAbstractItemView::InternalMove);@
    then it works as I want it to... hmm.

    Where's the bug?
    Is it the compiler or ide problem?? Or just some parameter I missed somewhere in the designer or maybe the defaults in the designer differ from those in the framework itself?

    I still can't make it work via the designer.


  • Lifetime Qt Champion

    Sounds strange indeed

    Can you compare the values of these properties for both widgets ?



  • The same.... Actually I found out that it has nothing to do with the designer and only happens to QListView. I added both QListWidget and QListView to the layout. Added several items to QListWidget and then set the QListView's model to QListWidget::model();
    Then I set both's
    @plistWidget->setDragDropMode(QAbstractItemView::InternalMove);
    plistView->setDragDropMode(QAbstractItemView::InternalMove);@
    and what I get is that QListWidget works properly (it does'n scroll anywhere, and the moved item stays selected) but QListView doesn't behave the same way (it scrolls to top and selects eithe the index item used to be at or the one right next to where it was depending on moving the item up or down the list).
    Seem like I just don't know something about it but I can't find the answer to this.


  • Lifetime Qt Champion

    QListWidget reimplements the dropEvent method so it might be that.

    Did you have a look at the "bug report system":http://bugreports.qt-project.org ? Somebody might already have posted something about it, if not you could consider open a new bug report providing a minimal compilable example that show the behavior


  • Lifetime Qt Champion


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.