Results 1 to 7 of 7

Thread: Keyboard widget producing incorrect value

  1. #1
    Join Date
    Jun 2018
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Keyboard widget producing incorrect value

    The keyboard widget is casting 16 bit hex value into an 8 bit char, losing the leading byte. The character that appears on the '5' button is currently a 'G' (0x0047). It should actually be a '?' (Capital N with caron - 0x0147):

    Qt Code:
    1. // Create keyboard
    2. //
    3. // Note: The hex numbers here are unicode code points, not
    4. // extended ascii or utf-8 encoding.
    5. //
    6. QHBoxLayout* row1 = new QHBoxLayout();
    7. createBtn(row1, 2, '1', '_', '1', '!', static_cast<char>(0xBC));
    8. createBtn(row1, 2, '2', '%', '2', '@', static_cast<char>(0xBD));
    9. createBtn(row1, 2, '3', '&', '3', '?', static_cast<char>(0xBE));
    10. createBtn(row1, 2, '4', '(', '4', static_cast<char>(0xFF), static_cast<char>(0xB0));
    11. createBtn(row1, 2, '5', ')', '5', static_cast<char>(0xDF), static_cast<char>(0x0147));
    12. createBtn(row1, 2, '6', '-', '6', static_cast<char>(0xE0), static_cast<char>(0xC0));
    13. createBtn(row1, 2, '7', ':', '7', static_cast<char>(0xE1), static_cast<char>(0xC1));
    14. createBtn(row1, 2, '8', '/', '8', static_cast<char>(0xE2), static_cast<char>(0xC2));
    15. createBtn(row1, 2, '9', ',', '9', static_cast<char>(0xE3), static_cast<char>(0xC3));
    16. createBtn(row1, 2, '0', '.', '0', static_cast<char>(0xE4), static_cast<char>(0xC4));
    17. createBtn(row1, 3, false, Qt::Key_Backspace, "<---");
    To copy to clipboard, switch view to plain text mode 

    Has anyone ran into something similar?

  2. #2
    Join Date
    Jul 2008
    Posts
    370
    Thanks
    9
    Thanked 46 Times in 44 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keyboard widget producing incorrect value

    Hi, 0x0147 is too large to fit into a char (which has only 8 bit), so you get the truncated value.

    Ginsengelf

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,765
    Thanks
    216
    Thanked 582 Times in 572 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Keyboard widget producing incorrect value

    Not only that, but it is a signed char which means any value over 0x7F will be negative. createBtn() might ignore the sign, but as Ginsengelf says, 0x0147 is still too large for a char.

    I do not know the signature for the createBtn() method, but if the last two arguments are actually QChar, then changing the static cast type to ushort will work.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Jun 2018
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Keyboard widget producing incorrect value

    Qt Code:
    1. QString KeyboardWidget::c2s(char value) const
    2. {
    3. // Correct for automatic mnemonics.
    4. if (value=='&') return QLatin1String("&&");
    5. else return QString(static_cast<QChar>(QLatin1Char(value)));
    6. }
    7.  
    8.  
    9. int KeyboardWidget::createBtn(QHBoxLayout* row,
    10. int span,
    11. char normal,
    12. char shift,
    13. char caps,
    14. char alt,
    15. char capAlt,
    16. bool autoRepeat)
    17. {
    18. int id = m_allButtons.count();
    19. KeyboardWidgetButton* btn = new KeyboardWidgetButton(this,
    20. id,
    21. c2s(normal),
    22. c2s(shift),
    23. c2s(caps),
    24. c2s(alt),
    25. c2s(capAlt));
    26. row->addWidget(btn, span);
    27. btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    28. btn->setAutoRepeat(autoRepeat);
    29. VERIFY(connect(btn, SIGNAL(takeAction(int)),
    30. this, SLOT(buttonClicked(int))));
    31. m_allButtons.append(btn);
    32. return id;
    33. }
    To copy to clipboard, switch view to plain text mode 

    If you were wondering.
    Last edited by Vysero; 7th September 2018 at 20:45.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,765
    Thanks
    216
    Thanked 582 Times in 572 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Keyboard widget producing incorrect value

    Well, then you are stuck with 8-bit signed chars as the keycodes. What is "c2s()" - "char to short"? If that's the case and you have this source code, change createBtn() to take ushort instead of char arguments, and pass these values directly into the KeyboardWidgetButton() constructor (without passing them through the c2s() conversion). You will then have buttons that return 16-bit keycodes.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Jun 2018
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Keyboard widget producing incorrect value

    Not sure if this is what you mean:
    Qt Code:
    1. QHBoxLayout* row2 = new QHBoxLayout();
    2. createBtn(row2, 1, 'q', 'Q', 'Q', 0xE5, 0xC5);
    3. createBtn(row2, 1, 'w', 'W', 'W', 0xE6, 0xC6);
    4. createBtn(row2, 1, 'e', 'E', 'E', 0xE7, 0xC7);
    5. createBtn(row2, 1, 'r', 'R', 'R', 0xE8, 0xC8);
    6. createBtn(row2, 1, 't', 'T', 'T', 0xE9, 0xC9);
    7. createBtn(row2, 1, 'y', 'Y', 'Y', 0xEA, 0xCA);
    8. createBtn(row2, 1, 'u', 'U', 'U', 0xEB, 0xCB);
    9. createBtn(row2, 1, 'i', 'I', 'I', 0xEC, 0xCC);
    10. createBtn(row2, 1, 'o', 'O', 'O', 0xED, 0xCD);
    11. createBtn(row2, 1, 'p', 'P', 'P', 0xEE, 0xCE);
    12.  
    13. QString KeyboardWidget::c2s(char value) const
    14. {
    15. // Correct for automatic mnemonics.
    16. if (value=='&') return QLatin1String("&&");
    17. else return QString(static_cast<QChar>(QLatin1Char(value)));
    18. }
    19.  
    20.  
    21. int KeyboardWidget::createBtn(QHBoxLayout* row,
    22. int span,
    23. ushort normal,
    24. ushort shift,
    25. ushort caps,
    26. ushort alt,
    27. ushort capAlt,
    28. bool autoRepeat)
    29. {
    30. int id = m_allButtons.count();
    31. KeyboardWidgetButton* btn = new KeyboardWidgetButton(this,
    32. id,
    33. normal,
    34. shift,
    35. caps,
    36. alt,
    37. capAlt);
    38. row->addWidget(btn, span);
    39. btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    40. btn->setAutoRepeat(autoRepeat);
    41. VERIFY(connect(btn, SIGNAL(takeAction(int)),
    42. this, SLOT(buttonClicked(int))));
    43. m_allButtons.append(btn);
    44. return id;
    45. }
    46.  
    47.  
    48. int KeyboardWidget::createBtn(QHBoxLayout* row,
    49. int span,
    50. bool checkable,
    51. int key,
    52. const char* text,
    53. bool autoRepeat)
    54. {
    55. int id = m_allButtons.count();
    56. KeyboardWidgetButton* btn = new KeyboardWidgetButton(this, id, key, QLatin1String(text));
    57. row->addWidget(btn, span);
    58. btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    59. btn->setCheckable(checkable);
    60. btn->setAutoRepeat(autoRepeat);
    61. VERIFY(connect(btn, SIGNAL(takeAction(int)),
    62. this, SLOT(buttonClicked(int))));
    63. m_allButtons.append(btn);
    64. return id;
    65. }
    To copy to clipboard, switch view to plain text mode 

    but compilation gives:

    Qt Code:
    1. : In member function 'int KeyboardWidget::createBtn(QHBoxLayout*, int, ushort, ushort, ushort, ushort, ushort, bool)':
    2. : error: 'QString::QString(const char*)' is private
    3. KeyboardWidget.cpp:329: error: within this context
    4. KeyboardWidget.cpp:329: error: invalid conversion from 'ushort' to 'const char*'
    5. KeyboardWidget.cpp:329: error: initializing argument 1 of 'QString::QString(const char*)'
    To copy to clipboard, switch view to plain text mode 
    Last edited by Vysero; 12th September 2018 at 21:06.

  7. #7
    Join Date
    Jun 2018
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Keyboard widget producing incorrect value

    Okay, I got it working.

Similar Threads

  1. DomDocument to XML: producing empty files
    By athomas in forum Newbie
    Replies: 4
    Last Post: 11th May 2012, 10:11
  2. Replies: 1
    Last Post: 14th November 2011, 09:58
  3. Incorrect widget position and size
    By mero in forum Qt Programming
    Replies: 0
    Last Post: 12th April 2011, 03:30
  4. Producing PDF
    By giusepped in forum Qt Programming
    Replies: 1
    Last Post: 29th May 2008, 08:41
  5. Producing Keypressed Event
    By anafor2004 in forum Newbie
    Replies: 11
    Last Post: 30th April 2008, 10:19

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.