c++ - 拖放操作后事件不起作用

标签 c++ events qt drag-and-drop qgraphicsview

我正在为我们的项目开发某种生成器。我想在我的应用程序中同时使用拖放支持和上下文菜单。目前我使用拖放支持,但上下文菜单不走运。 我的图形用户界面左侧是工具箱。我正在将小部件拖放到右侧 (QGraphicsScene),我还想在 QGraphicsScene 中使用上下文菜单。我以前在图形场景中使用上下文菜单。但是之前我没有使用drap & drop操作。我编写了正确的代码,但它不起作用。缺少什么?(是否与拖放相关)。下面是我的代码文件。
//#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 的基本实现

关于c++ - 拖放操作后事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3519442/

相关文章:

c++ - 我的重载运算符 < 不适用于 cpp 中的排序函数

C++ -- 关于函数指针的问题

delphi - 使用 Button OnClick 将值输入到 TEdit

events - 扩展面板中的 Scala Swing react

c++ - 需要帮助理解为什么我在 OpenCV C 代码中收到此错误消息...?

c++ - 如何向 Swift 隐藏 Objective-C 类的部分接口(interface)?

c# - 将扩展方法中的事件和函数分配为参数

c++ - Qt中是否有签名的 `sizeof`替代品

c++ - QT C++ - 如何引用另一个类中的小部件

node.js - Node Gui安装找不到Cmake