Results 1 to 16 of 16

Thread: Working with mouse events: How to do that with QTableWidget?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Working with mouse events: How to do that with QTableWidget?

    I really don't know anymore if is an event problem or what, because my code works "fine", i just have to click many times on a cell to get the correct value of (line, column).
    I tried to change my code of post #11, i don't know if is what anda_skoa said, but i got the same problem. Nothing changed.

    Qt Code:
    1. void Table::mousePressEvent(QMouseEvent *event) {
    2. if(event->button( ) == Qt::LeftButton) {
    3. cout << "(" << this->currentRow( ) << ", " << this->currentColumn( ) << ")" << endl;
    4. }
    5. else {
    6. QWidget::mousePressEvent( event );
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,318
    Thanks
    316
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Working with mouse events: How to do that with QTableWidget?

    The problem is likely that you never let the table widget do anything with the mouse press event when it is a left press. The events don't magically get passed up the line unless you call QTableWidget:: mousePressEvent() in addition to whatever you do with it.

    You are also calling QWidget's mousePressEvent() handler instead of the superclass of your own Table class (presumably QTableWidget) which bypasses the QTableWidget's handling of any other mousePressEvent. So it is no wonder your table widget isn't working properly - you've completely prevented it from seeing any mouse press events at all.

    So in the code you posted last, there's no correct current row or current column, because you haven't let the table widget see the mouse press event before your code swallows it and eats it.

    Try this instead:

    Qt Code:
    1. void Table::mousePressEvent(QMouseEvent *event)
    2. {
    3. QTableWidget::mousePressEvent( event );
    4. if(event->button( ) == Qt::LeftButton)
    5. {
    6. cout << "(" << this->currentRow( ) << ", " << this->currentColumn( ) << ")" << endl;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to d_stranz for this useful post:

    robgeek (13th September 2015)

  4. #3
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Working with mouse events: How to do that with QTableWidget?

    Now is working fine. Thanks a lot!

Similar Threads

  1. Replies: 2
    Last Post: 16th July 2012, 12:40
  2. Replies: 3
    Last Post: 8th October 2011, 09:46
  3. QTableWidget + QEventFilter: cannot filter mouse events
    By trallallero in forum Qt Programming
    Replies: 2
    Last Post: 8th October 2011, 08:16
  4. Replies: 2
    Last Post: 2nd April 2008, 14:19
  5. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13

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
  •  
Qt is a trademark of The Qt Company.