Hello Again!
If you don't call the base class implementation, the scene shouldn't receive the event.
OK, so there's the problem? I see the exact opposite of this.
Shall we have a quick overview of my code -
View header file
{
public:
protected:
};
class myView : public QGraphicsView
{
public:
myView(QWidget *parent=0);
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
};
To copy to clipboard, switch view to plain text mode
view methods
// setAcceptDrops(true);
// Comment the line below, and drop starts working
}
// event->accept();
qDebug() << "View: dragEnterEvent";
}
qDebug() << "View sees drop";
pos = event->pos();
pnt = this->mapToScene( event->pos() );
if (itm = itemAt( pos ) )
{
qDebug("There is an item here");
return;
}
else
{
qDebug("Blank area");
}
// Is there something underneath?
QByteArray ba
= event
->mimeData
()->data
("foo/bar");
while (!ds.atEnd()) {
MyNode * nd;
ds >> name >> path >> monid;
qDebug() << name << path << monid;
//
// This view is inside the central widget which is in the mainwindow, so node
// parent is the mainwindow so it can access the action functions. a bit
// untiy huh?
//
nd
= new MyNode
( (QWidget*)(this
->parent
())->parent
(), name, path, monid, MyNode
::BUTTON);
else
nd
= new MyNode
( (QWidget*)(this
->parent
())->parent
(), name, path, monid, MyNode
::POINT);
sc = this->scene();
pnt = this->mapToScene( event->pos() );
qDebug() << pnt;
nd->setPos(pnt);
sc->addItem(nd);
}
}
}
myView::myView(QWidget *parent) : QGraphicsView(parent) {
// setAcceptDrops(true);
// Comment the line below, and drop starts working
setScene(new QGraphicsScene(this));
}
void myView::dragEnterEvent(QDragEnterEvent *event) {
// event->accept();
qDebug() << "View: dragEnterEvent";
QGraphicsView::dragEnterEvent(event);
}
void myView::dropEvent(QDropEvent *event) {
QPointF pnt;
QPoint pos;
QGraphicsItem * itm;
qDebug() << "View sees drop";
pos = event->pos();
pnt = this->mapToScene( event->pos() );
if (itm = itemAt( pos ) )
{
qDebug("There is an item here");
QGraphicsView::dropEvent(event);
return;
}
else
{
qDebug("Blank area");
}
// Is there something underneath?
QByteArray ba = event->mimeData()->data("foo/bar");
QDataStream ds(&ba, QIODevice::ReadOnly);
while (!ds.atEnd()) {
QString name;
QString path;
QString monid;
MyNode * nd;
ds >> name >> path >> monid;
qDebug() << name << path << monid;
//
// This view is inside the central widget which is in the mainwindow, so node
// parent is the mainwindow so it can access the action functions. a bit
// untiy huh?
//
if (QString("Button")==name)
nd = new MyNode( (QWidget*)(this->parent())->parent(), name, path, monid, MyNode::BUTTON);
else
nd = new MyNode( (QWidget*)(this->parent())->parent(), name, path, monid, MyNode::POINT);
QGraphicsScene * sc;
sc = this->scene();
pnt = this->mapToScene( event->pos() );
qDebug() << pnt;
nd->setPos(pnt);
sc->addItem(nd);
}
}
void myView::dragMoveEvent(QDragMoveEvent *event) {
}
To copy to clipboard, switch view to plain text mode
Scene header
{
public:
void dropEvent(QGraphicsSceneDragDropEvent * event);
void dragEnterEvent(QGraphicsSceneDragDropEvent * event);
};
class myScene : public QGraphicsScene
{
public:
myScene(QObject *parent);
void dropEvent(QGraphicsSceneDragDropEvent * event);
void dragEnterEvent(QGraphicsSceneDragDropEvent * event);
};
To copy to clipboard, switch view to plain text mode
scene code
{
}
void myScene::dropEvent(QGraphicsSceneDragDropEvent * event)
{
qDebug() << "scene: dropEvent";
qDebug() << items(event->scenePos()).count();
this->setFocus(Qt::NoFocusReason);
gi = this->itemAt( event->scenePos() );
if (gi)
{
qDebug() << "Scene(dropEvent); there is an item thar";
gi->setFocus(Qt::NoFocusReason);
gi->setSelected(true);
}
}
void myScene::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
{
event->acceptProposedAction();
gi = this->itemAt( event->scenePos() );
if (gi)
{
qDebug() << "Scene(dragEnterEvent); there is an item thar";
gi->setFocus(Qt::NoFocusReason);
gi->setSelected(true);
}
qDebug() << "scene: dragEnterEvent";
}
myScene::myScene(QObject*parent): QGraphicsScene(parent)
{
}
void myScene::dropEvent(QGraphicsSceneDragDropEvent * event)
{
qDebug() << "scene: dropEvent";
qDebug() << items(event->scenePos()).count();
this->setFocus(Qt::NoFocusReason);
QGraphicsItem *gi;
gi = this->itemAt( event->scenePos() );
if (gi)
{
qDebug() << "Scene(dropEvent); there is an item thar";
gi->setFocus(Qt::NoFocusReason);
gi->setSelected(true);
}
QGraphicsScene::dropEvent(event);
}
void myScene::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
{
event->acceptProposedAction();
QGraphicsItem *gi;
gi = this->itemAt( event->scenePos() );
if (gi)
{
qDebug() << "Scene(dragEnterEvent); there is an item thar";
gi->setFocus(Qt::NoFocusReason);
gi->setSelected(true);
}
qDebug() << "scene: dragEnterEvent";
QGraphicsScene::dragEnterEvent(event);
}
To copy to clipboard, switch view to plain text mode
And finally item header
{
public:
int MonId;
int ValInt;
int DataType;
enum MimicType { LAMP, BUTTON, POINT };
MimicType mimic;
int EditMode;
void dropEvent(QGraphicsSceneDragDropEvent * event);
void dragEnterEvent(QGraphicsSceneDragDropEvent * event);
void dragMoveEvent(QGraphicsSceneDragDropEvent * event);
void myupdate();
void createActions();
private slots:
void doMyAct();
};
class MyNode : public QGraphicsItem
{
public:
int MonId;
int ValInt;
int DataType;
QString endnode;
QString path;
QString monid;
enum MimicType { LAMP, BUTTON, POINT };
MimicType mimic;
int EditMode;
MyNode(QWidget * parent, QString endnode, QString path, QString monid, MimicType mt);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem * option, QWidget *widget);
void mousePressEvent(QGraphicsSceneMouseEvent * event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
void dropEvent(QGraphicsSceneDragDropEvent * event);
void dragEnterEvent(QGraphicsSceneDragDropEvent * event);
void dragMoveEvent(QGraphicsSceneDragDropEvent * event);
void myupdate();
void createActions();
QAction * myAct;
void passDrop(QDropEvent * event);
private slots:
void doMyAct();
QWidget * parent;
};
To copy to clipboard, switch view to plain text mode
and the item drag methods...
void MyNode::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
{
qDebug() << "Node: dragEnterEvent";
//event->setAccepted(true);
//event->accept();
event->acceptProposedAction();
}
void MyNode::dragMoveEvent(QGraphicsSceneDragDropEvent * event)
{
qDebug() << "Node: dragMoveEvent";
event->accept();
}
void MyNode::dropEvent(QGraphicsSceneDragDropEvent * event)
{
qDebug() << "Node: dropEvent";
}
void MyNode::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
{
qDebug() << "Node: dragEnterEvent";
//event->setAccepted(true);
//event->accept();
event->acceptProposedAction();
QGraphicsItem::dragEnterEvent(event);
}
void MyNode::dragMoveEvent(QGraphicsSceneDragDropEvent * event)
{
qDebug() << "Node: dragMoveEvent";
event->accept();
QGraphicsItem::dragMoveEvent(event);
}
void MyNode::dropEvent(QGraphicsSceneDragDropEvent * event)
{
qDebug() << "Node: dropEvent";
QGraphicsItem::dropEvent(event);
}
To copy to clipboard, switch view to plain text mode
Bookmarks