That weird constructor
First of all, let me underline that the "weird constructor" mostly apply to visual controls (called widgets). Ordinary classes typically don't involve the anonymous procedure parameter, unless the class wraps some underlying API that is asynchronous or have special needs. An example of 'special needs' is the AddItem() method in TQTXListbox, but that is so that you can assign the content and do styling (if needed) as a part of the add procedure itself. It's actually a very clever way to write fast code. And you can pass NIL if you dont need the callback, just like in the constructors.
So, what exactly is this "weird" constructor thing? Let's look at the constructor for the form class:
constructor TQTXForm.Create(AOwner: TQTXComponent; CB: TQTXFormConstructor);
begin
inherited Create(AOwner, procedure (Widget: TQTXWidget)
begin
if assigned( CB ) then
CB( self );
end);
end;
If you look closer, you will notice that the constructor is not really that weird, it simply takes an anonymous procedure as a second parameter, allowing the caller to set properties as a part of the constructor itself. This can save a lot of cpu time since you can initialize a child widget ad-hoc with the values you want it to have.
The idea here is, that you can set properties and create child widgets inside this nested mechanism (that calls inherited), and as your final step you invoke the callback.
This ensures that child widgets have been created, before you allow the caller to adjust anything. And this type of code is something JavaScript loves and it optimizes the hell out of it.
There are some rules though! Adjusting properties is one thing, but obviously you cannot alter properties that are still in the process of being created. In some cases you might have to override the ObjectReady() procedure and do some tuning there, and in other cases its enough to just move the code after the inherited call above, like this:
constructor TQTXForm.Create(AOwner: TQTXComponent; CB: TQTXFormConstructor);
begin
inherited Create(AOwner, procedure (Widget: TQTXWidget)
begin
// init values and create child widgets here
if assigned( CB ) then
CB( self );
end);
// Access child widget here
end;
The ObjectReady() method is called after the constructor finished, so it's often the go-to spot for post adjustment of child widgets you have created.