Results 1 to 6 of 6

Thread: KeyEvent Handling

  1. #1
    Join Date
    Nov 2013
    Posts
    46
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default KeyEvent Handling

    I want whenever i press tab space cursor will move 4 position forward. I tried this code but its not working. Can anybody tell me what is wrong with the code? Why this code is not working?

    void MainWindow::keyPress(QKeyEvent *eve)
    {
    if (eve->key()==Qt::Key_Tab)
    {
    ui->plainTextEdit->insertPlainText(" ");
    }
    }

  2. #2
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: KeyEvent Handling

    What type of class is your mainwindow if it is QMainWindow you can use void keyPressEvent(QKeyEvent *); (I dont know if it also triggers on child items)

    else you can install an eventfilter

    installEventFilter(this);

    add this function to the main window
    bool MainWindow::eventFilter(QObject *obj, QEvent *event)

    in the filter you can check for a specific object and event
    if the event is the one you need you can entirely filter it by returning true (the event will be seen as handled) else return false (event will go to child objects)

  3. #3
    Join Date
    Nov 2013
    Posts
    46
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: KeyEvent Handling

    But i want cursor will move 4 spaces ahead instead of 8 spaces whenever i press tab space. How it will be done?

  4. #4
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: KeyEvent Handling

    use the eventfilter and return true if you receive the tab key, if you dont filter it you will do 4 spaces and then when the input box receives the tab it will add another tab

    edit:
    made some code
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. ui->lineEdit->installEventFilter(this);
    7. }
    8.  
    9. bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    10. {
    11. if(event->type() == QEvent::KeyPress) //event is installed on the lineedit so not checking object
    12. {
    13. QKeyEvent *key = dynamic_cast<QKeyEvent*>(event);
    14. if(key && key->key() == Qt::Key_Tab)
    15. {
    16. ui->lineEdit->insert("!!!!");
    17. return true;
    18. }
    19. }
    20. return false;
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by StrikeByte; 21st January 2014 at 11:04.

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

    parulkalra14 (21st January 2014)

  6. #5
    Join Date
    Nov 2013
    Posts
    46
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: KeyEvent Handling

    @StrikeByte: thanku so much sir its working fine now......

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: KeyEvent Handling

    Or derive from the text edit and implement it directly in the class that gets the events.

    Cheers,
    _

Similar Threads

  1. How to get keyevent in qt console application
    By zuoshaobo in forum Qt Programming
    Replies: 4
    Last Post: 7th December 2011, 06:55
  2. keyevent
    By mero in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2011, 10:32
  3. keyevent/zoom
    By bhogasena in forum Qt Programming
    Replies: 7
    Last Post: 23rd January 2009, 14:28
  4. KeyEvent to go to a parent/grandparent widget?
    By thomaspu in forum Qt Programming
    Replies: 2
    Last Post: 29th December 2007, 03:07
  5. KeyEvent propagation to Desktop
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2007, 12:38

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.