0

Qt と Qgraphicsview を使ってチックタクトゲームを作ろうとしたのですが、 mousePressEvent で Graphicstextitem を使ってボード上で x を描画すると、X が表示されません。それをどのように修正しますか?

問題は、テキストアイテムのシーンがメインファイルのシーンと異なることだと思いますが、それを修正する方法がわかりません。

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

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


     } 

   }

game.h で:

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

     Game();

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




       };
4

1 に答える 1