Solved A customized widget
-
Hi all,
Please take a look at the example of this page. what does it do? Does it convert decimal numbers to hexadecimal ones? E.g., 10 (Dec) = A (Hex).
If so, if possible please run the program and give it 10 to see if it gives back A to you or not.
-
Hi
Its showing how to subclass a widget and make it be a hexadecimal spin box.
a normal spin box do not show in hex.It expects you to input in HEX as seen by the validator and also mentioned in the text
"To achieve this, we use a QRegExpValidator that accepts between one and eight characters, all of which must be in the set {'0', ..., '9', 'A', ..., 'F', 'a', ..., 'f'}." -
Thanks.
So doesn't it do an conversion? -
@tomy
It does convert back and forth between string hex format and actual value.This convert a value to its HEX representation. note the 16 base parameter QString HexSpinBox::textFromValue(int value) const { return QString::number(value, 16).toUpper(); } This convert a HEX string to actual value int HexSpinBox::valueFromText(const QString &text) const { bool ok; return text.toInt(&ok, 16); }
It's meant as sample of a HEX spin edit. So it accepts hex and show hex notation.
The internal converting is used for letting up/down arrows work to alter the value. -
This convert a value to its HEX representation. note the 16 base parameter
This convert a HEX string to actual valueBut neither works!
When I enter 10, I expect that it shows A.
When I enter AB, I expect that it shows 171.
But it does none of these and shows the entries as they are! -
It's not a converter. All it does is lets you edit hex numbers i.e. if you want to see "A" you enter "A". It displays it in hex just as you entered it. There is a conversion inside it, but it's internal and only for the purpose of satisfying the required API, as @mrjj pointed out.
-
I can't understand what the job of that SpinBox is. Is it only showing what we enter?!
If so, then why when I enter 100 it shows FF? Whatever number between 100 to 255 yields FF in that widget! -
@tomy said in A customized widget:
I can't understand what the job of that SpinBox is
It's exactly the same as a normal QSpinBox, except normal spinbox uses decimal numbers (base 10) and this one uses hexadecimal (base 16). That's it. There's nothing more to it.
Is it only showing what we enter?!
Yes, exactly like a normal QSpinBox.
If so, then why when I enter 100 it shows FF?
Because it has a default range set in its constructor:
setRange(0, 255);
Which means anything bigger than 255(decimal) is clamped. 100 in hex is 256 decimal, one bigger than the maximum so it gets turned to 255 (FF in hex).
Remember that you're entering hex numbers, so anything greater than FF is clamped. If you want a bigger range you need to first callsetMaximum()
orsetRange()
with something bigger. -
@Chris-Kawa
I think I got it.
The normal SpinBox get numbers without limit. We modify its derived functions in order to make two changes: firstly, get it to get Hex numbers instead of Dec ones, and secondly, get it to have a range (a limit) of 0 to 255.
Right? -
@tomy
hi
Yes, but the range function can be called without subclassing.
The normal spin edit as a range of 0-99we subclass so we can make it take hex as input and show hex.
-
@mrjj
Yes, I think, that range setting isn't inside the virtual functions but inside the constructor so it's able to be used as an independent issue of subclassing.And please note, it here near the icon, says: "To demonstrate how to write a custom widget using this approach, we will create the IconEditor widget". To me that IconEditor is a program while it calls it a widget! If it's right, so we can call any program a widget. Yeah?
-
@tomy
Well the iconEditor uses the icon editor widget and some other stuff.
So normally you would not call a single widget a program as such but on
the other hand, qmainwindow is also a widget so sometimes an app is just one widget.
Anyway, the topic here is custom widget so dont put to much into the naming of the IconEditor. -
You can call
setRange()
from wherever you want. In the example it's used in the constructor to give that widget a sensible default value when it is created, but you can call it again later to change that range. the range 0-255 is chosen as a default because a usual use case for a hex spin box is to edit bytes, and 0-255 is the range of an 8 bit unsigned byte.If it's right, so we can call any program a widget. Yeah?
No, not really. I mean you can, but no one will know what you mean. The word "widget" means a piece of user interface, and, specifically in Qt world, it means a particular technology for creating these ui element - QWidget derived classes.
A program is a an executable that does something. One of its functions might be showing widgets, but that doesn't make a widget a program.
Think of it like this: A driving car can carry babies on the back seat but it doesn't mean you should call the baby a car :) Similarly a program can show widgets (or a single widget) but it doesn't make the widget a program ;) -
@tomy said in A customized widget:
To me that IconEditor is a program while it calls it a widget! If it's right, so we can call any program a widget. Yeah?
Come on! It is an example application to demonstrate how to write a custom widget. To be able to see this custom widget in action you need a program which uses it, right?
-
Well the iconEditor uses the icon editor widget and some other stuff.
Do we have such a widget?
I think the program subclasses QWidget and re-implements some protected functions.So normally you would not call a single widget a program as such but on
the other hand, qmainwindow is also a widget so sometimes an app is just one widget.
Anyway, the topic here is custom widget so dont put to much into the naming of the IconEditor.OK, good.
-
Hi
The widget in the sample was such widget. a custom widget.
please dont put to much in the name. its a custom widget. the name is not really important. -
@mrjj
Hi,OK, thanks.
PS: There was a really big earthquake here in my city last night when I was trying to send posts here!
-
@jsulm
Yeah, widgets are tools which are very useful for programs to use.