Recent

Author Topic: RTTI controls action  (Read 1532 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1752
RTTI controls action
« on: May 08, 2024, 07:09:59 am »
I'm developing an application that uses many RTTI controls. The basic skeleton is as follows.

Code: Pascal  [Select][+][-]
  1. unit uform2;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.    Classes, SysUtils, Forms, Controls, Graphics, Dialogs, RTTICtrls;
  9.  
  10. type
  11.    TQType = (qt1, qt2, qt3);
  12.    TMyObj = class
  13.        QType: TQType;
  14.        Value1,
  15.        value2,
  16.        value3 :  string;
  17.    end;
  18.  
  19.  
  20.    { TForm1 }
  21.  
  22.    TForm1 = class(TForm)
  23.       TIValue1: TTIEdit;
  24.       TIQtype: TTIRadioGroup;
  25.       procedure FormCreate(Sender: TObject);
  26.    private
  27.       MyObj: TMyObj;
  28.  
  29.       function getQType: TQType;
  30.       function getValue1: string;
  31.  
  32.       procedure setQType(AValue: TQType);
  33.       procedure setValue1 (AValue: string);
  34.  
  35.    published
  36.       property QType : TQType read getQType write setQType;
  37.       property Value1 : string read getValue1 write setValue1;
  38.    end;
  39.  
  40. var
  41.    Form1: TForm1;
  42.  
  43. implementation
  44.  
  45. {$R *.lfm}
  46.  
  47. { TForm1 }
  48.  
  49. procedure TForm1.FormCreate(Sender: TObject);
  50. begin
  51.    MyObj := TMyObj.create;
  52.    with MyObj do begin
  53.       QType:= qt2;
  54.       Value1 := 'v1';
  55.       Value2 := 'v2';
  56.       Value3 := 'v3';
  57.    end;
  58.  
  59.    TIQType.Link.TIObject := Self;
  60.    TIQType.Link.TIPropertyName := 'QType';
  61.  
  62.    TIValue1.Link.TIObject := Self;
  63.    TIValue1.Link.TIPropertyName := 'Value1';
  64. end;
  65.  
  66. function TForm1.getQType: TQType;
  67. begin
  68.   if Assigned(MyObj) then Result := MyObj.QType;
  69. end;
  70.  
  71. function TForm1.getValue1: string;
  72. begin
  73.   if Assigned(MyObj) then begin
  74.       case MyObj.QType of
  75.           qt1: Result := MyObj.Value1;
  76.           qt2: Result := MyObj.Value2;
  77.           qt3: Result := MyObj.Value3;
  78.       end;
  79.   end;
  80. end;
  81.  
  82. procedure TForm1.setQType(AValue: TQType);
  83. begin
  84.    if Assigned(MyObj) then MyObj.QType := AValue;
  85. end;
  86.  
  87. procedure TForm1.setValue1(AValue: string);
  88. begin
  89.    if Assigned(MyObj) then begin
  90.        case MyObj.QType of
  91.            qt1: MyObj.Value1 := Avalue;
  92.            qt2: MyObj.Value2 := Avalue;
  93.            qt3: MyObj.Value3 := Avalue;
  94.        end;
  95.    end;
  96. end;
  97.  
  98. end.
  99.  

Here, the key is that TIEdit1's content is changed automatically as I change the qtype from TIQType, which is a TIRadioGroup. It seems to call setQType getValue1, even though  do not call setValue1 in the setQType myself. How does this operate?
« Last Edit: May 08, 2024, 09:51:44 am by egsuh »

Thaddy

  • Hero Member
  • *****
  • Posts: 18765
  • To Europe: simply sell USA bonds: dollar collapses
Re: RTTI controls action
« Reply #1 on: May 08, 2024, 09:07:59 am »
If the rtti that is generated has getters and setters for the QType property then it will be called through essentially invoke, afaiu.
I reconstructed your code without changes, observed the same, but i still have to run it with the debugger.
« Last Edit: May 08, 2024, 09:10:49 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

egsuh

  • Hero Member
  • *****
  • Posts: 1752
Re: RTTI controls action
« Reply #2 on: May 08, 2024, 09:24:05 am »
Quote
If the rtti that is generated has getters and setters for the QType property then it will be called through essentially invoke, afaiu.
I reconstructed your code without changes, observed the same, but i still have to run it with the debugger.

Thank you, Thaddy. I prefer the current result --- actually this is very valuable, but want to know how it operates.

egsuh

  • Hero Member
  • *****
  • Posts: 1752
Re: RTTI controls action
« Reply #3 on: May 11, 2024, 01:30:11 pm »
I modified above code a little bit. I used TRadioGroup instead of TTIRadioGroup, and still it works. I mean, when the radiogroup's value changes, the Rtti control's value follows.


Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.    MyObj := TMyObj.create;
  4.    with MyObj do begin
  5.       QType:= qt2;
  6.       Value1 := 'v1';
  7.       Value2 := 'v2';
  8.       Value3 := 'v3';
  9.    end;
  10.  
  11.   // TIQType.Link.TIObject := Self;
  12.   // TIQType.Link.TIPropertyName := 'QType';
  13.  
  14.    with RadioGroup1.Items do begin  //--  these are added
  15.       Clear;
  16.       Add('Q1');
  17.       Add('Q2');
  18.       Add('Q3');
  19.    end;
  20.  
  21.    TIValue1.Link.TIObject := Self;
  22.    TIValue1.Link.TIPropertyName := 'Value1';
  23. end;
  24.  
  25. // ---------- and following procedure was addded --------------
  26. procedure TForm1.RadioGroup1Click(Sender: TObject);
  27. begin
  28.    if Assigned(MyObj) then MyObj.QType:= TQType(RadioGroup1.ItemIndex);
  29. end;
  30.  

So, does this mean that any  RTTI control checks changes of other properties of its TIObjects?  In my example, the TIEdit1 must be observing the changes in the value of property QType.

egsuh

  • Hero Member
  • *****
  • Posts: 1752
Re: RTTI controls action
« Reply #4 on: May 12, 2024, 02:33:02 pm »
But this is not reliable. When there are multiple RTTI controls in one form, which change values depending on the state of another control (e.g. clicking radio button, etc.), some of them change values automatically while others do not. I have to use "LoadfromProperty", like following:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.RadioGroup1Click(Sender: TObject);
  2. begin
  3.    if Assigned(MyObj) then begin
  4.       MyObj.QType:= TQType(RadioGroup1.ItemIndex);
  5.       TIValue1.Link.LoadFromProperty;
  6.    end;
  7. end;

But in this case, it is more like loading values manually to non-RTTI controls.

 

TinyPortal © 2005-2018