drag and drop problem



  • 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



  • 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 ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.