Results 1 to 4 of 4

Thread: QPushButton on Shift+Click?

  1. #1
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question QPushButton on Shift+Click?

    Hi All,

    I want to implement a QPushButton in such a way that it do something when user click it
    for which I can use clicked() signal but I also want that when user click while shift button is pressed then it will do something else.

    How can I do it?

    Thanks
    Vishal Chauhan

  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: QPushButton on Shift+Click?

    you can subclass QPushButton and reimplement keyPressEvent or you can install event fillter for your button and process QKeyEvent.

  3. #3
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QPushButton on Shift+Click?

    Thanks for reply.

    Can you give some sample code for doing it.

  4. #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: QPushButton on Shift+Click?

    sorry, not QKeyEvent, but QMouseEvent
    1. subclassing
    Qt Code:
    1. void MyPushButton::mousePressEvent(QMouseEvent *e)
    2. {
    3. if (e->modifiers() == Qt::ShiftModifier) {
    4. //do what you need
    5. return;
    6. }
    7. QPushButton::mousePressEvent(e);
    8. }
    To copy to clipboard, switch view to plain text mode 
    2. installFilter
    Qt Code:
    1. ...
    2. pushButton->installEventFilter(this);
    3. ...
    4.  
    5. bool MyWidget::eventFilter(QObject *obj, QEvent *event)
    6. {
    7. if (obj == pushButton && event->type() == QEvent::MouseButtonPress) {
    8. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
    9. if (event->modifiers() == Qt::ShiftModifier) {
    10. //do what you need
    11. return true;
    12. }
    13. }
    14. return QWidget::eventFilter(obj, event);
    15. }
    To copy to clipboard, switch view to plain text mode 

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

    vishal.chauhan (9th September 2008)

Similar Threads

  1. Double Click Capturing
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2011, 14:12
  2. QGraphicsScene Click / Double Click
    By philentropist in forum Qt Programming
    Replies: 1
    Last Post: 9th February 2007, 04:32
  3. Replies: 3
    Last Post: 26th September 2006, 12:16

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.