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):
{
qDebug() << "X: " << e->x() << ", Y: " << e->y();
}
void CrosswordArea::mouseMoveEvent(QMouseEvent *e)
{
qDebug() << "X: " << e->x() << ", Y: " << e->y();
}
To copy to clipboard, switch view to plain text mode
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):
{
resize(1500,800);
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);
}
{
qDebug() << "X: " << e->x() << ", Y: " << e->y();
}
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();
}
To copy to clipboard, switch view to plain text mode
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:
gui.jpg
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
- 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!
Bookmarks