PDA

View Full Version : QGraphicScene MousePressEvent



Spitz
16th November 2007, 18:03
Hi all,


I would like to know on wich position a user clicked in the scene.
I know I have to do this with the mousePressEvent(QGrapicsSceneMouseEvent *event),
but it just aint working...

This code pops me a dialog when I click anywhere on my form, except when I click in the scene:


void GUIGame::mousePressEvent(QMouseEvent *event){

QMessageBox::about(this,"xxxx!","yyyyy!");

}


When I change this event like this:



void GUIGame::mousePressEvent(QGraphicsSceneMouseEvent *event){

QMessageBox::about(this,"xxxx!","yyyyy!");

}

it does'nt do anything.

The constructor of my mainwindow is like this:



GUIGame::GUIGame() : QMainWindow(0, Qt::WindowFlags())
{
setWindowTitle(tr("DuckBoard"));
setWindowIcon(QPixmap("logo.png"));

this->setWindowState(Qt::WindowMaximized);
setWindowTitle(tr("DuckBoard"));
setWindowIcon(QPixmap("logo.png"));
gamemenu = menuBar()->addMenu(tr("&Game"));
gamemenu->addAction(QPixmap("icons/wand.png"), tr("&New"), this, SLOT(newGame()), QKeySequence("Ctrl+N"));
gamemenu->addAction(QPixmap("icons/cancel.png"), tr("&Quit"), this, SLOT(close()), QKeySequence("Ctrl+Q"));

helpmenu = menuBar()->addMenu(tr("&Help"));
helpmenu->addAction(QPixmap("icons/rosette.png"), tr("&Credits"), this, SLOT(credits()), QKeySequence("Ctrl+C"));
helpmenu->addAction(QPixmap("icons/help.png"), tr("&Help"), this, SLOT(help()), QKeySequence("Ctrl+H"));
canvas = new QGraphicsScene;
canvas->setSceneRect(0,0,640,640);
view = new QGraphicsView(canvas, NULL);
view->setFixedSize(642,642);
view->setAlignment(Qt::AlignLeft);
gamelog = new logGUI(this);
setBord(new Bord());
m_GUIBord = new BordGUI(canvas, geefBord());
setDobbel(new Dobbel());
m_GUIDobbel = new DobbelGUI(this);
QWidget *test = new QWidget;
QHBoxLayout *layout = new QHBoxLayout;
QVBoxLayout *layout2 = new QVBoxLayout;
layout->addWidget(view);
layout2->addWidget(m_GUIDobbel);
layout2->addWidget(gamelog);
layout->addLayout(layout2);
test->setLayout(layout);
setCentralWidget(test);
int j = 1+1;
int i=0;
disableBeforePlay();
gedaan = false;
aandebeurt = 0;

connect(m_GUIDobbel->geefWerpKnop(), SIGNAL(clicked()), this, SLOT(zetPion()));
}




Does anyone knows how I can activate the mousePressEvent for my scene?

Thanks in advance!

marcel
16th November 2007, 18:40
Either subclass QGraphicsScene and override mousePressEvent or install GUIGame as event filter for the scene and catch mouse press events for the scene in there.

What you did in the first case applies only for the GuiGame widget and the second case makes no sense.

Spitz
16th November 2007, 19:18
thanks for your answer,
It worked with subclassing!

pherthyl
16th November 2007, 22:46
Hard to tell what you're doing, but for a game, you probably want to create your own QGraphicsItems and detect the mouse press event on those, instead of detecting them on the scene and trying to figure out which item was pressed...