Results 1 to 6 of 6

Thread: shift + key

  1. #1
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default shift + key

    for some reason i cant make this work.

    i tried to detect shift + tab:
    Qt Code:
    1. switch(e->key())
    2. {
    3.  
    4. case Qt::Key_Tab:
    5. if(textCursor().selectionEnd() - textCursor().selectionStart() > 0)
    6. {
    7. bool shiftHeld = (e->modifiers() & Qt::ShiftModifier);
    8. tabPressedEvent( shiftHeld );
    9. return;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    didn't work. idea?

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: shift + key

    Maybe you need to add
    Qt Code:
    1. e->accept();
    To copy to clipboard, switch view to plain text mode 
    before return statement.

  3. #3
    Join Date
    Sep 2009
    Location
    Tilburg, The Netherlands
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: shift + key

    QWidget detects shift-tab in the event() method (together with some other key combinations, see the source if you're curious) and prevents keypressEvent from being called.
    You can re-implement QWidget::event() and handle it there or forward it to keypressEvent manually.

    Be aware that this may have side effects for navigating widgets using tab/shift-tab, but I'm not sure about that.

  4. #4
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: shift + key

    in an example i found they did it in the KeyPressedEvent - is that outdated?
    http://doc.trolltech.com/4.4/tools-customcompleter.html

    the accept didn't work.

    debugged it a little, seems it detects shift as the input key.
    can you post (or point me to) an example of how to do so with the QWidget::event()?

  5. #5
    Join Date
    Sep 2009
    Location
    Tilburg, The Netherlands
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: shift + key

    Ok, just had another look at this and it turns out that QWidget::event() doesn't actually block shift-tab. sorry for misinforming you, looked over the source a bit to hastily.

    I've done some testing and found out that shift-tab is not reported as Qt::Key_Tab, but as Qt::Key_Backtab (at least on my system, debian linux with kde). So all you have to do is check for Qt::Key_Backtab:

    Qt Code:
    1. switch(e->key())
    2. {
    3. case Qt::Key_Tab:
    4. if(textCursor().selectionEnd() - textCursor().selectionStart() > 0)
    5. {
    6. bool shiftHeld = e->modifiers() & Qt::ShiftModifier;
    7. tabPressedEvent( shiftHeld );
    8. }
    9. break;
    10.  
    11. case Qt::Key_Backtab:
    12. tabPressedEvent( true );
    13. break;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Where I kept the shift modifier check, just in case there is a system which does not use backtab to identify shift+tab.

    If you need to dissable the behavour that tab/shift+tab changes focus to the next widget you will still need to re-implement event() however. The article Events and Event Filters has an example on this (which is perfect for you).

    hope this helps and happy coding.

  6. #6
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: shift + key

    wow.. this is almost embarrassingly simple.

    this is also true for brackets and braces it seems

Similar Threads

  1. QTableView multiselection with shift
    By swrer in forum Newbie
    Replies: 3
    Last Post: 11th July 2010, 10:10
  2. Window Shift problem in Qt4.3.4
    By santosh.kumar in forum Qt Programming
    Replies: 0
    Last Post: 5th July 2010, 05:37
  3. QPen color shift
    By Alberto in forum Qt Programming
    Replies: 0
    Last Post: 22nd October 2008, 11:35
  4. QPushButton on Shift+Click?
    By vishal.chauhan in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2008, 10:00
  5. Comparing the |(shift+\) char
    By warry in forum Newbie
    Replies: 1
    Last Post: 8th September 2008, 07:05

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.