PDA

View Full Version : How to write a program to show 64bit hex with checkbox?



Ryan111
2nd April 2015, 08:48
Hi
I'm trying to write a program to show a 64bit hex value or less than 64bit as 64 checkbox. the GUI is this:

http://upload.tehran98.com/upme/uploads/dcc08af871105f101.png

the scenario:

when you put a hex value in the lineEdit, it should show us the changes in the checkboxes. please note that each checkbox is as a bit in order number.
or when check or uncheck the checkboxes, it should show us the changes in the lineEdit.

now the question is how to write this? I think I have to choose a special slot for lineEdit. how about textChanged() slot? should I use it? and what for checkboxes?

Any idea?

stampede
2nd April 2015, 09:15
One possible solution is to convert the text to 64 bit integer on each "textChanged()" and check/uncheck each checkBox according to the bit value in the number. Adding all checkBoxes to a list makes thing simple:


void MyWidget::textChanged(const QString& text){
const qint64 value = text.toInt64();
for (int i=0 ; i<64 ; ++i){
this->m_checkBoxList[i]->setChecked( test_bit(value,i) );
}
}

implementing "test_bit(qint64 value, int bitIndex)" to check a single bit in number is left as an excercise ;)
How to add all checkBoxes to a single list ? You can just hardcode 64 inserts in constructor, or if you have properly named the checkboxes in designer (like "checkBox_0", "checkBox_1", etc.) you can use QWidget::findChildren(const QString& name) method. Remeber that the order is important, so you probably can't simply call "findChildren" with an empty "name".
What to do on each checkBox change ? Kind of inverse of the procedure described above, I think you will figure this out yourself.

yeye_olive
2nd April 2015, 09:25
Yes, QLineEdit::textChanged() works, but it will be emitted repeatedly during edition, which may be a little too often. QLineEdit::editingFinished() is only emitted once edition is over. Pick your favorite, or experiment with them both.

You may want to set an input mask on the QLineEdit (see QLineEdit::setInputMask()) to force the input to match a 16-digit hexadecimal number.

Now, about the checkboxes. QCheckBox has a signal that is emitted whenever the state of the checkbox changes. Look up the fine documentation for details.

Be careful about naive implementations: if you choose to react to QLineEdit::textChanged(), then every keystroke will cause the signal to be emitted, then your code will update the checkboxes, whose state will change, and your code may reset the QLineEdit's text once for each updated checkbox. I wonder how that may interfere with the user's editing.

stampede
2nd April 2015, 10:29
Be careful about naive implementations: if you choose to react to QLineEdit::textChanged(), then every keystroke will cause the signal to be emitted, then your code will update the checkboxes, whose state will change, and your code may reset the QLineEdit's text once for each updated checkbox
Too bad you warned him, I really wanted Ryan to figure this out himself when trying to debug these lovely infinite loops ;)

wysota
2nd April 2015, 10:36
SIde note: Your GUI doesn't have a layout applied.

Ryan111
2nd April 2015, 14:16
Too bad you warned him, I really wanted Ryan to figure this out himself when trying to debug these lovely infinite loops ;)
SomeOne is trying to make trouble for me!:p:D
I'm a simple and poor 24 years old agricultural engineer from a small town(in Iran) then please make simple all things for me.:)

SIde note: Your GUI doesn't have a layout applied.

Is it necessary? or I can ignore it.

wysota
2nd April 2015, 14:38
Is it necessary? or I can ignore it.

If you want your program to behave correctly then it is necessary.

Ryan111
2nd April 2015, 15:15
If you want your program to behave correctly then it is necessary.
certainly I do want. please define "behave". behave in shape and form and or it can make a bug in codes?

wysota
2nd April 2015, 15:33
Shape and form :) E.g. if you want the window to resize properly or adjust to different font sizes people may have set in their systems.

Ryan111
3rd April 2015, 06:54
Shape and form :) E.g. if you want the window to resize properly or adjust to different font sizes people may have set in their systems.

Thanks buddy,
I saw this clip:

www.youtube.com/watch?v=PQEBoftbtVQ