Results 1 to 6 of 6

Thread: Trigger QAction by 'Key_Escape'

  1. #1
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Question Trigger QAction by 'Key_Escape'

    Hi all,
    I'm trying to install a global action for a QWidget that has to be triggered when 'Esc' is pressed.

    I try this code:
    Qt Code:
    1. vClose=new QAction(this);
    2. vClose->setShortcut(Qt::Key_Escape);
    3. connect(vClose,SIGNAL(triggered()),this,SLOT(quickClose()));
    4. addAction(vClose); // I'm in a QWidget subclass
    To copy to clipboard, switch view to plain text mode 
    but it doesn't work. The function quickClose() is not called.
    Instead if I put another key (such as Qt::Key_F4 or Qt::Key_F7, etc...) it gets called normally.
    I could understand that Qt::Key_Escape and Qt::Key_Enter behave in a their own way...
    So, what is my noobie mistake?
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  2. #2
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trigger QAction by 'Key_Escape'

    It should work. Are you inside custom widget inherited directly from QWidget or some other class like QDialog? Esc key might already be being used by that.

  3. #3
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Trigger QAction by 'Key_Escape'

    Quote Originally Posted by yogeshm02 View Post
    It should work. Are you inside custom widget inherited directly from QWidget or some other class like QDialog? Esc key might already be being used by that.
    No, before was inherited from QDialog, but since there Esc forces the dialog to close (as expected by QDialog), I changed its ancestor and now the class is inherited directly from QWidget:
    Qt Code:
    1. class frmcontact:public QWidget
    To copy to clipboard, switch view to plain text mode 
    The fact that other keys work but not Esc and Enter is strange...
    thank you for the suggestion, any other idea or I have to try reimplementing onKeyPress (such a bad thing here...)?
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  4. #4
    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: Trigger QAction by 'Key_Escape'

    You should be using QShortcut:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char* argv[])
    4. {
    5. QApplication app(argc, argv);
    6. new QShortcut(Qt::Key_Escape, &w, SLOT(close()));
    7. w.show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    Raccoon29 (28th March 2008)

  6. #5
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Trigger QAction by 'Key_Escape'

    Quote Originally Posted by jpn View Post
    You should be using QShortcut:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char* argv[])
    4. {
    5. QApplication app(argc, argv);
    6. new QShortcut(Qt::Key_Escape, &w, SLOT(close()));
    7. w.show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 
    Thank you, even if I discovered that my solution worked too (so falled for some internal troubles that I'm going to investigate about), using QShortcut seems much more clear for this job.
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  7. #6
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Trigger QAction by 'Key_Escape'

    Ok, I solved it.
    All has been caused by a shortcut that I set in QtDesigner... and I forgot...

    So if QActions are not triggered, it is most because it encountered conflicts when triggering.
    So take a look to all QAction implementations

    Hope to be usefull for someone else.
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

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.