QListWidget drag&drop



  • Hi,
    i have 3 instances of object based on QListWidget (on picture boxes with gray borders). In one of this widgets are items with icons and a need move this items to other widgets, without copying. How to? Thanks!
    !http://mksoft.marconet.sk/pics/SOC-forum.jpg(Preview)!



  • New Year on the street! Wait with dev. It is whole 2012 year in front of you!



  • I'm from Slovakia and I had new year 14 hours earlier, and I need finish this project before end of January. I have a lot of work :)



  • I'm also tried this:
    @
    void ListBox::startDrag(Qt::DropActions supportedActions)
    {
    QListWidget::startDrag(supportedActions);
    delete this->currentItem();
    }
    @
    Now it works, but it has one problem, if is drop unsuccessfull, item is deleted too. I don't know how to solve this. How I can access to data of dragged QListWidgetItem?



  • I solved this, here is my code:
    @
    void ListBox::dragMoveEvent(QDragMoveEvent *e)
    {
    if (e->mimeData()->hasFormat("application/x-item") && e->source() != this) {
    e->setDropAction(Qt::MoveAction);
    e->accept();
    } else
    e->ignore();
    }

    void ListBox::dropEvent(QDropEvent *event)
    {
    if (event->mimeData()->hasFormat("application/x-item")) {
    event->accept();
    event->setDropAction(Qt::MoveAction);
    QListWidgetItem *item = new QListWidgetItem;
    QString name = event->mimeData()->data("application/x-item");
    item->setText(name);
    /if (provider->getColumnType(name) == "text") {
    item->setIcon(QIcon(":/images/iString"));
    }
    else {
    item->setIcon(QIcon(":/images/iInteger"));
    }
    /
    addItem(item);
    } else
    event->ignore();
    }

    void ListBox::startDrag(Qt::DropActions supportedActions)
    {
    QListWidgetItem *item = currentItem();
    QMimeData *mimeData = new QMimeData;
    QByteArray ba;
    ba = item->text().toLatin1().data();
    mimeData->setData("application/x-item", ba);
    QDrag *drag = new QDrag(this);
    drag->setMimeData(mimeData);
    if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
    delete takeItem(row(item));
    }

    void ListBox::dragEnterEvent(QDragEnterEvent *event)
    {
    if (event->mimeData()->hasFormat("application/x-item"))
    event->accept();
    else
    event->ignore();
    }

    Qt::DropAction ListBox::supportedDropActions()
    {
    return Qt::MoveAction;
    }
    @


Log in to reply
 

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