mercredi 28 octobre 2020

How to drag and drop a specific QPixmap into a QGraphicsView

On a subclassed QListWidget I have several items. Every QListWidget item (e.g. "ROS Init", "Images" etc) that is shown below is associated with a specific icon.

The problem I have is that I am trying to drag and drop the specific icon corresponding to that QListWidget item, but nothing happens.

Below the function responsible for the dragging:

void ListView::startDrag(Qt::DropActions supportedActions)
{
    QMap<int, QString> icons;
    icons.insert(IT_RosInit, "ROS Init");
    icons.insert(IT_Images, "Images");
    icons.insert(IT_Path, "Path");
    icons.insert(IT_RosShutDown, "ROS Shutdown");

    if (supportedActions & Qt::CopyAction)
    {
        const QList<QListWidgetItem *> &m_items(selectedItems());
        if (m_items.isEmpty())
            return;

        QPixmap pixmapLaser("/home/images/laserscan.png");
        QPixmap pixmapPCloud2("/home/images/pcloud2.png");
        // etc ...

        QStringList iconImages;

        for(int i = 0; i < icons.count(); ++i)
        {
            for (const QString &tableType : iconImages) {
                if (tableType == "ROS Init")
                {
                    auto *data = mimeData(m_items);
                    auto *drag = new QDrag(this);
                    drag->setPixmap(pixmapLaser);
                    drag->setMimeData(data);
                    drag->setHotSpot(pixmapLaser.rect().center());
                    drag->exec(Qt::CopyAction);
                 }
                else if(tableType == "Images")
                {
                    auto *data2 = mimeData(m_items);
                    auto *drag2 = new QDrag(this);
                    drag2->setPixmap(pixmapPCloud2);
                    drag2->setMimeData(data2);
                    drag2->setHotSpot(pixmapPCloud2.rect().center());
                    drag2->exec(Qt::CopyAction);
                }
            }
        }
    }
    else
    {
        QListWidget::startDrag(supportedActions);
    }
}

After subclassing the QListWidget I just reimplemented the usual drag and drop function. All other function are working properly but the startDrag and in fact as I try to drag the proper QPixmap, I actually see nothing being dragged.

I consulted this source, useful, and also this other source which was useful but it didn't reimplement the startDrag but instead dropEvent which for me is not a problem because that is working well.

I also consulted this source and this other source but that also didn't help fixing the problem.

Thanks for shedding light on this matter for solving the problem

Aucun commentaire:

Enregistrer un commentaire