yonibr
1st April 2012, 12:05
I am working on a tic tac toe program. At the moment, I am trying to make it so that you can click where you want the "x" to go. I currently have an event handler for a mouse click:
void QWidget::mousePressEvent ( QMouseEvent *event )
{
QPoint mousePos = mapFromGlobal(QCursor::pos());
}
My goal now is to set up 9 different areas in the main window. I then want to determine which area received the click. What would these functions look like?
The 9 areas would fall in between the squares of the tic tac toe board. The function that draws that is here:
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
// Draw Tic Tac Toe Board
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 10, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin));
painter.drawLine(80, 20, 80, 250);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 10, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin));
painter.drawLine(170, 20, 170, 250);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 10, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin));
painter.drawLine(10, 90, 250, 90);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 10, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin));
painter.drawLine(10, 180, 250, 180);
}
Thanks for the help
void QWidget::mousePressEvent ( QMouseEvent *event )
{
QPoint mousePos = mapFromGlobal(QCursor::pos());
}
My goal now is to set up 9 different areas in the main window. I then want to determine which area received the click. What would these functions look like?
The 9 areas would fall in between the squares of the tic tac toe board. The function that draws that is here:
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
// Draw Tic Tac Toe Board
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 10, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin));
painter.drawLine(80, 20, 80, 250);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 10, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin));
painter.drawLine(170, 20, 170, 250);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 10, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin));
painter.drawLine(10, 90, 250, 90);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 10, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin));
painter.drawLine(10, 180, 250, 180);
}
Thanks for the help