Results 1 to 7 of 7

Thread: need to filter Esc key for a dialog in a class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Posts
    54
    Thanks
    4
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default need to filter Esc key for a dialog in a class

    Dear friends,

    i need to filter Esc key for a particular dialog in my project not for all dialogs, i tried with overriding
    Qt Code:
    1. QDialog::KeyPressEvent(QKeyEvent *)
    To copy to clipboard, switch view to plain text mode 
    but it applies for all dialogs in my project. i need help...

    Thanks,

    regards,
    Askar

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: need to filter Esc key for a dialog in a class

    install eventFilter for the QDialog and catch the key Qt::key
    or use QAction for key and triggered to an empty slot()

    Qt Code:
    1. Dialog:: eventFilter(QObject *ob, QEvent *e)
    2. {
    3. if(e->type() == Qt::Key_Escape){
    4. printf("is it coming inside event..\n")
    5. }
    6. return QWidget::eventFilter(ob, e);
    7. }
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Sep 2009
    Posts
    54
    Thanks
    4
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: need to filter Esc key for a dialog in a class

    I cont understand how can i use ur code

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: need to filter Esc key for a dialog in a class

    see eventFilter is a virtually protected property of QObject

    see this link http://doc.trolltech.com/4.2/eventsandfilters.html

    this protected function is used to filter the particular event on the widget ex: mouse press, keypress, mouse hover

    using the event filter u can catch the key press of escape ..

    this code
    Qt Code:
    1. bool FilterObject::eventFilter(QObject *object, QEvent *event)
    2. {
    3. if (object == target && event->type() == QEvent::KeyPress) {
    4. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    5. if (keyEvent->key() == Qt::Key_escape) {
    6. // Special tab handling
    7. return true;
    8. } else
    9. return false;
    10. }
    11. return fals
    To copy to clipboard, switch view to plain text mode 
    will filter the escape key input of the QDialog
    "Behind every great fortune lies a crime" - Balzac

  5. #5
    Join Date
    Sep 2009
    Posts
    54
    Thanks
    4
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: need to filter Esc key for a dialog in a class

    I TRIED WITH THIS CODE
    Qt Code:
    1. bool QDialog::eventFilter(QObject *object, QEvent *event)
    2. {
    3. if (event->type() == QEvent::KeyPress) {
    4. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    5. if (keyEvent->key() == Qt::Key_Escape) {
    6. keyEvent->ignore();
    7. // Special tab handling
    8. return true;
    9. } else
    10. return false;
    11. }
    12. return false;
    13. }
    To copy to clipboard, switch view to plain text mode 
    BUT , THE CONDITION
    Qt Code:
    1. if (event->type() == QEvent::KeyPress)
    To copy to clipboard, switch view to plain text mode 
    FAILS FOR Esc KEYY PRESS\

  6. #6
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: need to filter Esc key for a dialog in a class

    did u give installEventFilter(this) to the QDialog ... can u show us the code u written
    "Behind every great fortune lies a crime" - Balzac

  7. #7
    Join Date
    Sep 2009
    Posts
    54
    Thanks
    4
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: need to filter Esc key for a dialog in a class

    sure here is my code


    Qt Code:
    1. void CommonFunctions::progressbar(QString Dialogtext)
    2. {
    3. layout = new QVBoxLayout;
    4. l = new QLabel( Dialogtext);
    5. l->setWordWrap(true);
    6. progress = new QProgressBar();
    7. progress->setWindowModality(Qt::WindowModal);
    8. progress->setRange(0,10);
    9. layout->addWidget(l);
    10. layout->addWidget(progress,Qt::AlignCenter);
    11. win->setMaximumWidth(320);
    12. win->setModal(true);
    13. win->setLayout(layout);
    14. win->setWindowTitle("title");
    15. installEventFilter(win);
    16. win->setWindowFlags(Qt::Dialog|Qt::WindowMaximizeButtonHint);
    17. win->show();
    18. }
    19.  
    20. bool QDialog::eventFilter(QObject *object, QEvent *event)
    21. {
    22. if (event->type() == QEvent::KeyPress) {
    23. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    24. if (keyEvent->key() == Qt::Key_Escape) {
    25. keyEvent->ignore();
    26. return true;
    27. } else
    28. return false;
    29. }
    30. return false;
    31. }
    To copy to clipboard, switch view to plain text mode 

    i had checked this code by setting a breakpoint in eventFilter(), while pressing Esc key debugger stops at break point and checks the condition in 22 line of the above code , but it fails & returns false

    where im getting wrong can u help me....
    Last edited by Askar; 11th December 2009 at 07:36.

Similar Threads

  1. QT Class GUI Interactions??
    By mikey33 in forum Newbie
    Replies: 3
    Last Post: 19th November 2009, 02:01
  2. Issue with Modelless dialog on Mac
    By Satyanarayana Chebrolu in forum Qt Programming
    Replies: 0
    Last Post: 24th February 2009, 10:10
  3. Problem with QDialog class
    By sudheer168 in forum Qt Programming
    Replies: 4
    Last Post: 23rd December 2008, 09:03
  4. Use QWidget derived class in Dialog
    By qtneuling in forum Qt Tools
    Replies: 2
    Last Post: 17th May 2008, 23:29
  5. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57

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
  •  
Qt is a trademark of The Qt Company.