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 and drop problem

drag and drop problem

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 621 Views
  • 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.
  • Zakaria94Z Offline
    Zakaria94Z Offline
    Zakaria94
    wrote on last edited by
    #1

    Hi everybody,
    i'm working on a game named othello , and i want to use drag and drop.
    So i set a mousePressEvent with a if, the if call a function and the else calls another function.
    The if call a pick up function(saisirPion) and the else a drop function(placerPion).
    the pick up works perfectlly but i can't drop it.

    //your code here
    #ifndef JEU_H
    #define JEU_H
    
    
    #include<QGraphicsView>
    #include<QGraphicsScene>
    #include "othellier.h"
    #include<QMouseEvent>
    
    class jeu:public QGraphicsView
    {
        Q_OBJECT
    public:
    
        jeu(QWidget* parent=NULL);
    
        void cachermenu();
        QString getTour();
        void setTour(QString joueur);
        void saisirPion(carreau* Pion);
        void placerPion(carreau* PionARemplacer);
        void PasserTour();
        void EnleverDeLaBarre(carreau* Pion,QString joueur);
    
    
    
          void mouseMoveEvent(QMouseEvent *event);
          void mousePressEvent(QMouseEvent *event);
    
    
    
        QGraphicsScene* scene;
        othellier* Othellier;
        carreau* PionAPlacer;
        QPointF positionOriginal;
    
    public  slots:
        void jouer();
    
    private:
    void barrejoueur(int x, int y, int width, int height, QColor couleur);
    void interface();
    void createNewCard(QString proprio);
    void createinitialcard();
    void drawcard();
    
    
    
    QString tour;
    QGraphicsTextItem* tourText;
    QList<carreau *> player1cards;
    QList<carreau *> player2cards;
    
    
    
    
    
    };
    
    
    //#include<QGraphicsRectItem>
    #include<QGraphicsSceneMouseEvent>
    #include <QWidget>
    class carreau: public QGraphicsRectItem
    {
    public:
        carreau(QGraphicsItem* parent=NULL);
        void creerLignes();
        void trouverVoisin();
        void changerJoueur();
        void Renverser();
    
    
        void setowner(QString proprio);
        void setEstPlace(bool b);
    
        void mousePressEvent(QGraphicsSceneMouseEvent *event);
    
    
        bool getEstPlace();
        QString getProprio();
    
    
    
    private:
        QString joueur;
        bool EstPlace;
        QList<carreau* > voisins;
        QList<QGraphicsLineItem* > lignes;
    
    
    
    
    
    
    };
    

    and the definition of the pickup and the drop functions

    //void jeu::saisirPion(carreau *Pion)
    {
        if(Pion->getProprio() == getTour() && PionAPlacer ==NULL)
        {
            PionAPlacer = Pion;
            positionOriginal = Pion->pos();
            return;
    
        }
    }
    
    void jeu::placerPion(carreau *PionARemplacer)
    {
        PionAPlacer->setPos(PionARemplacer->pos());
        Othellier->getcarreaux().removeAll(PionARemplacer);
        Othellier->getcarreaux().append(PionAPlacer);
        scene->removeItem(PionARemplacer);
        PionAPlacer->setEstPlace(true);
        EnleverDeLaBarre(PionAPlacer,getTour());
        PionAPlacer->trouverVoisin();
        PionAPlacer = NULL;
        createNewCard(getTour());
        PasserTour();
    }
    
    //
    void carreau::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        if(getEstPlace() == false)
        {
            Jeu->saisirPion(this);
        }
    
    else{
            Jeu->placerPion(this);
    
        }
    
    
    }
    
    bool carreau::getEstPlace()
    {
        return EstPlace;
    
    }
    
    void carreau::setEstPlace(bool b)
    {
        EstPlace = b;
    
    }
    
    

    I got this warning : "QGraphicsScene::removeItem: item 0x17818ff0's scene (0x0) is different from this scene (0x15cbd048)"
    I'm stuck here for 2 days i can't find where is the problem, thank's for your help

    1 Reply Last reply
    0
    • Joel BodenmannJ Offline
      Joel BodenmannJ Offline
      Joel Bodenmann
      wrote on last edited by Joel Bodenmann
      #2

      Hello and welcome to the community!

      I think that you are making your own life complicated. From what I can see you only want to move a QGraphicsItem around in a QGraphicsScene. You don't want to move them between different widgets - is that correct?
      If so, I recommend letting QGraphicsScene doing all the hard work: Simply make your item become movable by settings the QGraphicsItem::ItemIsMovable flag to true. From then on you can move your item around by dragging it (click and hold to move and it will stop moving once you release the mouse button). That's exactly the behavior you want to archive if I interpret your code pieces correctly.
      If you need your pieces to snap to certain predefined positions you can do that by implementing a Snap-to-Grid behavior in the graphics items itemChange() method.

      To answer your actual question: The warning you mention is what you get when you try to remove an item from a scene that doesn't contain the item. Without further analyzing your code that's most likely the case when you call scene->removeItem(PionARemplacer); and PionARemplacer isn't assigned to that scene (according to the message you get it's not assigned to any scene because it says that the scene is null).
      However, what I don't understand is your statement that you can't drop it. The message you get simply tells you that something is wrong with removing the item. However, in your case that shouldn't have any effect at all. Can you further explain the issue that you are facing?

      By the way, a generic remark: Try to avoid using non-english terms in source code. Parceque pas tout le monde parle francais ;)

      Industrial process automation software: https://simulton.com
      Embedded Graphics & GUI library: https://ugfx.io

      1 Reply Last reply
      1

      • Login

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