PDA

View Full Version : hex from Qlineedit and checksum



hovuquocan1997
15th July 2015, 20:31
I have a Qlineedit that I want the user to input hex freely without restriction. I've tried to use setInputMask but you have to know the size before hand in order to set it up. So I was wondering if there is a way to do what I want.

Another thing is after the user inputs hex values, how can I grab them and put them into my char array to do checksum and send it to the serial port?

So basically an example of what I want is this:

char array with 0x01, 0x10, 0x20, 0x00, 0x01, 0x00 in the front, then the user inputs some random hex like 1234 in the Qlineedit. And then when a button is pushed, the hex in the Qlineedit will be added to the char array, and then something will calculate the checksum and add it to the char array also. Then I can also add the end byte at the end of the array. The final char array should have these:

{0x01, 0x10, 0x20, 0x00, 0x01, 0x00, 0x12, 0x34, 0x78, 0x04}
(0x12 and 0x34 from Qlineedit, 0x78 is the checksum and 0x04 is the end byte)

Thank you!

ChrisW67
15th July 2015, 22:53
You can limit the user input with a QValidator sub-class. You can remove any character that is not valid hex in the validate function, and also flag Intermediate on odd-numbers of chars. The packet assembly could happen in the slot attached to the button if the input is valid.

Alternatively, a QLineEdit subclass could intercept and swallow keypresses that were not valid.

hovuquocan1997
15th July 2015, 22:58
Thank you for the reply, but can you please elaborate and give me an example of how the codes should look like? I'm still new to Qt and therefore am not familiar with all the functions that are in those classes.