Results 1 to 3 of 3

Thread: mouseMoveEvent doesn't respond properly

  1. #1
    Join Date
    Nov 2013
    Location
    Kungsbacka, Sweden
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default mouseMoveEvent doesn't respond properly

    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):


    Qt Code:
    1. void CrosswordArea::mouseMoveEvent(QMouseEvent *e)
    2. {
    3. qDebug() << "X: " << e->x() << ", Y: " << e->y();
    4. }
    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):

    Qt Code:
    1. MyWindow::MyWindow(QString str)
    2. {
    3. resize(1500,800);
    4.  
    5. QWidget *bigContainer = new QWidget;
    6. QVBoxLayout *contLayout = new QVBoxLayout;
    7.  
    8. QScrollArea *scrollArea = new QScrollArea();
    9. CrosswordArea *cwArea = new CrosswordArea("#1");
    10.  
    11. ButtonArea *btnArea = new ButtonArea(cwArea);
    12.  
    13. scrollArea->setWidget(cwArea);
    14.  
    15. contLayout->addWidget(btnArea);
    16. contLayout->addWidget(scrollArea);
    17.  
    18. bigContainer->setLayout(contLayout);
    19.  
    20. setCentralWidget(bigContainer);
    21. setMouseTracking(true);
    22. centralWidget()->setMouseTracking(true);
    23. }
    24.  
    25. void MyWindow::mouseMoveEvent(QMouseEvent *e)
    26. {
    27. qDebug() << "X: " << e->x() << ", Y: " << e->y();
    28. }
    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
          • 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!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: mouseMoveEvent doesn't respond properly

    I think you posting is missing the output.

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

  3. #3
    Join Date
    Nov 2013
    Location
    Kungsbacka, Sweden
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: mouseMoveEvent doesn't respond properly

    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!

Similar Threads

  1. Replies: 6
    Last Post: 13th July 2011, 20:10
  2. Graphics scene doesn't repaint properly
    By blooglet in forum Qt Programming
    Replies: 5
    Last Post: 16th April 2011, 16:48
  3. Http request doesn't respond when the host is local
    By dineshkumar in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2011, 14:09
  4. Delegate doesn't paint properly
    By woodtluk in forum Qt Programming
    Replies: 4
    Last Post: 11th August 2010, 12:43
  5. QGraphicsScene doesn't respond to mousePressEvent
    By Affenbrotbaum in forum Qt Programming
    Replies: 1
    Last Post: 21st January 2010, 20:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.