Results 1 to 4 of 4

Thread: How to catch MouseBottonPress for QListWidget

  1. #1
    Join Date
    Oct 2006
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to catch MouseBottonPress for QListWidget

    QEventFilter doesn't handle QEvent:MouseButtonPress for QListWidget but reacts to QEvent::KeyPress .

    Below is a sample code :
    Qt Code:
    1. //////////////////////////////HPP File /////////////////////////////////////
    2. class key_press_filter: public QObject
    3. {
    4. Q_OBJECT
    5. protected:
    6. bool eventFilter(QObject *obj, QEvent *event);
    7. };
    8.  
    9. class my_list: public QListWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. my_list(QWidget* p = 0);
    14. virtual ~my_list();
    15. };
    16. ////////////////////////////////////////////////////////////////////////////////
    17. //////////////////////////////CPP File /////////////////////////////////////
    18.  
    19. my_list::my_list(QWidget* p): QListWidget(p)
    20. {
    21. setSelectionMode(QAbstractItemView::MultiSelection);
    22. addItem("First");
    23. addItem("Second");
    24. addItem("Third");
    25. key_press_filter* kf = new key_press_filter();
    26. installEventFilter(kf);
    27.  
    28. }
    29. bool key_press_filter::eventFilter(QObject *o, QEvent *e)
    30. {
    31. my_list* f = static_cast<my_list*>(o);
    32. if (e->type() == QEvent::MouseButtonPress||
    33. e->type() == QEvent::KeyPress) {
    34. QMouseEvent *m = static_cast<QMouseEvent*>(e);
    35. f->addItem("aaaaaaaaaaaa");
    36. } else {
    37. // standard event processing
    38. return QObject::eventFilter(o, e);
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 


    Plz help me to catch RBM & LBM for QListWidget
    Last edited by wysota; 2nd October 2006 at 11:54. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to catch MouseBottonPress for QListWidget

    Try installing the event filter on the view port.
    Qt Code:
    1. installEventFilter(kf);
    2. viewport()->installEventFilter(kf);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following 2 users say thank you to jpn for this useful post:

    Levon Nikoghosyan (2nd October 2006), Trasmeister (2nd October 2006)

  4. #3
    Join Date
    Sep 2006
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to catch MouseBottonPress for QListWidget

    Was wrestling with the same problem with QTableWidget this morning! Thanks!

  5. #4
    Join Date
    Sep 2006
    Posts
    10
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to catch MouseBottonPress for QListWidget

    // OBJECT_NAME means, your instance name of the looking object
    // for example:- QTextEdit* sampleEdit = new QTextEdit()
    // watched == sampleEdit

    if( ( watched == OBJECT_NAME ) && ( e->type()==QEvent::MouseButtonPress ) )
    {
    QMouseEvent *mousePress = (QMouseEvent*)e;

    if ( (mousePress->button() == Qt::LeftButton) || (mousePress->button() == Qt::LeftButton) )
    {
    // do your code //
    return true;
    }
    }

    I think the above code will help you regarding MOUSE PRESS EVENT with QListWidget.

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.