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. Functions called unclearly
Qt 6.11 is out! See what's new in the release blog

Functions called unclearly

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 6 Posters 7.1k Views 5 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.
  • Chris KawaC Chris Kawa

    They are called by the input event handlers defined in the QSpinBox class. If you want to see exact order of calls place a breakpoint in any of them and run it through the debugger. It will break there and give you the call stack trace.
    You can also open the source code of QSpinBox and look for these function calls there if you're interested.

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

    @Chris-Kawa
    are those functions overridden?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #6

      Have a look at the documentation:

      • http://doc.qt.io/qt-5/qspinbox.html#validate
      • http://doc.qt.io/qt-5/qspinbox.html#valueFromText
      • http://doc.qt.io/qt-5/qspinbox.html#textFromValue

      All three functions are virtual, so yes, in your HexSpinBox they're overridden. Thus a more adequate code would state this explicitly:

      class HexSpinBox : public QSpinBox
      {
      // ...
      protected:
         QValidator::State validate(QString&, int&) const override;
         int valueFromText(const QString&) const override;
         QString textFromValue(int) const override;
      //...
      
      1 Reply Last reply
      3
      • tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by tomy
        #7

        Thanks.
        I also had a look at the Help window of Qt Creator.

        Few questions:

        1- In hexspinbox.h we have

        public:
            HexSpinBox(QWidget* parent = 0);
        

        and in hexspinbox.cpp, this:

        HexSpinBox::HexSpinBox(QWidget *parent)
             : QSpinBox(parent)
        

        My (incomplete) understanding is: Since every widget in Qt is a subclass of QWidget, by QWidget* parent = 0 we've separated the class HexSpinBox from its parent which is QWidget. Then in HexSpinBox(QWidget *parent) : QSpinBox(parent) we've used the pointer to the parent and have assigned it to the QSpinBox making it the only parent of the HexSpinBox. Is it right?

        2- The program should accepts values from 0 to 255. But when I input values from 100 upwards, it converts them all only to FF! Why is it that way, please?

        mrjjM 1 Reply Last reply
        0
        • tomyT tomy

          Thanks.
          I also had a look at the Help window of Qt Creator.

          Few questions:

          1- In hexspinbox.h we have

          public:
              HexSpinBox(QWidget* parent = 0);
          

          and in hexspinbox.cpp, this:

          HexSpinBox::HexSpinBox(QWidget *parent)
               : QSpinBox(parent)
          

          My (incomplete) understanding is: Since every widget in Qt is a subclass of QWidget, by QWidget* parent = 0 we've separated the class HexSpinBox from its parent which is QWidget. Then in HexSpinBox(QWidget *parent) : QSpinBox(parent) we've used the pointer to the parent and have assigned it to the QSpinBox making it the only parent of the HexSpinBox. Is it right?

          2- The program should accepts values from 0 to 255. But when I input values from 100 upwards, it converts them all only to FF! Why is it that way, please?

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

          @tomy said in Functions called unclearly:

          My (incomplete) understanding is: Since every widget in Qt is a subclass of QWidget, by QWidget* parent = 0 we've separated the class HexSpinBox from its parent which is QWidget. Then in HexSpinBox(QWidget *parent) : QSpinBox(parent) we've used the pointer to the parent and have assigned it to the QSpinBox making it the only parent of the HexSpinBox. Is it right?

          Ehh No. Not at all. Base Class
          (like HexSpinBox : public QSpinBox < - base class )
          has nothing to do with parent. which is a pointer to another widget class that will own it and delete it when parent itself is deleted.

          HexSpinBox(QWidget* parent = 0);
          =0 just mean, its optional and if not given, its 0.

          This line
          HexSpinBox::HexSpinBox(QWidget *parent)
          : QSpinBox(parent)

          is HexSpinBox constructor and it also call its base class constructor
          ( : QSpinBox(parent) <- base class constructor )
          so it get a chance to initialize as it wants.

          tomyT 1 Reply Last reply
          2
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #9

            Hi,

            To add to @mrjj, you have a Hex spin box so it's normal that it ends up with FF since that's the hexadecimal representation of 255. If you don't want that then just use a QSpinBox with a maximum at 255.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • mrjjM mrjj

              @tomy said in Functions called unclearly:

              My (incomplete) understanding is: Since every widget in Qt is a subclass of QWidget, by QWidget* parent = 0 we've separated the class HexSpinBox from its parent which is QWidget. Then in HexSpinBox(QWidget *parent) : QSpinBox(parent) we've used the pointer to the parent and have assigned it to the QSpinBox making it the only parent of the HexSpinBox. Is it right?

              Ehh No. Not at all. Base Class
              (like HexSpinBox : public QSpinBox < - base class )
              has nothing to do with parent. which is a pointer to another widget class that will own it and delete it when parent itself is deleted.

              HexSpinBox(QWidget* parent = 0);
              =0 just mean, its optional and if not given, its 0.

              This line
              HexSpinBox::HexSpinBox(QWidget *parent)
              : QSpinBox(parent)

              is HexSpinBox constructor and it also call its base class constructor
              ( : QSpinBox(parent) <- base class constructor )
              so it get a chance to initialize as it wants.

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

              @mrjj
              Hi,

              From

              public:
                  HexSpinBox(QWidget* parent = 0);
              

              and

              HexSpinBox::HexSpinBox(QWidget *parent)
                   : QSpinBox(parent)
              

              So here parent is a pointer to QWidget and that parent goes on pointing to that QWidget until the parent itself is deleted. (at the end of the program)
              It seems we need that parent only for initialising the base class (here QSpinBox). Right?
              And it's obligatory. That is, if we want to use a base class (here QSpinBox) we must invoke its constructor and initialise it so that we are able to use it. Right?

              @SGaist
              Hi,

              The matter is that whatever number: 100, 105, 211 (that are all lower than 255) I give it it only shows FF!

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

                @tomy said in Functions called unclearly:

                It seems we need that parent only for initialising the base class (here QSpinBox). Right?
                Yes, to give it to base class.

                And it's obligatory.
                Well i did mean the paramter but yes you can say its obligatory to call the base constructor.

                tomyT 1 Reply Last reply
                1
                • mrjjM mrjj

                  @tomy said in Functions called unclearly:

                  It seems we need that parent only for initialising the base class (here QSpinBox). Right?
                  Yes, to give it to base class.

                  And it's obligatory.
                  Well i did mean the paramter but yes you can say its obligatory to call the base constructor.

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

                  @mrjj
                  Thank you.
                  But since parent is assigned to zero in the header file, parent = 0, we in fact are sending a zero to the base class's constructor. Yes?

                  mrjjM jsulmJ 2 Replies Last reply
                  0
                  • tomyT tomy

                    @mrjj
                    Thank you.
                    But since parent is assigned to zero in the header file, parent = 0, we in fact are sending a zero to the base class's constructor. Yes?

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

                    @tomy
                    The Default value is 0
                    so if we create it like
                    HexSpinBox * mine= HexSpinBox(); // none given, zero will be used
                    alternatively we do
                    HexSpinBox * mine= HexSpinBox(this); // now we give it a parameter/value and that is sent to the base class.( this = mainwindow or where ever you create it)

                    The parameter=0 is called a default value. Which means that value is used if no parameter value is given.
                    Also for functions.

                    void PrintX( QString test="hello");

                    PrintX(); // prints hello
                    PrintX("WORLD"); // prints WORLD

                    tomyT 1 Reply Last reply
                    3
                    • tomyT tomy

                      @mrjj
                      Thank you.
                      But since parent is assigned to zero in the header file, parent = 0, we in fact are sending a zero to the base class's constructor. Yes?

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

                      @tomy If parent is set as 0 that means the QObject does not have any parent. This is perfectly valid and it is perfectly valid to pass 0 to base class constructor - it is exactly the same - no parent. If a QObject does not have a parent then it is not deleted automatically and you as developer have to delete it when it is not needed. If a QObject has a parent then it is deleted when parent is deleted.

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

                      1 Reply Last reply
                      2
                      • mrjjM mrjj

                        @tomy
                        The Default value is 0
                        so if we create it like
                        HexSpinBox * mine= HexSpinBox(); // none given, zero will be used
                        alternatively we do
                        HexSpinBox * mine= HexSpinBox(this); // now we give it a parameter/value and that is sent to the base class.( this = mainwindow or where ever you create it)

                        The parameter=0 is called a default value. Which means that value is used if no parameter value is given.
                        Also for functions.

                        void PrintX( QString test="hello");

                        PrintX(); // prints hello
                        PrintX("WORLD"); // prints WORLD

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

                        @mrjj
                        You're right and thank you.
                        I suppose in my case that we instantiate the class in main.cpp,

                        HexSpinBox spinBox;
                        

                        a zero will be sent to QSpinBox.

                        @jsulm

                        If a QObject does not have a parent then it is not deleted automatically and you as developer have to delete it when it is not needed. If a QObject has a parent then it is deleted when parent is deleted.

                        By 'a QObject' you meant the class HexSpinBox, yes?
                        Thanks for your valuable information.

                        In this case I think I should declare a destructor for the class, therefore in the header:

                        ...
                        public:
                            HexSpinBox(QWidget* parent = 0);
                           ~HexSpinBox();
                        ...
                        

                        And in the implementation:

                        HexSpinBox::~HexSpinBox() {
                            delete this;
                        }
                        

                        But it doesn't seem to work properly.

                        ? jsulmJ 2 Replies Last reply
                        0
                        • tomyT tomy

                          @mrjj
                          You're right and thank you.
                          I suppose in my case that we instantiate the class in main.cpp,

                          HexSpinBox spinBox;
                          

                          a zero will be sent to QSpinBox.

                          @jsulm

                          If a QObject does not have a parent then it is not deleted automatically and you as developer have to delete it when it is not needed. If a QObject has a parent then it is deleted when parent is deleted.

                          By 'a QObject' you meant the class HexSpinBox, yes?
                          Thanks for your valuable information.

                          In this case I think I should declare a destructor for the class, therefore in the header:

                          ...
                          public:
                              HexSpinBox(QWidget* parent = 0);
                             ~HexSpinBox();
                          ...
                          

                          And in the implementation:

                          HexSpinBox::~HexSpinBox() {
                              delete this;
                          }
                          

                          But it doesn't seem to work properly.

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #16

                          @tomy said in Functions called unclearly:

                          HexSpinBox::~HexSpinBox() {
                          delete this;
                          }

                          Whenever ~HexSpinBox() is called the object gets destroyed by the C++ runtime. Calling delete on the object inside the destructor yourself is a bug.

                          HexSpinBox(QWidget* parent = 0);

                          BTW, although valid code, for new code QWidget *parent = nullptr is highly recommended.

                          tomyT 1 Reply Last reply
                          2
                          • tomyT tomy

                            @mrjj
                            You're right and thank you.
                            I suppose in my case that we instantiate the class in main.cpp,

                            HexSpinBox spinBox;
                            

                            a zero will be sent to QSpinBox.

                            @jsulm

                            If a QObject does not have a parent then it is not deleted automatically and you as developer have to delete it when it is not needed. If a QObject has a parent then it is deleted when parent is deleted.

                            By 'a QObject' you meant the class HexSpinBox, yes?
                            Thanks for your valuable information.

                            In this case I think I should declare a destructor for the class, therefore in the header:

                            ...
                            public:
                                HexSpinBox(QWidget* parent = 0);
                               ~HexSpinBox();
                            ...
                            

                            And in the implementation:

                            HexSpinBox::~HexSpinBox() {
                                delete this;
                            }
                            

                            But it doesn't seem to work properly.

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

                            @tomy You should read http://doc.qt.io/qt-5.9/objecttrees.html
                            By QObject I mean any class derived from QObject.

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

                            tomyT 1 Reply Last reply
                            1
                            • ? A Former User

                              @tomy said in Functions called unclearly:

                              HexSpinBox::~HexSpinBox() {
                              delete this;
                              }

                              Whenever ~HexSpinBox() is called the object gets destroyed by the C++ runtime. Calling delete on the object inside the destructor yourself is a bug.

                              HexSpinBox(QWidget* parent = 0);

                              BTW, although valid code, for new code QWidget *parent = nullptr is highly recommended.

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

                              @Wieland

                              Whenever ~HexSpinBox() is called the object gets destroyed by the C++ runtime. Calling delete on the object inside the destructor yourself is a bug.

                              Yes, thanks. I changed it to:

                              HexSpinBox::~HexSpinBox() { }
                              

                              and it's working properly apparently.

                              BTW, although valid code, for new code QWidget *parent = nullptr is highly recommended.

                              Agree. Especially in modern C++. :)

                              1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @tomy You should read http://doc.qt.io/qt-5.9/objecttrees.html
                                By QObject I mean any class derived from QObject.

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

                                @jsulm
                                Thanks, read.

                                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