繁体   English   中英

拖放操作后事件不起作用

[英]Event does not work after drag and drop operation

我为我们的项目开发了某种构建器。 我想在我的应用程序中同时使用拖放支持和上下文菜单。 目前,我使用拖放支持,但上下文菜单没有运气。 我的GUI左侧是工具箱。 我将小部件拖放到右侧(QGraphicsScene),我也想在QGraphicsScene中使用上下文菜单。 我以前在图形场景中使用上下文菜单。 但是在我没有使用过拖放操作之前。 我编写了正确的代码,但是它不起作用。 缺少什么?(与拖放相关)。 以下是我的代码文件。
//#dragwidget.cpp-工具箱小部件

#include "dragwidget.h"

DragWidget::DragWidget(void)
{
}

DragWidget::~DragWidget(void)
{
}

void DragWidget::mousePressEvent(QMouseEvent *ev)
{
    if(ev->button() == Qt::LeftButton)
    {
        QMimeData *data = new QMimeData;
        data->setProperty("type",property("type")); 
        QDrag *drag = new QDrag(this);
        drag->setMimeData(data);
        drag->start();
    }
}

//#view.cpp-QGraphicsView的包装器类

#include "view.h"

View::View(Scene *scene,QWidget *parent)
:QGraphicsView(scene,parent)
{
}

View::~View()
{
}

//#scene.cpp-QGraphicsScene的包装器类,可捕获放置事件

#include "scene.h"

Scene::Scene(QObject *parent)
:QGraphicsScene(parent)
{
}

Scene::~Scene(void)
{
}

void Scene::setSceneRect(qreal x, qreal y, qreal w, qreal h)
{
    QGraphicsScene::setSceneRect(x,y,w,h);
}

void Scene::dragEnterEvent(QGraphicsSceneDragDropEvent *e)
{
    if(e->mimeData()->hasFormat("text/plain"));
        e->acceptProposedAction();
}

void Scene::dragMoveEvent(QGraphicsSceneDragDropEvent *e)
{
}

void Scene::dropEvent(QGraphicsSceneDragDropEvent *e)
{
    e->acceptProposedAction();
    int itemType = e->mimeData()->property("type").toInt();
    switch(itemType)
    {
    case BlockSegment:
        drawStandartBlockSegment(e->scenePos());
        break;
    case Switch:
        drawStandartSwitch(e->scenePos());
        break;
    case Signal:
        drawStandartSignal(e->scenePos());
        break;
    }
}

void Scene::drawStandartBlockSegment(QPointF p)
{
    BlockSegmentItem *blockSegment = new BlockSegmentItem(p.x(),p.y(),p.x()+100,p.y());
    addItem(blockSegment);
}

void Scene::drawStandartSwitch(QPointF p)
{
    SwitchItem *switchItem = new SwitchItem(":app/SS.svg");
    switchItem->setPos(p);
    addItem(switchItem);
}

void Scene::drawStandartSignal(QPointF p)
{
    SignalItem *signalItem = new SignalItem(":app/sgs3lr.svg");
    signalItem->setPos(p);
    addItem(signalItem);
}

//#item.cpp-我的自定义图形场景项目的基类

#include "item.h"

Item::Item(ItemType itemType, QGraphicsItem *parent)
:QGraphicsWidget(parent),m_type(itemType)
{
}

Item::~Item()
{
}

int Item::type()
{
    return m_type;
}

QRectF Item::boundingRect() const
{
    return QRectF(0,0,0,0);
}

QPainterPath Item::shape() const
{
    QPainterPath path;
    return path;
}

void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(painter);
    Q_UNUSED(option);
    Q_UNUSED(widget);
}

void Item::deleteItem()
{

}

void Item::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    Q_UNUSED(event);
}

//#switchitem.cpp-我的自定义开关项。

#include "switchitem.h"

SwitchItem::SwitchItem(const QString& fileName,QGraphicsItem *parent /* = 0 */)
:Item(Switch,parent)
{
    svg = new QGraphicsSvgItem(fileName,this);
    createActions();
    createContextMenu();
}

SwitchItem::~SwitchItem(void)
{
}

QRectF SwitchItem::boundingRect() const
{
    return QRectF(pos().x(),pos().y(),svg->boundingRect().width(),
                                  svg->boundingRect().height());
}

QPainterPath SwitchItem::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}

void SwitchItem::createActions()
{
    deleteAct = new QAction("Sil",this);
    deleteAct->setIcon(QIcon(":app/delete.png"));
    connect(deleteAct,SIGNAL(triggered()),this,SLOT(deleteItem()));
}

void SwitchItem::createContextMenu()
{
    contextMenu = new QMenu("SwitchContextMenu");
    contextMenu->addAction(deleteAct);
}

void SwitchItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *e)
{
    contextMenu->exec(e->screenPos());
}

//#app.cpp-应用程序类

#include "app.h"
#include "dragwidget.h"

App::App(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    Q_INIT_RESOURCE(app);
    ui.setupUi(this);
    canvasScene = new Scene(this);
    canvasScene->setSceneRect(0,0,5000,5000);
    canvasView = new View(canvasScene);
    canvasView->setBackgroundBrush(Qt::black);
    canvasView->ensureVisible(0,0,canvasView->geometry().width(),canvasView->geometry().height());
    QHBoxLayout *layout = new QHBoxLayout;
    toolBox = new QFrame(this);
    layout->addWidget(toolBox,1);
    layout->addWidget(canvasView,7);
    ui.centralWidget->setLayout(layout);
    createToolBox();

}

App::~App()
{

}

void App::createToolBox()
{
    QVBoxLayout *layout = new QVBoxLayout;
    QLabel *blockSegmentLabel = new QLabel("Block Segment");
    DragWidget *blockSegmentWidget = new DragWidget;
    blockSegmentWidget->setProperty("type",BlockSegment);
    blockSegmentWidget->setPixmap(QPixmap(":app/blocksegment.png"));
    QLabel *switchLabel = new QLabel("Switch");
    DragWidget *switchWidget = new DragWidget;
    switchWidget->setProperty("type",Switch);
    switchWidget->setPixmap(QPixmap(":app/SS.png"));
    QLabel *signalLabel = new QLabel("Signal");
    DragWidget *signalWidget = new DragWidget;
    signalWidget->setProperty("type",Signal);
    signalWidget->setPixmap(QPixmap(":app/sgs3lr.png"));
    layout->addWidget(blockSegmentLabel);
    layout->addWidget(blockSegmentWidget);
    layout->addWidget(switchLabel);
    layout->addWidget(switchWidget);
    layout->addWidget(signalLabel);
    layout->addWidget(signalWidget);
    toolBox->setLayout(layout);
}
void DragWidget::mousePressEvent(QMouseEvent *ev)
{
    if(ev->button() == Qt::LeftButton)
    {
        QMimeData *data = new QMimeData;
        data->setProperty("type",property("type")); 
        QDrag *drag = new QDrag(this);
        drag->setMimeData(data);
        drag->start();
    }
}

问题在这里。 您已经“吃”了右键:)尝试回退mousePressEvent的基本实现

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM