Drag & Drop and Context Menu problem QGraphicsScene
-
wrote on 19 Aug 2010, 07:20 last edited by
I developing some kind of builder for our project. I want to use both drag and drop support and context menu in my application. Currently I use drag and drop support but no luck with context menu. Below is my builders gui.
!http://img245.imageshack.us/img245/1858/trackbuilder2.jpg(GUI)!
Left side my gui is toolbox. I am draging and droping widgets to the right side(QGraphicsScene) and I also want to use context menu inside QGraphicsScene. I use context menu inside graphics scene before. But before I did not use drap & drop operations. I write proper code but it does not work. What is missing?(Is it related drag & drop). Below is my code files.
@//#dragwidget.cpp - Toolbox widgets
#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 - Wrapper class for QGraphicsView
#include "view.h"
View::View(Scene *scene,QWidget *parent)
:QGraphicsView(scene,parent)
{
}View::~View()
{
}//#scene.cpp - Wrapper class for QGraphicsScene which catch drop events
#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 - Base class for my custom graphics scene items
#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 - my custom switch item.
#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());
}
@ -
wrote on 19 Aug 2010, 08:40 last edited by
You put down a lot of text but you fail to be specific:
[quote]I write proper code but it does not work.[/quote]
what exactly doesn't work? You expect something and you see something. What do you expect and what do you see (or not)?
-
wrote on 19 Aug 2010, 10:28 last edited by
Sorry for my bad explanation.
Draging & Droping to QGraphicsScene works.
Context menu event on item inside scene does not work.
-
wrote on 19 Aug 2010, 12:24 last edited by
What happens if you do
@void SwitchItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *e)
{
contextMenu->move(e->screenPos());
contextMenu->show();
}@instead? Maybe there's an issue with calling an event loop within an event loop here. (Can't be too sure).
-
wrote on 19 Aug 2010, 13:01 last edited by
Nothing happens. Code does not even enter contextMenuEvent
-
wrote on 19 Aug 2010, 13:34 last edited by
Did you set the "QWidget::contextMenuPolicy":http://doc.trolltech.com/latest/qwidget.html#contextMenuPolicy-prop correctly?
-
wrote on 19 Aug 2010, 13:40 last edited by
Why should I set that?
-
wrote on 19 Aug 2010, 14:06 last edited by
Well, if it's not Qt::DefaultContextMenu, you don't get the event.
-
wrote on 20 Aug 2010, 12:15 last edited by
It is not related with that. Its default option is Qt::DefaultContextMenu
-
wrote on 1 Jun 2011, 17:27 last edited by
could you please send me the whole project,every class and header file,including the main method.I have to do something similar ,just the drag and drop action,but i'm having difficulties with it and your complete code would be a great help for me..
I am supposed to do some college project,but i am seriously stuck,i've tried everything,searching online,doing it myself,but there's always some tiny error which messes everything up.Your code would definitely help me a lot..
my email is
narutouzumakis@live.comThank you in advance
Nikolas
-
wrote on 2 Jun 2011, 16:02 last edited by
or can anyone at least show me how the main function would look like,how everything should be called and initialized,please..
thanks