Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 事件处理:访问另一个小部件';s属性_Qt_Drag And Drop_Event Handling - Fatal编程技术网

Qt 事件处理:访问另一个小部件';s属性

Qt 事件处理:访问另一个小部件';s属性,qt,drag-and-drop,event-handling,Qt,Drag And Drop,Event Handling,好的,这应该很容易 我尝试将drop事件处理到QGraphicsView小部件上。从QTreeView小部件拖动的传入数据。为此,我重新实现了以下方法: void QGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event) { event.accept(); } void QGraphicsScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event) {

好的,这应该很容易

我尝试将drop事件处理到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);
}
这很好用;但是如何在这个小部件的drop事件中更改另一个graphicsview场景呢?即:

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);
}

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

子类化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;
}
void GUI::GUI()
{     
ui->mGraphicsView->installEventFilter(此);
}
BOOLGUI::eventFilter(QObject*对象,QEvent*事件)
{
if(object==ui->mGraphicsView&&event->type()==QEvent::DropEnter){
QDropEvent*dropEvent=静态强制转换(事件);
//处理丢弃事件
返回true;
}
其他的
返回false;
}

我应该继承图形视图类并从中创建信号和对象吗?或者我应该改变qgraphicsview标题吗?你说你重新实现了那些方法,怎么实现的?您不应该更改QGraphicsView,您应该将其子类化。我重写了QGraphicscene和QGraphicsView的拖放事件(我实际上收到了“不一致的dll”警告,但工作正常),这样做是错误的吗?我是否应该将我的小部件升级到另一个继承QGraphicsView的类中;然后在那里放置并重新实现拖动事件?是的,你应该定义它的子类,然后重新实现你需要使用的方法,或者你可以使用事件过滤器,检查我编辑的回答哇,我刚刚学习了事件过滤器。它们更容易处理,我真的不需要子类化。谢谢你,伙计