Results 1 to 5 of 5

Thread: key mapping program Qtimer problems

  1. #1
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default key mapping program Qtimer problems

    Hi,

    I am trying to write a code so that when I press "1" it should map it and show a , b, or c according tot he timeout conditions from the previous key pressed.
    Following is my code:

    Qt Code:
    1. if((e->key() == Qt::Key_1) && ((target->objectName() == "textBox1") || (target->objectName() == "textBox2") || (target->objectName() == "textBox3") || (target->objectName() == "textBox4")))
    2. {
    3. int value;
    4. bool stat = connect(sameKey, SIGNAL(timeout()), this, SLOT(second()));
    5. qDebug()<<"connect returns "<< stat;
    6. sameKey->start(1000);
    7.  
    8. value = processKey(Qt::Key_1);
    9. qDebug()<<"value received from processKey is "<<value;
    10. if(value == 0)
    11. {
    12. if(target->objectName() == "textBox1")
    13. {
    14. QString temp = textBox1->toPlainText();
    15. temp.append("a");
    16. textBox1->setText(temp);
    17. }
    18. }
    19. else if(value == 1)
    20. {
    21. if(target->objectName() == "textBox1")
    22. {
    23. QString temp = textBox1->toPlainText();
    24. temp.append("b");
    25. textBox1->setText(temp);
    26. }
    27. }
    28. else if(value == 2)
    29. {
    30. if(target->objectName() == "textBox1")
    31. {
    32. QString temp = textBox1->toPlainText();
    33. temp.append("c");
    34. textBox1->setText(temp);
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

    the process key function
    Qt Code:
    1. int MainWindow::processKey(int keyCode)
    2. {
    3. // qDebug()<<"got the keycode as "<<keyCode;
    4. current_key = keyCode;
    5.  
    6. if((!timerFlag) && (current_key == prev_key))
    7. {
    8. qDebug("b4 timer stop");
    9. count++;
    10. if(count == 3)
    11. count = 0;
    12. sameKey->stop();
    13. }
    14. else
    15. {
    16. qDebug("in else");
    17. count = 0;
    18. timerFlag = 0;
    19. // sameKey->stop();
    20. }
    21.  
    22. prev_key = current_key;
    23. qDebug()<<"returning count as "<<count;
    24. return count;
    25. }
    To copy to clipboard, switch view to plain text mode 

    the update timer function
    Qt Code:
    1. void MainWindow::second()
    2. {
    3. qDebug("in timer update slot.");
    4. sameKey->stop();
    5. timerFlag = 1;
    6. }
    To copy to clipboard, switch view to plain text mode 

    But the output I get when I press 1 twice simultaneously is "ab" and not just "b".
    Please help me.
    Thanks in advance.
    - Charvi

  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: key mapping program Qtimer problems

    you mean something like the following?:
    Qt Code:
    1. //Header file
    2. ...
    3.  
    4. typedef QMap<int, QString> KeyMapType;
    5. ....
    6.  
    7. KeyMapType m_keyMap;
    8. ...
    9.  
    10. //Implementation
    11. //in some init method (contructor?)
    12. m_keyMap[Qt::Qt::Key_1] = "a";
    13. m_keyMap[Qt::Qt::Key_2] = "b";
    14. ...
    15.  
    16.  
    17. void MyClass::keyPressEvent ( QKeyEvent * event )
    18. {
    19. KeyMapType::const_iterator itKey = m_keyMap.find(event->key());
    20. if(itKey != m_keyMap.end()){
    21. qDebug()<<(*itKey)->second;
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    This is just pseudo code to show the idea.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: key mapping program Qtimer problems

    ... and use QTextCursor to insert characters instead of reading the whole text and setting it back again after appending one character.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: key mapping program Qtimer problems

    ummm, actually in my case Key_1 maps to a, b and c depending on the number of times the key is pressed withing a pre-defined time interval.
    This corresponds to a functionality just like that in earlier cell phones without Qwerty.

    and QTextCursor will certainly reduce the number of LoC. Thanks.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: key mapping program Qtimer problems

    So attach a string list and take the appropriate cell from the list based on the time period.

    and QTextCursor will certainly reduce the number of LoC. Thanks.
    It will greatly reduce the lag in your application.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 27th June 2021, 02:23
  2. QTimer resolution problems under QWS
    By denizlitr in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 16th December 2011, 15:31
  3. Replies: 0
    Last Post: 20th July 2010, 14:30
  4. mapping manager
    By sreedhar in forum Qt Programming
    Replies: 6
    Last Post: 16th February 2007, 13:48
  5. Problems mapping an image file into QByteArray
    By Dark_Tower in forum Qt Programming
    Replies: 3
    Last Post: 12th December 2006, 21:23

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.