Results 1 to 7 of 7

Thread: Disable Tab Key

  1. #1
    Join Date
    Feb 2006
    Posts
    16
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Disable Tab Key

    Somebody knows how to disable the tab key. I need to disable the use of tab key to move between fileds.

    Thanks for your help

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Disable Tab Key

    Qt Code:
    1. void MyWidget::keyPressEvent(QKeyEvent *e)
    2. {
    3. if(e->key()==Qt::Key_Tab)
    4. {
    5. e->accept();
    6. //do somthing if you want
    7. }
    8. else e->reject();
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Posts
    16
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Disable Tab Key

    I have this error with your code:

    error: ‘class QKeyEvent’ has no member named ‘reject’

    I am using QT 3.3.6

  4. #4
    Join Date
    Feb 2006
    Posts
    31

    Default Re: Disable Tab Key

    You can try turn of TabFocus for each fields.

    For example:
    If you have one QPushButton and one QLineEdit in your window. You can try

    mMyBtn->setFocusPolicy(QWidget::NoFocus);
    mMyEdit->setFocusPolicy(QWidget::NoFocus);

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Disable Tab Key

    Quote Originally Posted by otortos
    I have this error with your code:

    error: ‘class QKeyEvent’ has no member named ‘reject’

    I am using QT 3.3.6
    Sorry, it should be e->ingnore();

  6. #6
    Join Date
    Feb 2006
    Posts
    16
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Disable Tab Key

    This is my code:

    void Form1::keyPressEvent(QKeyEvent *e)
    {
    if(e->key()==Qt::Key_Tab)
    {
    e->ignore();
    }
    else
    {
    e->ignore();
    }
    }

    and, when i press "tab" still move to the next field.

  7. #7
    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: Disable Tab Key

    This approach is wrong. The key press event goes directly to a focused child.

    You could prevent the child from getting tab key press events by an event filter, but in my opinion that wouldn't be very good idea. I don't know what kind of widgets do you have, but some widgets have an "internal navigation" etc. with tab key. E.g. a date and time edit, which first changes the current field, and then after the last field changes the focus.
    Qt Code:
    1. // install event filter for all widgets, e.g. in "MyWidget" constructor
    2. QList<QWidget*> widgets = findChildren<QWidget*>();
    3. foreach (QWidget* widget, widgets)
    4. widget->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. bool MyWidget::eventFilter(QObject* o, QEvent* e)
    2. {
    3. if (e->type() == QEvent::KeyPress)
    4. {
    5. QKeyEvent* k = static_cast<QKeyEvent*>(e);
    6. if (k->key() == Qt::Key_Tab)
    7. {
    8. // filter tab out
    9. return true;
    10. }
    11. }
    12. return false;
    13. }
    To copy to clipboard, switch view to plain text mode 

    A better solution could be to trick the tab order. By this way you wouldn't prevent widgets from receiving and acting due to tab key press events.
    Qt Code:
    1. QList<QWidget*> widgets = findChildren<QWidget*>();
    2. foreach (QWidget* widget, widgets)
    3. widget->setTabOrder(widget, widget);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. how to disable OnItem drops in a QListView?
    By rgl in forum Qt Programming
    Replies: 2
    Last Post: 31st January 2009, 15:26
  2. How to disable NextButton in QWizard ?
    By litroncn in forum Qt Programming
    Replies: 3
    Last Post: 27th May 2008, 07:05
  3. Disable Checkable Button Question
    By jbpvr in forum Qt Programming
    Replies: 9
    Last Post: 20th March 2007, 17:57
  4. Replies: 1
    Last Post: 9th February 2007, 09:41

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.