PDA

View Full Version : mouseMoveEvent doesn't respond properly



Ponnytail
3rd November 2013, 13:08
I'm trying to create a crossword tool but I've ran into a problem with mouse moving events.
What I want to do is to let the user mark several squares in the crossword grid by first marking the starter square and then drag the mouse to expand the marked area. (for starters we can ignore the left mouse button)

My crossword grid is a subclass of QWidget and the mousePressEvent works like a charm for marking single squares. What seemed logic to me was to also track mouse movements in this widget. I added "setMouseTracking(true)" to the constructor and simply implemented the mouseMoveEvent function as follows (having declared it in my header file):



void CrosswordArea::mouseMoveEvent(QMouseEvent *e)
{
qDebug() << "X: " << e->x() << ", Y: " << e->y();
}

When I moved the mouse around, I didn't get any output.

When I searched around the net for similar problems I found that usually you add mouse tracking to the central widget of your main window. So I tried the following in my main window (which subclasses QMainWindow):


MyWindow::MyWindow(QString str)
{
resize(1500,800);

QWidget *bigContainer = new QWidget;
QVBoxLayout *contLayout = new QVBoxLayout;

QScrollArea *scrollArea = new QScrollArea();
CrosswordArea *cwArea = new CrosswordArea("#1");

ButtonArea *btnArea = new ButtonArea(cwArea);

scrollArea->setWidget(cwArea);

contLayout->addWidget(btnArea);
contLayout->addWidget(scrollArea);

bigContainer->setLayout(contLayout);

setCentralWidget(bigContainer);
setMouseTracking(true);
centralWidget()->setMouseTracking(true);
}

void MyWindow::mouseMoveEvent(QMouseEvent *e)
{
qDebug() << "X: " << e->x() << ", Y: " << e->y();
}

This does track mouse movement but not the way I want it to. In the following picture, moving the mouse around the red area gives output:

9748

I have no idea why my program doesn't track mouse movement within the grid area. If it's of any help my GUI has the following structure:



MyWindow - QMainWindow

bigContainer - QWidget

buttonArea - QWidget

2xQPushButton


scrollArea - QScrollArea

CrosswordArea - QWidget


QGridLayout 24x40 containing Tile objects (subclass of QWidget)










So my question is simple but tricky. How can I allow and track mouse movement only in my crossword area?

Any kind of help is much appreciated.

Thanks!

anda_skoa
3rd November 2013, 14:56
I think you posting is missing the output.

Anyway, I think you would be better off using QGraphicsView instead of a grid of widgets.

Ponnytail
4th November 2013, 19:45
Thanks for your answer anda_skoa. A gridlayout is perfect for my application, no changes there.
I solved the problem myself. I had to listen for mouse movement in my tiles and then "tunnel" the event up to the crossword widget.

Problem solved!