Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drag & Drop and Context Menu problem QGraphicsScene
Forum Updated to NodeBB v4.3 + New Features

Drag & Drop and Context Menu problem QGraphicsScene

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 7.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    onurozcelik
    wrote on last edited by
    #1

    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());
    }
    @

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      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)?

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • O Offline
        O Offline
        onurozcelik
        wrote on last edited by
        #3

        Sorry for my bad explanation.

        Draging & Droping to QGraphicsScene works.

        Context menu event on item inside scene does not work.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #4

          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).

          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • O Offline
            O Offline
            onurozcelik
            wrote on last edited by
            #5

            Nothing happens. Code does not even enter contextMenuEvent

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Franzk
              wrote on last edited by
              #6

              Did you set the "QWidget::contextMenuPolicy":http://doc.trolltech.com/latest/qwidget.html#contextMenuPolicy-prop correctly?

              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • O Offline
                O Offline
                onurozcelik
                wrote on last edited by
                #7

                Why should I set that?

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  Franzk
                  wrote on last edited by
                  #8

                  Well, if it's not Qt::DefaultContextMenu, you don't get the event.

                  "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    onurozcelik
                    wrote on last edited by
                    #9

                    It is not related with that. Its default option is Qt::DefaultContextMenu

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      NarutoUzumakis
                      wrote on last edited by
                      #10

                      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.com

                      Thank you in advance

                      Nikolas

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        NarutoUzumakis
                        wrote on last edited by
                        #11

                        or can anyone at least show me how the main function would look like,how everything should be called and initialized,please..
                        thanks

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved