2010-08-19 68 views
0

我爲我們的項目開發某種構建器。我想在我的應用程序中同時使用拖放支持和上下文菜單。目前我使用拖放支持,但沒有運氣與上下文菜單。 左側我的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); 
} 

回答

1
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