Recent

Author Topic: Event value disappeared in the object inspector  (Read 2182 times)

alpine

  • Hero Member
  • *****
  • Posts: 1171
Event value disappeared in the object inspector
« on: February 14, 2024, 04:31:48 pm »
I have a component containing an event property with a setter. When I install the component into the IDE, the property value disappears each time when I reload the project.

Consider the following source (example package and project attached):
Code: Pascal  [Select][+][-]
  1. unit Shape1;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TShape1 }
  13.  
  14.   TShape1 = class(TShape)
  15.   private
  16.     FOnWhatever: TNotifyEvent;
  17.     procedure SetOnWhatever(AValue: TNotifyEvent);
  18.   public
  19.     procedure DoWhatever;
  20.   published
  21.     property OnWhatever: TNotifyEvent read FOnWhatever write SetOnWhatever;
  22.   end;
  23.  
  24. procedure Register;
  25.  
  26. implementation
  27.  
  28. procedure Register;
  29. begin
  30.   {$I shape1_icon.lrs}
  31.   RegisterComponents('Misc',[TShape1]);
  32. end;
  33.  
  34. { TShape1 }
  35.  
  36. procedure TShape1.SetOnWhatever(AValue: TNotifyEvent);
  37. begin
  38.   if FOnWhatever=AValue then Exit; // <--- Comment this, troubles the obj.inspector
  39.   FOnWhatever:=AValue;
  40. end;
  41.  
  42. procedure TShape1.DoWhatever;
  43. begin
  44.   if Assigned(FOnWhatever) then
  45.     FOnWhatever(Self);
  46. end;
  47.  
  48. end.
The example project compiles and works as expected, i.e. the event property have the proper value, only it is lost into the object inspector.
When I comment line #38 above, the issue disappears.

Anyone have  an idea?

Found this: https://forum.lazarus.freepascal.org/index.php/topic,29609.0.html but no explanation there.
My Laz is rather old, but anyway: Lazarus 2.2.4 (rev lazarus_2_2_4) FPC 3.2.2 x86_64-linux-gtk2.
« Last Edit: February 14, 2024, 04:43:13 pm by alpine »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 15192
  • Censorship about opinions does not belong here.
Re: Event value disappeared in the object inspector
« Reply #1 on: February 14, 2024, 05:36:01 pm »
maybe:
Code: Pascal  [Select][+][-]
  1. property OnWhatever: TNotifyEvent read FOnWhatever write SetOnWhatever;stored;
And or compile the new class in{$M+} state.
Code: Pascal  [Select][+][-]
  1. //snip
  2.   {$M+}
  3.   TShape1 = class(TShape)
{$M+} is a local directive, so you should define it anyway on a per unit and/or per class basis.
« Last Edit: February 14, 2024, 05:42:39 pm by Thaddy »
Of course the national anthem of the U.S.A. was written by Jimi Hendrix, didn't you know that?

alpine

  • Hero Member
  • *****
  • Posts: 1171
Re: Event value disappeared in the object inspector
« Reply #2 on: February 14, 2024, 05:56:28 pm »
maybe:
Code: Pascal  [Select][+][-]
  1. property OnWhatever: TNotifyEvent read FOnWhatever write SetOnWhatever;stored;
And or compile the new class in{$M+} state.
Code: Pascal  [Select][+][-]
  1. //snip
  2.   {$M+}
  3.   TShape1 = class(TShape)
{$M+} is a local directive, so you should define it anyway on a per unit and/or per class basis.
Thanks for the reply!

However, the property value is stored. I can see it into the .lfm text. It is also loaded properly, since when rebuilt the executable works as expected, i.e. the event handler is called.
IMHO the issue is in the object inspector itself. It simply does not display the property value after the project reload.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Josh

  • Hero Member
  • *****
  • Posts: 1315
Re: Event value disappeared in the object inspector
« Reply #3 on: February 14, 2024, 06:11:44 pm »
have you tried uninstalling/removing your component from the ide, making sure you rebuild and restart the ide to complete the removal.
Then re-install the component and rebuild and restart the ide.

The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Thaddy

  • Hero Member
  • *****
  • Posts: 15192
  • Censorship about opinions does not belong here.
Re: Event value disappeared in the object inspector
« Reply #4 on: February 14, 2024, 06:20:51 pm »
Yes, if it is a component you need to rebuild the ide, but do not forget {$M+}
Of course the national anthem of the U.S.A. was written by Jimi Hendrix, didn't you know that?

alpine

  • Hero Member
  • *****
  • Posts: 1171
Re: Event value disappeared in the object inspector
« Reply #5 on: February 14, 2024, 06:38:25 pm »
have you tried uninstalling/removing your component from the ide, making sure you rebuild and restart the ide to complete the removal.
Then re-install the component and rebuild and restart the ide.
I haven't tried exactly that, but since I'm modifying an existing and installed package, I'm constantly doing a "Build Lazarus with Profile:" on each change. When I experienced troubles, I have installed the package on my Winbox Lazarus (currently I'm on Linux) and there the behavior was the same. Then I've made a small, isolated package - the one attached - and tried it on both Win/Linux installations - again - the same.

About the {M+} - it is not the only property in the original component, I believe that without RTTI no property will be shown at all in the inspector, and all the components are TPersistent descendants - docs says the {M+} is implied.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 15192
  • Censorship about opinions does not belong here.
Re: Event value disappeared in the object inspector
« Reply #6 on: February 14, 2024, 07:13:34 pm »
You HAVE to rebuild the Lazarus IDE. There is no option yet to get around that. There will be in the future.
Of course the national anthem of the U.S.A. was written by Jimi Hendrix, didn't you know that?

wp

  • Hero Member
  • *****
  • Posts: 12143
Re: Event value disappeared in the object inspector
« Reply #7 on: February 14, 2024, 07:29:06 pm »
In TAChart there's this:
Code: Pascal  [Select][+][-]
  1. procedure TChart.SetOnAfterCustomDrawBackground(AValue: TChartAfterCustomDrawEvent);
  2. begin
  3.   if TMethod(FOnAfterCustomDrawBackground) = TMethod(AValue) then exit;
  4.   ...
  5. end;

When I apply this (and implement the TMethod compare operator) your component works:
Code: Pascal  [Select][+][-]
  1. operator = (const A, B: TMethod): Boolean;
  2. begin
  3.   Result := (A.Code = B.Code) and (A.Data = B.Data);
  4. end;
  5.  
  6. procedure TShape1.SetOnWhatever(AValue: TNotifyEvent);
  7. begin
  8.   if TMethod(FOnWhatever) = TMethod(AValue) then exit;
  9.   FOnWhatever:=AValue;
  10. end;
  11.  

n7800

  • Full Member
  • ***
  • Posts: 114

alpine

  • Hero Member
  • *****
  • Posts: 1171
Re: Event value disappeared in the object inspector
« Reply #9 on: February 14, 2024, 10:08:58 pm »
I just wanted to give a link:
https://wiki.freepascal.org/IDE_tricks#Events_.28Method_properties.29_in_the_Object_Inspector
Thanks, that makes the things clear. I didn't know it.

@wp
No matter how confident I can feel, there is always something more that can be learned. It's not an important one but it is of those that can ruin your whole afternoon.
Thanks a lot, pal.
« Last Edit: February 15, 2024, 05:36:10 am by alpine »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Bart

  • Hero Member
  • *****
  • Posts: 5317
    • Bart en Mariska's Webstek
Re: Event value disappeared in the object inspector
« Reply #10 on: February 14, 2024, 10:54:58 pm »
B.t.w.: for events it's rather customary to have it like:
Code: Pascal  [Select][+][-]
  1.   property OnSomething: TNotifyEvent read FOnSomething write FOnSomething;

Bart

 

TinyPortal © 2005-2018