Unsolved Functions called unclearly
-
@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.