Results 1 to 17 of 17

Thread: Shortcut

  1. #1
    Join Date
    May 2007
    Posts
    37
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Shortcut

    I want this:

    When a user pressed enter, the window closes and there will be activated a slot. I have this code:

    Qt Code:
    1. QLabel *lblUserName = new QLabel(tr("Mailadres:"));
    2. QLabel *lblPass = new QLabel(tr("Wachtwoord:"));
    3. QLabel *lblLogin = new QLabel(tr("Aanmelden:"));
    4.  
    5. mail = new QLineEdit;
    6.  
    7. pass = new QLineEdit;
    8. pass->setEchoMode(QLineEdit::Password);
    9.  
    10. QPushButton *blogin = new QPushButton(tr("&Aanmelden"));
    11.  
    12. QGridLayout *lay_ok = new QGridLayout;
    13. lay_ok->addWidget(lblUserName,0,0);
    14. lay_ok->addWidget(mail,1,0);
    15. lay_ok->addWidget(lblPass,2,0);
    16. lay_ok->addWidget(pass,3,0);
    17. lay_ok->addWidget(lblLogin,4,0);
    18. lay_ok->addWidget(blogin,5,0);
    19. setLayout(lay_ok);
    20.  
    21. setMinimumSize(250,175);
    22. setMaximumSize(250,175);
    23.  
    24. QAction *action = new QAction("&Aanmelden",this);
    25. action->setShortcut(QKeySequence(Qt::Key_Enter));
    26.  
    27. connect(action,SIGNAL(triggered()),SLOT(close()));
    28. connect(action,SIGNAL(triggered()),SLOT(startLogin()));
    29.  
    30. connect(blogin, SIGNAL(clicked()), SLOT(close()));
    31. connect(blogin, SIGNAL(clicked()), SLOT(startLogin()));
    To copy to clipboard, switch view to plain text mode 

    But this doesn't work. The clicked() signals works good (when the signal is "blogin"), the triggered() signals (sender is "action") doesn't work.
    Using Qt version 4.3.0

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shortcut

    You have to attach that action to some widget to make it work.
    You can use QWidget::addAction;

    For example:
    Qt Code:
    1. blogin->addAction( action );
    To copy to clipboard, switch view to plain text mode 


    Regards

  3. #3
    Join Date
    May 2007
    Posts
    37
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Shortcut

    I've changed it to:

    Qt Code:
    1. QAction *action = new QAction("&Aanmelden",this);
    2. action->setShortcut(QKeySequence(Qt::Key_Enter));
    3. blogin->addAction(action);
    4.  
    5. connect(action,SIGNAL(triggered()),SLOT(close()));
    6. connect(action,SIGNAL(triggered()),SLOT(startLogin()));
    To copy to clipboard, switch view to plain text mode 

    But it doesn't work. Pressing enter doesn't work.
    Using Qt version 4.3.0

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shortcut

    Have you tried setting other shortcuts? For example something with a modifier: CTRL + X.
    QAbstractButton also has a setShortcut() method. Maybe you have better luck with this one.

    Also, tale a look at QAction::setShortcutContext.

    The problem is that enter is also used to "click" a button, when it has the focus. So be careful, because it could emit a clicked event, therefore calling your slot twice.

    Regards

  5. #5
    Join Date
    May 2007
    Posts
    37
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Shortcut

    1) This works:

    Qt Code:
    1. action->setShortcut(QKeySequence("CTRL+E"));
    To copy to clipboard, switch view to plain text mode 

    But how can do the same, but with the enter key?

    2) How can I avoid the twice emitted slot?
    Using Qt version 4.3.0

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shortcut

    I don't think you can this easily... You can think at enter as a reserved key.

    You could do it by installing event filters in your main window and in the subsequent parents of the button and redirecting Enter to the button. My guess is that the enter key event is consumed by the main window if it does'nt find any child that has the focus...

    Couldn't you settle with any other shortcut?

  7. #7
    Join Date
    May 2007
    Posts
    37
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Shortcut

    I use KDE and Kopete. In a chat window in Kopete, when I press enter, the text is sent, so why can they do it so easy and I not?
    Using Qt version 4.3.0

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shortcut

    Probably they made use of the good old event filters .

    Can't you take a look on how it's done in Kopete?

  9. #9
    Join Date
    May 2007
    Posts
    37
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Shortcut

    I've found this (kdenetwork/kopete/kopete/chatwindow/kopetechatwindow.cpp in KDE 3.80.3):

    Qt Code:
    1. chatSend = new KAction( KIcon("mail_send"), i18n( "&Send Message" ), coll );
    2. coll->addAction( "chat_send", chatSend );
    3. connect( chatSend, SIGNAL( triggered(bool) ), SLOT( slotSendMessage() ) );
    4. //Default to 'Return' for sending messages
    5. chatSend->setShortcut( QKeySequence(Qt::Key_Return) );
    6. chatSend->setEnabled( false );
    To copy to clipboard, switch view to plain text mode 
    Using Qt version 4.3.0

  10. #10
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shortcut

    Qt::Key_Enter refers to the enter key of the numpad (or to no key on a laptop...) whereas the Qt::Key_Return refers to the return key (what a discovery!!!) which is often considered as the enter key, especially now that numpad-less laptops are so widespread....

    Hope this helps
    Current Qt projects : QCodeEdit, RotiDeCode

  11. #11
    Join Date
    May 2007
    Posts
    37
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Shortcut

    1) I use a laptop. I now changed my code to this:

    Qt Code:
    1. action->setShortcut(QKeySequence(Qt::Key_Return));
    To copy to clipboard, switch view to plain text mode 
    And it works fine.

    2) I'll use in the slot function a code that disables the action (http://doc.trolltech.com/snapshot/qa...l#enabled-prop) and in the function where the connect is one which enables it. So I'll get never a slot which emitted twice?

    3) What will happen when someone with a non-laptop keyboard presses the Enter key at the numeric part? Will my slot work or won't it?
    Last edited by Voldemort; 2nd May 2007 at 16:06.
    Using Qt version 4.3.0

  12. #12
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shortcut

    1. Good
    2. I don't quite get you... Whatever the enabled status, a slot shouldn't be emitted twice... What will change is the possibility for your slot ot be emitted at a given time.
    3. It won't... You can't set several shortcuts to an action. At least no using QAction::setShortcut(). You'll have to play with QShortcut or mess with events if your shortcuts are specific to a single widget
    Edit : If you are using Qt 4.2, a new function has been introduced which will certainly make your life easier : QAction::setShortcuts()
    Current Qt projects : QCodeEdit, RotiDeCode

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

    Default Re: Shortcut

    Since Qt 4.2 one can set multiple shortcuts to an action.
    J-P Nurmi

  14. #14
    Join Date
    May 2007
    Posts
    37
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Shortcut

    1) My code currently is:

    Qt Code:
    1. QPushButton *blogin = new QPushButton(tr("&Aanmelden"));
    2.  
    3. action->setShortcut(QKeySequence(Qt::Key_Return));
    4. blogin->addAction(action);
    5.  
    6. connect(action,SIGNAL(triggered()),SLOT(close()));
    7. connect(action,SIGNAL(triggered()),SLOT(startLogin()));
    8.  
    9. connect(blogin, SIGNAL(clicked()), SLOT(close()));
    10. connect(blogin, SIGNAL(clicked()), SLOT(startLogin()));
    To copy to clipboard, switch view to plain text mode 
    This works fine, but what wil happen when a user sets the focus on the QPushButton and presses Enter? Then will action sending the signal triggered and blogin the signal clicked => the close and startLogin() function will be emited twice.

    marcel said this some post above this:

    The problem is that enter is also used to "click" a button, when it has the focus. So be careful, because it could emit a clicked event, therefore calling your slot twice.
    How can I solve this? A bool which will say the slot is emited or not?

    2) Can I do this:

    Qt Code:
    1. action->setShortcut(Qt::Key_Return);
    2. action->setShortcut(Qt::Key_Enter);
    To copy to clipboard, switch view to plain text mode 

    Or is the function setShortcuts required? Can someone give an example (when I do it wrong I can't test it will work because I can only press "Return" (have no "Enter" on a laptop))?
    Using Qt version 4.3.0

  15. #15
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shortcut

    Quote Originally Posted by Voldemort View Post
    This works fine, but what wil happen when a user sets the focus on the QPushButton and presses Enter? Then will action sending the signal triggered and blogin the signal clicked => the close and startLogin() function will be emited twice.
    I'd tend to think that the signal will be emitted only once either because the button will catch the event or, more probably (but depends on internal event handling of QPushButton), because the shortcut will be triggered and stop event propagation... Have you tried it or are you making assumptions on potential problems???

    Mutliples shortcuts, provided that you use Qt 4.2 or newer, can be assigned this way :

    Qt Code:
    1. QList<QKeySequence> shortcuts;
    2. shortcuts
    3. << Qt::Key_Return // this is the primary shortcut
    4. << Qt::Key_Enter // this is another valid shortcut but won't appear on a menu for exmple
    5. << Q::Key_F7 // yet another shortcut, just to show that you can go beyond two...
    6. ;
    7.  
    8. action->setShortcuts(shortcuts);
    To copy to clipboard, switch view to plain text mode 
    Hope this helps.
    Last edited by fullmetalcoder; 2nd May 2007 at 20:49. Reason: added a missing instruction within code tag
    Current Qt projects : QCodeEdit, RotiDeCode

  16. #16
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shortcut

    Have you verified this? Is the signal emitting twice?
    I'm not sure, never tried it. Also, when you press space on a button that has the focus causes the button to be clicked .

    If the signals does get emitted twice, you might wanna try setFocusPolicy( Qt::NoFocus ) on the QPushButton.

  17. #17
    Join Date
    May 2007
    Posts
    37
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Shortcut

    When I tried it, the new window isn't opened twice (the slot shows a new window and closes the current). I think it won't be a problem, but maybe in the future (newer Qt versions)?
    Using Qt version 4.3.0

Similar Threads

  1. Shortcut for QMenu
    By Ryhel in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2007, 11:57
  2. Strange shortcut problem
    By blukske in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2007, 10:26
  3. Qt 4.2 Shortcut Management
    By dvmorris in forum Qt Programming
    Replies: 20
    Last Post: 5th March 2007, 20:48
  4. shortcut with more than one key
    By ChasW in forum Qt Programming
    Replies: 1
    Last Post: 26th January 2007, 06:38
  5. Trying to set shortcut
    By mikro in forum Newbie
    Replies: 2
    Last Post: 30th November 2006, 09:55

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.