Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QListWidget Drag after scrolling down the list

I am creating A Sortable List in Qt. The Code works perfectly well for Downward Scroll but when i having some issues getting the draggable item after i scroll the list down. I have added some test case screenshot for better understanding

Screen1 Screen2 Screen3 Screen4

Well this is the test case code

    #include <QtGui>

   int main(int argc, char **argv) 
   {
      QApplication app(argc, argv);
      QListWidget *listWidget = new QListWidget;
      for(int i=0;i<100;++i){
        listWidget->addItem("SongOne");
        listWidget->addItem("SongTwo");
        listWidget->addItem("SongThree");
        listWidget->addItem("SongFour");
        listWidget->addItem("SongFive");
      }
      listWidget->setDragDropMode(QAbstractItemView::InternalMove);
      listWidget->setDragEnabled(true);
      listWidget->setAcceptDrops(true);
      listWidget->setDropIndicatorShown(true);
      listWidget->viewport()->setAcceptDrops(true);
      listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
      listWidget->show();
      app.exec();
      delete listWidget;
      return 0;
    }

Thankz for taking the time in reading my post. Do help me if you have any hint on what i am missing out.I think i am missing setting some property. In the main Program(not the test code), i tried rewriting the dragMoveEvent and few more method, but no use.

the problem here is not the drag and drop but the QRect that is created while doing Drag and Drop. In Figure Two the Qrect is Created but not in Figure Four

like image 710
sobingt Avatar asked Oct 04 '12 08:10

sobingt


2 Answers

Try to remove the lines:

listWidget->setDragEnabled(true);
listWidget->setAcceptDrops(true);
listWidget->setDropIndicatorShown(true);
listWidget->viewport()->setAcceptDrops(true);

They may interfer with the "real" Drag'n'Drop system. Reordering is typically handled in an optimized way.

Another solution could be that you subclass QListWidget and get the element's index in the drag event and the other's element's index in the drop event. So you know what to reorder and where it should be placed (before or after the second element's index). The rest should be easy, just takeItem() and insertItem().

Hope I helped you with these two ways ;)

like image 101
Alexander Nassian Avatar answered Oct 17 '22 16:10

Alexander Nassian


In win7, i was not able to reproduce the same behavior. It just works(even size hint is tweaked to meet your screen shot requirement. :) ) My test code was...

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QListWidget *listWidget = new QListWidget;
    const char* titles[] =  {
        "SongOne%1",
        "SongTwo%1",
        "SongThree%1",
        "SongFour%1",
        "SongFive%1",
    };
    for(int i=0;i<100;++i){
        QString title = QString(titles[i%5]).arg(i);
        QListWidgetItem* item = new QListWidgetItem(title);
        item->setData(Qt::SizeHintRole, QSize(50,100));
        listWidget->addItem(item);
    }
    listWidget->setDragDropMode(QAbstractItemView::InternalMove);
    listWidget->setDragEnabled(true);
    listWidget->setAcceptDrops(true);
    listWidget->setDropIndicatorShown(true);
    listWidget->viewport()->setAcceptDrops(true);
    listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    listWidget->show();
    app.exec();
    delete listWidget;
    return 0;
}
like image 1
Joonhwan Avatar answered Oct 17 '22 16:10

Joonhwan