0

好的,这应该很容易。

我试图将放置事件处理到 QGraphicsView 小部件上。从 QTreeView 小部件拖动的传入数据。为此,我重新实现了这些方法:

void QGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
    event.accept();
}

void QGraphicsScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
    event.accept();
}

void QGraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event)
{
    event.accept();
}


void QGraphicsView::dropEvent(QDropEvent *event)
{
    QPixmap pixmap(event->mimedata()->urls()[0].toString().remove(0,8));
    this.scene()->addPixmap(pixmap);
}

这很好用;但是如何在这个小部件的放置事件中更改另一个图形视图场景?那是:

void QGraphicsView::dropEvent(QDropEvent *event)
{
    QPixmap pixmap(event->mimedata()->urls()[0].toString().remove(0,8));
    // I cannot access ui; and cannot access my widgets...:
    ui->anotherview->scene()->addPixmap(pixmap);
}
4

1 回答 1

1

在您的 QGraphicsView 中制作一个自定义信号void showPixmap(QPixmap p)并将其连接到您的主 gui 类中的一个插槽,您可以在其中访问 ui 元素。然后您可以调用emit showPixamp(pixmap)dropEvent。

子类化 QGraphicsView

//header file
class CustomView : public QGraphicsView 
{
public:
    CustomView(QGraphicsScene*, QWidget*=NULL);
    ~CustomView();

signals:
    void showPixmap(QPixmap p);

protected:
    virtual void dropEvent(QDropEvent *event);
};


//cpp file
CustomView::CustomView(QGraphicsScene *scene, QWidget* parent)
    :QGraphicsView(scene, parent) 
{
    //if you need to initialize variables, etc.
}
void CustomView::dropEvent(QDropEvent *event)
{
    //handle the drop event
    QPixmap mPixmap;
    emit showPixmap(mPixmap);
}

在主 GUI 类中使用事件过滤器

void GUI::GUI()
{     
    ui->mGraphicsView->installEventFilter(this);
}
bool GUI::eventFilter(QObject *object, QEvent *event)
{
    if (object == ui->mGraphicsView && event->type() == QEvent::DropEnter) {
        QDropEvent *dropEvent = static_cast<QDropEvent*>(event);
        //handle the drop event
        return true;
    }
    else
        return false;
}
于 2013-08-21T17:50:35.410 回答