Results 1 to 7 of 7

Thread: why QListWidget can't get QEvent::MouseButtonPress?

  1. #1
    Join Date
    Apr 2008
    Posts
    44
    Thanks
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Red face why QListWidget can't get QEvent::MouseButtonPress?


    There are three different kinds of widgets in my project: List(QListWidget), Label(QLabel) and some pushbuttons.
    And i don't want the List receive any mouse event. thanks for any advice.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: why QListWidget can't get QEvent::MouseButtonPress?

    install event filter for your listWidget and ignore all mouse events.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    to_guliang (13th May 2009)

  4. #3
    Join Date
    Apr 2008
    Posts
    44
    Thanks
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: why QListWidget can't get QEvent::MouseButtonPress?

    I have tried two methods, but don't succeed.
    the one of them:
    Register the List with the target object by calling installEventFilter() on the target:
    Qt Code:
    1. list->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

    Then handle the mouse events in the monitor's eventFilter() function.
    Qt Code:
    1. bool ProgramList::eventFilter(QObject *target, QEvent *event)
    2. {
    3. if(target == list)
    4. { printf("\nevent->type() = %d\n",event->type());
    5. if(event->type() == QEvent::MouseButtonPress)
    6. {
    7. event->ignore();
    8. return true;
    9. }
    10. }
    11. return QWidget::eventFilter(target, event);
    12. }
    To copy to clipboard, switch view to plain text mode 

    But the event type have't "MouseButtonPress" at all when i click the List.
    but if i call installEventFilter() for the label or the pushbutton, there is "MouseButtonPress" .
    i don't know why, please help me. thanks very much!

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: why QListWidget can't get QEvent::MouseButtonPress?

    try this
    Qt Code:
    1. list->viewport()->installEventFilter(this);
    2. ....
    3. bool ProgramList::eventFilter(QObject *target, QEvent *event)
    4. {
    5. if(target == list->viewport())
    6. { printf("\nevent->type() = %d\n",event->type());
    7. if(event->type() == QEvent::MouseButtonPress)
    8. {
    9. event->ignore();
    10. return true;
    11. }
    12. }
    13. return QWidget::eventFilter(target, event);
    14. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    to_guliang (13th May 2009)

  7. #5
    Join Date
    Apr 2008
    Posts
    44
    Thanks
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: why QListWidget can't get QEvent::MouseButtonPress?

    the other method:
    I Reimplement mousePressEvent(), the code as following:
    the code in .h:
    Qt Code:
    1. void mousePressEvent(QMouseEvent *event);
    To copy to clipboard, switch view to plain text mode 
    the code in .cpp:
    Qt Code:
    1. void ProgramList::mousePressEvent(QMouseEvent *event)
    2. {
    3. QWidget::mousePressEvent(event);
    4. if( )//the condition:the position is in the List
    5. {
    6. event->ignore();
    7. }
    8. else event->accept();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Then i find the function "mousePressEvent" don't excute when i click the list ; but it excute when i click the Label.
    There must be something wrong in my program. it's so nice for you to point it out and give me the correct code. thanks very much.

  8. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: why QListWidget can't get QEvent::MouseButtonPress?

    installing event filter on a litwidget's viewport should fix you problem.
    use my example in prev.post.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. The following user says thank you to spirit for this useful post:

    to_guliang (13th May 2009)

  10. #7
    Join Date
    Apr 2008
    Posts
    44
    Thanks
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: why QListWidget can't get QEvent::MouseButtonPress?


    thanks very much!!
    it works!

Similar Threads

  1. QListWidget transparent background or pixmap
    By wdezell in forum Qt Programming
    Replies: 6
    Last Post: 6th March 2009, 17:53
  2. Segmentation Faul using addItem (QListWidget)
    By gnusar in forum Qt Programming
    Replies: 3
    Last Post: 8th November 2008, 09:27
  3. how to sync a QTreeWidget and a QListWidget?
    By zl2k in forum Qt Programming
    Replies: 2
    Last Post: 5th September 2008, 20:55
  4. QListWidget Problem
    By pmabie in forum Qt Programming
    Replies: 1
    Last Post: 7th October 2007, 06:52
  5. Replies: 13
    Last Post: 15th December 2006, 11:52

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.