Results 1 to 16 of 16

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

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

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

    Hello!

    I'm trying to make a QTableWidget shows me different things when i click(one click) on a cell with the right button and when i click with left button. I'm using QtCreator-3.4.2 and when i click on QTableWidget and "Go To Slot" i can use only "cellClicked(int, int)". It works when i click with left button, but i don't know how to do the same thing with the right button. I know there is QMouseEvent, but i don't know how to use now because i'm already using "cellClicked".

    How can i do that?
    Thanks!

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    You can implement mousePressEvent for table widget & have a flag there to get to know whether it is left click or right click event.

    Qt Code:
    1. void tablewidget::mousePressEvent(QMouseEvent *event)
    2. {
    3. if(event->button() == Qt::LeftButton)
    4. m_isLeftClick = true; // bool m_isLeftClick; is class member
    5. else if (event->button() == Qt::RightButton)
    6. m_isLeftClick = false;
    7. }
    To copy to clipboard, switch view to plain text mode 

    then in your slot check whether it is left click or right click
    Thanks :-)

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

    robgeek (10th September 2015)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

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

    How can i do that?
    prasad_N's solution requires you to derive your own class from QTableWidget and override mousePressEvent. The alternative is to use the out-of-the-box QTableWidget and install an event filter on it that you monitor from some other class (like your main window that owns the table widget).

    You probably don't want to use only mousePressEvent() for this anyway. I would use the mousePressEvent / mouseReleaseEvent as a pair, for the following reason: If the user presses the mouse in your table widget, it automatically triggers your action. This means there is no way for the user to say, oops, I didn't mean to do that - I clicked accidently. In most other mouse click scenarios, the action isn't triggered unless the mouse press and mouse release both occur in the same window. Mouse press sets a flag, mouse release does the action if and only if the flag is set. This avoids things like clicking in the window and then moving out of it to release (a common way to "escape" out of doing something you didn't intend) or clicking outside the window and moving into it to release. If you only watched for one or the other event, your app would almost certainly have unintended consequences where something is performed that the user didn't want.

    Be careful using right mouse press events if you also use context menus.

  5. The following 2 users say thank you to d_stranz for this useful post:

    prasad_N (11th September 2015), robgeek (10th September 2015)

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

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

    Thanks for your help! I tried to make a test program with a qtablewidget, but did not work! I created an UI with a QWidget a
    I created a derived class from QTableWidget and override mousePressedEvent. When i click on the table nothing happens. I believe this is happening because i didn't call mousePressedEvent anywhere, but i don't know where to call it.

    Take a look at the code, please. What is wrong?

    Qt Code:
    1. #ifndef TESTEVT_H
    2. #define TESTEVT_H
    3.  
    4. #include <iostream>
    5. #include <QMouseEvent>
    6. #include <QTableWidget>
    7.  
    8. using namespace std;
    9.  
    10. class TestEvt : public QTableWidget {
    11.  
    12. public:
    13. TestEvt( );
    14. virtual void mousePressEvent(QMouseEvent *event);
    15. };
    16.  
    17. #endif // TESTEVT_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "testevt.h"
    2.  
    3. TestEvt::TestEvt( ) {
    4. cout << "Object created." << endl;
    5. }
    6.  
    7. void TestEvt::mousePressEvent(QMouseEvent *event) {
    8. if(event->button( ) == Qt::LeftButton)
    9. cout << "Left Button Clicked." << endl;
    10. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow {
    11. Q_OBJECT
    12.  
    13. public:
    14. explicit MainWindow(QWidget *parent = 0);
    15. ~MainWindow( );
    16.  
    17. private:
    18. Ui::MainWindow *ui;
    19. };
    20.  
    21. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "testevt.h"
    3. #include "ui_mainwindow.h"
    4.  
    5. MainWindow::MainWindow(QWidget *parent) : QMainWindow( parent ), ui(new Ui::MainWindow) {
    6. ui->setupUi( this );
    7. TestEvt evt;
    8. }
    9.  
    10. MainWindow::~MainWindow( ) {
    11. delete ui;
    12. }
    To copy to clipboard, switch view to plain text mode 

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    You need to pass the even on to the implementation that QTableWidget was using before you overwrote it, i.e. calling the base class implementation either before or after you do you custom handling.

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    robgeek (11th September 2015)

  9. #6
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by robgeek View Post
    When i click on the table nothing happens. I believe this is happening because i didn't call mousePressedEvent anywhere
    first of all you can n't see table, because you are creating table on stack. create it on heap (TestEvt* eve = new TestEvt (this)) or have it as class member.

    Quote Originally Posted by robgeek View Post
    but i don't know where to call it.
    You don't need to cal this explicitly, when you click on table mousePressEvent will be called automatically.

    changes I can suggest you is:

    Qt Code:
    1. class TestEvt : public QTableWidget {
    2.  
    3. public:
    4. TestEvt(QWidget* parent ):QTableWidget(parent) {}
    5. virtual void mousePressEvent(QMouseEvent *event) {
    6. if(event->button( ) == Qt::LeftButton)
    7. qDebug() << "Left Button Clicked.";
    8. QTableWidget::mousePressEvent(event); // call base class implementation
    9. }
    10. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. TestEvt* eve = new TestEvt(this); //create instance on heap
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 


    Note: you may not see table widget fully here, please use layout in order to fit table widget in you main window.


    Added after 41 minutes:


    Quote Originally Posted by d_stranz View Post
    I would use the mousePressEvent / mouseReleaseEvent as a pair, for the following reason: If the user presses the mouse in your table widget, it automatically triggers your action. This means there is no way for the user to say, oops, I didn't mean to do that - I clicked accidently. In most other mouse click scenarios, the action isn't triggered unless the mouse press and mouse release both occur in the same window. Mouse press sets a flag, mouse release does the action if and only if the flag is set. This avoids things like clicking in the window and then moving out of it to release (a common way to "escape" out of doing something you didn't intend) or clicking outside the window and moving into it to release. If you only watched for one or the other event, your app would almost certainly have unintended consequences where something is performed that the user didn't want.

    Be careful using right mouse press events if you also use context menus.
    informative, Thanks a lot.
    Last edited by prasad_N; 11th September 2015 at 07:39.
    Thanks :-)

  10. The following user says thank you to prasad_N for this useful post:

    robgeek (11th September 2015)

  11. #7
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

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

    I did what you said and worked fine. The only problem was the table was created above the one i created using "Design". The new one i don't know how to configure the size and position using Design, i believe i have to do that in TestEvt class since she is derived from QTableWidget.

    Yesterday i tried one thing i saw in a video from youtube. I tried to do the same thing, but instead creating TestEvt on heap i created on stack "TestEvt evt;" and promoted the table i created with Desing to "TestEvt". Yesterday worked fine, but today i tried it again, and i'm getting the following error message:
    /usr/include/qt/QtCore/qobject.h:135: error: 'void QObject::setObjectName(const QString&)' is inaccessible
    void setObjectName(const QString &name);
    ^

    /home/rob/Programming/C-C++/Linux/Qt/build-Test-Desktop-Debug/ui_mainwindow.h:37: error: within this context
    histTable->setObjectName(QStringLiteral("histTable"));
    ^

    /home/rob/Programming/C-C++/Linux/Qt/build-Test-Desktop-Debug/ui_mainwindow.h:37: error: 'QObject' is not an accessible base of 'TestEvt'

    /usr/include/qt/QtWidgets/qwidget.h:481: error: 'void QWidget::setGeometry(const QRect&)' is inaccessible
    void setGeometry(const QRect &);
    ^

    /home/rob/Programming/C-C++/Linux/Qt/build-Test-Desktop-Debug/ui_mainwindow.h:38: error: within this context
    histTable->setGeometry(QRect(10, 10, 381, 281));
    ^

    /home/rob/Programming/C-C++/Linux/Qt/build-Test-Desktop-Debug/ui_mainwindow.h:38: error: 'QWidget' is not an accessible base of 'TestEvt'

  12. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by robgeek View Post
    I tried to do the same thing, but instead creating TestEvt on heap i created on stack "TestEvt evt;" and promoted the table i created with Desing to "TestEvt".
    When you promote a widget in designer you don't have to create an instance yourself at all.
    The code generated from designer's ui file will contain an instantiation of the promoted class.

    Quote Originally Posted by robgeek View Post
    Yesterday worked fine, but today i tried it again, and i'm getting the following error message:
    That sounds like you are no longer publically inheriting from QTableView.

    Cheers,
    _

  13. The following user says thank you to anda_skoa for this useful post:

    robgeek (11th September 2015)

  14. #9
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

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

    That sounds like you are no longer publically inheriting from QTableView.
    Thanks! I put "public" before QTableWidget and now everything is working fine!
    Qt Code:
    1. class TestEvt : public QTableWidget {...
    To copy to clipboard, switch view to plain text mode 

    Which one do you thing is more correct? Promote my table or do like prasad_N did.

  15. #10
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

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

    Now i'm getting another weird problem! Sorry bother you, guys! Is another problem, but with my QTableWidget, that's why i did'nt create another topic.

    I'm trying to get the row and column number when i click in a certain cell, but to see the correct output, i have to click in the same line ten times, maybe more, then the output changes to the correct row number. Why this is happening? Whith the following code, i'm trying to get e correct line and column of a certain cell when click on it.

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

    Considering i promoted my QTableWidget to "TestEvt" which is derived from QTableWidget. I did that to reimplement "mousePressEvent()" in "TestEvt" class.

  16. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by robgeek View Post
    Which one do you thing is more correct? Promote my table or do like prasad_N did.
    Both are equally correct, the promotion option is just more convenient.

    Quote Originally Posted by robgeek View Post
    Qt Code:
    1. void Table::mousePressEvent(QMouseEvent *event) {
    2. if(event->button() == Qt::LeftButton) {
    3. cout << "(" << this->currentRow( ) << ", " << this->currentColumn( ) << ")" << endl;
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 
    If that is your actual code then you are forgetting to call the base class implementation of mousePressEvent().

    Cheers,
    _

  17. #12
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

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

    If that is your actual code then you are forgetting to call the base class implementation of mousePressEvent().
    Sorry, i didn't understand! All code examples i saw look alike my code. How can i do that?

    Here, my "guide".

  18. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

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

    Here, my "guide".
    If you are relying on that as your "guide", no wonder you are lost in the wilderness. In just one example I looked at, I saw at least three errors.

    This should be your guide instead. Pay close attention to what it says about overriding vs. extending event behavior as well as what it says about return values for the basic QObject::event() method.

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

    robgeek (13th September 2015)

  20. #14
    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 

  21. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 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 

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

    robgeek (13th September 2015)

  23. #16
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.