Search code examples
c++qtqgraphicsviewqgraphicssceneqgraphicstextitem

how to use scene object of main file in other file?


I tried to make tictactoe game using Qt and Qgraphicsview but when I draw x on board using Graphicstextitem in mousePressEvent , X does not appear. how fix that ?

I think the problem is that scene of textitem Different from scene of main file but I do not know how fix that.

main.cpp:

int main(int argc, char *argv[])
 {
   QApplication a(argc, argv);
      Game *gm = new Game;

     Game *rect1=new Game;
  gm->scenc->setSceneRect(0,0,800,600);
  rect1->setRect(160,100,150,150);
  gm->scenc->addItem(rect1);
  gm->view->setScene(gm->scenc);
  gm->view->show();
  return a.exec();
 }

in game.cpp:

#include <game.h>

 Game::Game()
 {
  scenc= new QGraphicsScene;
  view = new QGraphicsView;
  text= new QGraphicsTextItem;
}

 void Game::mousePressEvent(QGraphicsSceneMouseEvent *event)
 {
    if (event->buttons() == Qt::LeftButton )
     { 
                 text->setPlainText("X");
                 text->setFont(QFont("Tahoma",24));
                 text->setPos((160+160+120)/2,140);
                 scenc->addItem(text);


     } 

   }

in game.h :

 class Game : public QObject , public QGraphicsRectItem
{
    Q_OBJECT
    public:

     Game();

     QGraphicsScene *scenc;
     QGraphicsView *view;
     QGraphicsTextItem *text;
     void mousePressEvent(QGraphicsSceneMouseEvent *event);




       };

Solution

  • The following example illustrates how to do it the right way. Firstly, you have to notice, that Game::mousePressEvent doesn't override any virtual function. It's a good habit to use the override keyword and to drop the virtual keyword in order to be sure, that a virtual function is overwritten.

    Game is not derived from QGraphicsScene and has therefore no mousePressEvent member.

    Try the following example app.

    MyScene.h

    #pragma once
    #include <QWidget>
    #include <QDebug>
    #include <QHBoxLayout>
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QGraphicsTextItem>
    #include <QGraphicsSceneMouseEvent>
    
    class MyScene : public QGraphicsScene {
        Q_OBJECT
    public:
        MyScene(QWidget* parent = nullptr) : QGraphicsScene(parent) {
            setSceneRect(0, 0, 800, 600);       
        }
        void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* mouseEvent) override {
            if (mouseEvent->buttons() == Qt::LeftButton)
            {
                auto text = new QGraphicsTextItem;
                addItem(text);
                text->setPlainText("X");
                text->setPos(mouseEvent->scenePos());
            }
        }
    private:
        QGraphicsView* mView;
        QGraphicsTextItem* mText;
    };
    

    main.cpp

    #include <QApplication>
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QGraphicsTextItem>
    #include "MyScene.h"
    
    int main(int argc, char* argv[])
    {
        QApplication a(argc, argv);
        auto scene = new MyScene;
        auto view = new QGraphicsView;
        view->setScene(scene);
        view->show();
        return a.exec();
    }