2

I have a QDialog for beginning of my game.in this class I have a QGraphicsTextItem. I want to it is clickable. When user clicked play game start. I do this but not work.

class Mydialog_start:public QDialog
{ 
   Q_OBJECT
   public:
   explicit Mydialog_start(QWidget *parent = 0);  
   signals:   
   public slots:
   void on_play_clicked();
   void on_exit_clicked();
   private:
   QGraphicsScene* scene;
   QGraphicsView* view;
   QPixmap image;
   QBrush brush;
   QGraphicsTextItem* text;
   QFont font;
   const int x_size;
   const int y_size; 
};
Mydialog_start::Mydialog_start(QWidget *parent) :
 QDialog(parent),x_size(400),y_size(400)
{
  scene=new QGraphicsScene(this);
  view=new QGraphicsView(this);
  view->setScene(scene);
  scene->setSceneRect(0,0,x_size,y_size);
  image.load(":picture/image/background.jpg");
  image=image.scaled(x_size,y_size);
  brush.setTexture(image);
  scene->setBackgroundBrush(brush);
  font.setBold(true);
  font.setPointSize(40);
  font.setItalic(true);
  text=scene->addText("play",font);
  text->setDefaultTextColor(QColor("red"));
  text->setPos(100,300);
  this->setFixedSize(400,400);
  connect(text,SIGNAL(linkActivated(QString("play"))),this,SLOT(on_play_clicked()));
}
void Mydialog_start::on_play_clicked()
{
  accept();
}
void Mydialog_start::on_exit_clicked()
{
   reject();
}
int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  MainWindow w; 
  Mydialog_start dialog;
  dialog.exec();
  if( dialog.exec()==QDialog::Accepted)
  {
    w.show();
  }
  else
  {
    w.close();
  }
 }
maryam
  • 1,437
  • 2
  • 18
  • 40
  • 1
    Did you search at all before posting this? I actually tried editing your title to correct it, when it said that a question with the exact same title exists: http://stackoverflow.com/questions/3624733/how-to-make-a-qgraphicstextitem-clickable – Mitch Jul 04 '14 at 14:37
  • i read before but his problem different from my problem. that was not my answer!! – maryam Jul 04 '14 at 14:53
  • 1
    So if you add `text->setTextInteractionFlags(Qt::TextBrowserInteraction);`, it doesn't work? – Mitch Jul 04 '14 at 15:15

1 Answers1

1

Not quite sure whether you needed your text item to be "editable" - see Mitch's comment...

It seems that you need your item to be "clickable" - then all you need are some flags:

text->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
Thalia
  • 13,637
  • 22
  • 96
  • 190