Results 1 to 18 of 18

Thread: Question about drag and drop in QGraphicsView

  1. #1
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Question about drag and drop in QGraphicsView

    First of all hello to all fellow programmers. I am a new member so wanted to say hi first.

    I am working with a little projekt to learn more about the graphicsscene, graphicsview and graphicsitem and how the work together. I have managed to create a graphicsview and graphicsscene and managed to add items to it using the mouse events.

    Now what i want to do is to move these items i have added. And going crazy here. Been reading docs for 2 days now but havent really figured it out yet. I am a bit of a newbie at Qt so maybe thats why i cant grasp it.

    Hopefully there are some helpfull people here that can help me out understanding this.

    Also attached my code in a zip

    Kind regards
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Question about drag and drop in QGraphicsView

    And going crazy here
    It would be helpful if you explain what you are going crazy about.
    What are the SPECIFIC problems you have or don't understand.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about drag and drop in QGraphicsView

    The SPECIFIC problem i have is that i dont know how to make the items i add to the graphicsscene movable. So I can click on them and drag them with my mouse.

    Have read somewhere to reimplement QGraphicsItem::dragEnterEvent(), QGraphicsItem::dragMoveEvent(), QGraphicsItem::dragLeaveEvent(), and QGraphicsItem::dropEvent() but dont quite understand how. Are there other ways?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Question about drag and drop in QGraphicsView

    but dont quite understand how.
    You don't understand how - what?
    Based on your text it sounds you don't know how to subclass or override a method - if this is the case, its not Qt but C++ question, which is off topic for this forum.
    If its something else you don't understand, please be SPECIFIC about what your problem is, if you want help!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about drag and drop in QGraphicsView

    Your code has a lot of problems such as this:
    Qt Code:
    1. void MainWindow::paintEvent(QPaintEvent*)
    2. {
    3. QPainter painter(this);
    4. QPen pen(Qt::gray, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    5. painter.setPen(pen);
    6. ui->graphicsView->setScene(m_scene);
    7. }
    To copy to clipboard, switch view to plain text mode 
    Adding items in mouse move events is probably a bad idea as well. Spaghetti code doesn't help too, try simplifying it.

    As for your problem - if you want to make items movable, you don't need to use drag and drop, that's for something else. You can make items movable by setting the ItemIsMovable flag on the item and making sure the scene has a chance to process mouse events (that is if you reimplement mouse event handlers, be sure to call the base class implementation).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    meazza (30th March 2011)

  7. #6
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about drag and drop in QGraphicsView

    Quote Originally Posted by wysota View Post
    Your code has a lot of problems such as this:
    Qt Code:
    1. void MainWindow::paintEvent(QPaintEvent*)
    2. {
    3. QPainter painter(this);
    4. QPen pen(Qt::gray, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    5. painter.setPen(pen);
    6. ui->graphicsView->setScene(m_scene);
    7. }
    To copy to clipboard, switch view to plain text mode 
    Adding items in mouse move events is probably a bad idea as well. Spaghetti code doesn't help too, try simplifying it.

    As for your problem - if you want to make items movable, you don't need to use drag and drop, that's for something else. You can make items movable by setting the ItemIsMovable flag on the item and making sure the scene has a chance to process mouse events (that is if you reimplement mouse event handlers, be sure to call the base class implementation).
    I can understand that my code looks bad. I have just used it to try some stuff out and there are some stuff left that I am not using like the code you pointed out. But your explenation helps alot. I am gonna try so simplify it and set the flags to se if it works out. Thanks alot Zen Master

  8. #7
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about drag and drop in QGraphicsView

    Qt Code:
    1. void Graphicsscene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. if(figur == 1)
    4. {
    5. if (event->button() != Qt::LeftButton)
    6. return;
    7.  
    8. QPolygonF myPolygon;
    9. myPolygon << QPointF(-100, 0) << QPointF(0, 100)
    10. << QPointF(100, 0) << QPointF(0, -100)
    11. << QPointF(-100, 0);
    12. PolygonItem->setPolygon(myPolygon);
    13. PolygonItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemSendsGeometryChanges);
    14. PolygonItem->setPos(event->scenePos());
    15. addItem(PolygonItem);
    16. update();
    17. figur = 0;
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    This code makes it possible to move the item around but have noticed that the are that you can click to move it around is very small. Is it possible to make it bigger? ex if i click on the figure so it moves it or something similar?
    Last edited by meazza; 31st March 2011 at 08:52.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about drag and drop in QGraphicsView

    So where do you have to click for it to move?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about drag and drop in QGraphicsView

    I have to click on the edge of the item, and it has to be on the pixel. Very hard to click it with the mouse.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about drag and drop in QGraphicsView

    Apparently the inside of the item is empty. If simply setting a brush doesn't help you'll have to reimplement the shape() of the item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    meazza (31st March 2011)

  13. #11
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about drag and drop in QGraphicsView

    I fixed it i think. I thought since i add the items during a pressEvent and when i tried to move the item i press the mouse so the pressEvent funktion was called hence not allowing me to move it. So added QGraphicsScene::mousePressEvent(mouseEvent); if iwas not adding items in the pressEvent.

    Well it works now but do you find this a reasonable solution?

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about drag and drop in QGraphicsView

    I don't think adding items in a mouse press event is a reasonable solution. I tend to lean towards more high-level solutions.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #13
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about drag and drop in QGraphicsView

    Well I am not a master of zen yet , but hopefully someday I will be. What would that high-level solution be? Just asking out of curiosity, not gonna try it out this time. Not high-level yet .

    Thanks for all your help

  16. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about drag and drop in QGraphicsView

    Like emitting a signal "hey, I want item to be added at these coordinates" and then handling it in a dedicated method that can be called from different contexts. And mousePress is probably a bad place to do it anyway, mouseRelease would be a better one.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #15
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Question about drag and drop in QGraphicsView

    What about the solution of reimplement the function mouseMoveEvent()?
    You can do something like that too:
    Qt Code:
    1. void Object::mouseMoveEvent(QMouseEvent *event){
    2. PolygonItem->move(event->x(),event->y());
    3. }
    To copy to clipboard, switch view to plain text mode 

  18. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about drag and drop in QGraphicsView

    Sure but why if you have the ItemIsMovable flag?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #17
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about drag and drop in QGraphicsView

    I have found another question. How do QWidgets behave in the graphicsscene? Do I need to use the QGraphicsProxyWidget to be able to move it or? Because i cant set flags to a normal QWidget.

    Qt Code:
    1. Embedded *MyWidget = new Embedded();
    2. QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
    3. proxy = addWidget(MyWidget);
    4. proxy->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    5. proxy->setPos(mouseEvent->scenePos());
    6. addItem(proxy);
    7. figur = 0;
    To copy to clipboard, switch view to plain text mode 

    I tried this and it positions as i would like to but cant move it..Maybe I am waaaay out there on this one

  20. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about drag and drop in QGraphicsView

    I'd say you can't move it because the widget handles the event so it never reaches the scene code responsible for moving the item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Drag from QtreeWidget and drop to QGraphicsView
    By syjgin in forum Qt Programming
    Replies: 0
    Last Post: 3rd July 2010, 14:17
  2. Drag and Drop from QListWidget to QGraphicsView (Scene)
    By fritzone in forum Qt Programming
    Replies: 0
    Last Post: 16th June 2010, 09:41
  3. How to drag and drop on QGraphicsView frame
    By wudelei in forum Qt Programming
    Replies: 1
    Last Post: 16th April 2010, 08:04
  4. Drag and Drop from QListView to QGraphicsView
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 20th August 2009, 14:26
  5. Drag and drop from QTreeWidget to QGraphicsView
    By igu@na in forum Qt Programming
    Replies: 2
    Last Post: 27th August 2008, 08:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.