拖放操作后事件不起作用

问题描述:

我为我们的项目开发某种构建器。我想在我的应用程序中同时使用拖放支持和上下文菜单。目前我使用拖放支持,但没有运气与上下文菜单。 左侧我的gui是工具箱。我正在拖放窗口部件到右侧(QGraphicsScene),我也想在QGraphicsScene中使用上下文菜单。我之前使用图形场景中的上下文菜单。但之前我没有使用drap &下拉操作。我写了适当的代码,但它不起作用。缺少什么?(是否相关拖动&下降)。以下是我的代码文件。
//#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(); 
    } 
} 

问题是在这里。你已经“吃掉了”右键:)尝试回落到鼠标按钮的基本实现中

+0

我觉得问题与鼠标按钮无关。因为我回退到基地implmentation但上下文菜单不起作用。 – onurozcelik 2010-08-23 08:11:26

+0

我想我遇到了类似的问题。在执行拖放操作后,无论按下哪个按钮,我的下一个鼠标按键都将被先前拖动的GraphicsWidget接收。你有没有想过这个? – Brianide 2012-12-13 14:53:30