Unsolved Functions called unclearly
-
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. -
@Chris-Kawa
are those functions overridden? -
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; //...
-
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 inHexSpinBox(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?
-
@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. -
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.
-
@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!
-
@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. -
@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? -
@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 -
@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.
-
@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.
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.
-
@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. -
@tomy You should read http://doc.qt.io/qt-5.9/objecttrees.html
By QObject I mean any class derived from QObject. -
@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++. :)
-
@jsulm
Thanks, read.