Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. A customized widget

A customized widget

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 4.8k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by tomy
    #1

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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'}."

      1 Reply Last reply
      1
      • tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by
        #3

        Thanks.
        So doesn't it do an conversion?

        mrjjM 1 Reply Last reply
        0
        • tomyT tomy

          Thanks.
          So doesn't it do an conversion?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @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.

          tomyT 1 Reply Last reply
          3
          • mrjjM mrjj

            @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.

            tomyT Offline
            tomyT Offline
            tomy
            wrote on last edited by
            #5

            @mrjj

            This convert a value to its HEX representation. note the 16 base parameter
            This convert a HEX string to actual value

            But 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!

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • tomyT Offline
                tomyT Offline
                tomy
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #8

                  @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 call setMaximum() or setRange() with something bigger.

                  tomyT 1 Reply Last reply
                  2
                  • Chris KawaC Chris Kawa

                    @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 call setMaximum() or setRange() with something bigger.

                    tomyT Offline
                    tomyT Offline
                    tomy
                    wrote on last edited by Chris Kawa
                    #9

                    @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?

                    mrjjM 1 Reply Last reply
                    0
                    • tomyT tomy

                      @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?

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @tomy
                      hi
                      Yes, but the range function can be called without subclassing.
                      The normal spin edit as a range of 0-99

                      we subclass so we can make it take hex as input and show hex.

                      tomyT 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @tomy
                        hi
                        Yes, but the range function can be called without subclassing.
                        The normal spin edit as a range of 0-99

                        we subclass so we can make it take hex as input and show hex.

                        tomyT Offline
                        tomyT Offline
                        tomy
                        wrote on last edited by
                        #11

                        @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?

                        mrjjM jsulmJ 2 Replies Last reply
                        0
                        • tomyT tomy

                          @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?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @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.

                          tomyT 1 Reply Last reply
                          1
                          • Chris KawaC Offline
                            Chris KawaC Offline
                            Chris Kawa
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            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 ;)

                            1 Reply Last reply
                            2
                            • tomyT tomy

                              @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?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @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?

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              tomyT 1 Reply Last reply
                              0
                              • mrjjM mrjj

                                @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.

                                tomyT Offline
                                tomyT Offline
                                tomy
                                wrote on last edited by
                                #15

                                @mrjj

                                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.

                                1 Reply Last reply
                                0
                                • mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  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.

                                  tomyT 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    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.

                                    tomyT Offline
                                    tomyT Offline
                                    tomy
                                    wrote on last edited by
                                    #17

                                    @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!

                                    1 Reply Last reply
                                    0
                                    • jsulmJ jsulm

                                      @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?

                                      tomyT Offline
                                      tomyT Offline
                                      tomy
                                      wrote on last edited by
                                      #18

                                      @jsulm
                                      Yeah, widgets are tools which are very useful for programs to use.

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved