拖拽将照片文件拖放到QGraphicsView

时间:2013-07-08 15:13:10

标签: c++ qgraphicsview

如何让QGraphicsView处理拖放?比如说,从某个文件夹中拖动图像文件,然后放到QGraphicsView? Windows,QT5.2,C ++。

2 个答案:

答案 0 :(得分:2)

你需要创建一个QGraphicsScene的子类(如果你还没有这样做)并覆盖它的dragEnterEvent(QGraphicsSceneDragDropEvent *)方法,如果参数代表你想要的图像文件,就在它的参数上调用acceptProposedAction()能够处理 - 即如果dragDropEvent-> mimeData() - > hasUrls()返回true,并且至少有一个由dragDropEvent-> mimeData() - > urls()返回的QUrls可用于构造一个有效的QPixmap(通过QPixmap(url [i] - > toLocalFile))。

然后,您需要覆盖dropEvent(QGraphicsSceneDragDropEvent *)方法以创建新的QGraphicsPixmapItem对象(使用在QGraphicsSceneDragDropEvent对象的mimeData对象中指定的文件名创建QPixmap)并将QGraphicsPixmapItem添加到QGraphicsScene。

答案 1 :(得分:1)

我还需要在场景中添加QGraphicsItem,在项目上添加setAcceptDrops(true)background项目可通过以下代码完成这项工作。

#include <QApplication>
#include <QDragEnterEvent>
#include <QGraphicsRectItem>
#include <QGraphicsSceneDragDropEvent>
#include <QGraphicsView>
#include <QMainWindow>
#include <QMimeData>
#include <QUrl>

class MyGraphicsScene : public QGraphicsScene
{
    Q_OBJECT

  protected:
    QGraphicsRectItem* background = nullptr;

  public:
    MyGraphicsScene()
    {
        this->addItem( this->background = new QGraphicsRectItem(0, 0, 1, 1) );
        background->setAcceptDrops(true);
    }

    virtual void resize(int width, int height)
    {
        this->background->setRect(0, 0, width - 1, height - 1);
    }

  protected:
    virtual void dragEnterEvent(QGraphicsSceneDragDropEvent* event) override
    {
        if ( event->mimeData()->hasUrls() )
            event->acceptProposedAction();
    }

    virtual void dropEvent(QGraphicsSceneDragDropEvent* event) override
    {
        if ( event->mimeData()->hasUrls() )
            foreach ( const QUrl& url, event->mimeData()->urls() )
                qDebug("GraphicsScene: dropped %s", qPrintable(url.toLocalFile()) );
    }
};

class MyGraphicsView : public QGraphicsView
{
    Q_OBJECT

  protected:
    MyGraphicsScene myGraphicsScene;

  public:
    MyGraphicsView()
    {
        this->setScene( &this->myGraphicsScene );
    }

  protected:
    virtual void resizeEvent(QResizeEvent* event) override
    {
        this->myGraphicsScene.resize( event->size().width(), event->size().height() );
    }
};

class MyMainWindow : public QMainWindow
{
    Q_OBJECT

  protected:
    MyGraphicsView* myGraphicsView = nullptr;

  public:
    MyMainWindow(QWidget *parent = nullptr)
        : QMainWindow(parent)
    {
        this->setAcceptDrops(true);
        this->setCentralWidget(this->myGraphicsView = new MyGraphicsView());
    }

  protected:
    virtual void dragEnterEvent(QDragEnterEvent* event) override
    {
        if ( event->mimeData()->hasUrls() )
            event->acceptProposedAction();
    }

    virtual void dropEvent(QDropEvent *event) override
    {
        if ( event->mimeData()->hasUrls() )
            foreach ( const QUrl& url, event->mimeData()->urls() )
                qDebug("MainWindow: dropped %s", qPrintable(url.toLocalFile()) );
    }
};

int main(int argc, char *argv[])
{
    QApplication application(argc, argv);
    MyMainWindow myMainWindow;
    myMainWindow.show();
    return application.exec();
}

#include "main.moc"