Results 1 to 8 of 8

Thread: Mouse events for different tables: How to handle?

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

    Default Mouse events for different tables: How to handle?

    Hello!

    I'm making a program which has many tables, lets say for this post, two tables. So, with the table_1 i want to capture mouse events and do something when left button is pressed and other thing when right button is pressed. To do that i created a class called Table derived from QTableWidget and reimplemented mousepressevent(). I created using Desing a table and promoted it to Table. For this table, everything is working fine.

    But now, i want to do the same thing with table_2, but i don't know how to select the context, when i click on the table_1 do this, when i click in the table_2, do that. I wonder if i have to create another class, like Table2, rewrite mousepressevent() and promote table_2 to Table2.

    What do you think? What is the best way for doing that?

  2. #2
    Join Date
    Jun 2007
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Lightbulb Re: Mouse events for different tables: How to handle?

    your context (the widget table_1 or table_2) should be in the "this" pointer in your reimplementation of mousePressEvent()

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

    robgeek (17th September 2015)

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

    Default Re: Mouse events for different tables: How to handle?

    So, i have to promote each table to different classes in which i have different mousePressEvent() implementation?
    For instance:
    Qt Code:
    1. table_1 promoted to Table1(here i have an mousePressEvent())
    2. table_2 promoted to Table2(here i have an mousePressEvent())
    3. table_3 promoted to Table3(here i have an mousePressEvent())
    To copy to clipboard, switch view to plain text mode 

    Here my basic implementation of mousePressEvent().
    Qt Code:
    1. void Table::mousePressEvent(QMouseEvent *event) {
    2. QTableWidget::mousePressEvent( event );
    3.  
    4. if(event->button( ) == Qt::LeftButton) {
    5. this->currentRow( );
    6. this->currentColumn( );
    7. // Do something with this table.
    8. //"this" is the table made with Desing promoted to Table.
    9. }
    10. else if(event->button( ) == Qt::RightButton) {
    11. this->currentRow( );
    12. this->currentColumn( );
    13. // Do other thing with this table.
    14. }
    15.  
    16. // And if i click on other table?
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by robgeek; 17th September 2015 at 16:04.

  5. #4
    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: Mouse events for different tables: How to handle?

    If the mouse handling is different, then yes, a second class will give you the best separation.

    Cheers,
    _

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

    robgeek (17th September 2015)

  7. #5
    Join Date
    Jun 2007
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Mouse events for different tables: How to handle?

    now, you only need 1 class. your ONE code for mousepressevent can then check if the "this" object is table1 or table2 and do different things for these cases

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

    Default Re: Mouse events for different tables: How to handle?

    Quote Originally Posted by anda_skoa View Post
    If the mouse handling is different, then yes, a second class will give you the best separation.

    Cheers,
    _
    In my real case, i have four tables, each one with different behavior when i click on them, then the best way for doing that is creating four classes, like my example in post #3?

  9. #7
    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: Mouse events for different tables: How to handle?

    I would say so yes.
    Alternatively four differen event filter classes.

    Or one view subclass that delegates click handling to an object of special purpose class and four implementatiin of that interface.

    Or one view subclass that emits a signal and handling the signal of the different views in different slots.

    Zandru is of course right that in theory that can also be achieved by somehow identifying each instance of the same class, e.g. via the objectName or something set from outside, but this can get unwieldy quite soon.

    Cheers,
    _

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

    robgeek (17th September 2015)

  11. #8
    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: Mouse events for different tables: How to handle?

    In cases like this, I will implement a single class and add an enum or some other type of flag I set when I create the instance that allows my code to distinguish which version of the class a given instance is. If all you need to do is change some small bit of behavior, then using this approach with a switch() based on the enum value is often the easiest. I like this style because it puts all the code in the same place instead of scattering it over multiple classes or methods.

    On the other hand, if there are many differences between versions of the widget, then doing as anda_skoa suggests is cleaner and more maintainable.

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

    robgeek (17th September 2015)

Similar Threads

  1. Handle KeyRelease Events inside a while loop
    By u2bpavankumar in forum Qt Programming
    Replies: 2
    Last Post: 2nd July 2015, 12:47
  2. Replies: 3
    Last Post: 12th May 2010, 13:11
  3. Replies: 5
    Last Post: 27th April 2010, 11:04
  4. Unable to handle WindowStateChange events
    By TheRonin in forum Qt Programming
    Replies: 1
    Last Post: 6th March 2008, 11:20
  5. QPushButton:: Handle right mouse button events. Easyway?
    By Harvey West in forum Qt Programming
    Replies: 6
    Last Post: 28th February 2007, 16: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.