Migration von QT3 nach Freepascal

Für Fragen zur Programmiersprache auf welcher Lazarus aufbaut
Antworten
mtbf40
Beiträge: 72
Registriert: Do 3. Nov 2011, 16:39

Migration von QT3 nach Freepascal

Beitrag von mtbf40 »

Hallo,

ich habe ein Projekt für eine Migration von QT3 nach Freepascal. Ich habe jetzt folgendes Problem: es geht um Typisierung

hier der Code aus QT

Code: Alles auswählen

 
# dies ist eine zusammengefaßte Auflistung 
 
class CPI_SingleValue
{
public:
    CPI_SingleValue(){};
    ~CPI_SingleValue(){};
 
    QString VALUE;
    QString DESIGN_ID;
    QString FK_FORMAT_ID;
    QString VALUE_ID;
};
#endif //CPI_SINGLEVALUE;
 
/** values description */
typedef QMap <int, CPI_SingleValue> _valuemap;
typedef QMap <int, _valuemap> _valueList;
 
_valueList valuedata;
 
_valuemap val = valuedata[query.value(1).toInt()];
CPI_SingleValue sval;
 
    sval.VALUE = query.value(crow++).toString();
    tmp.setNum(cdata.key());
    sval.FK_FORMAT_ID = tmp;
 
    sval.DESIGN_ID = query.value(1).toString();
    sval.VALUE_ID = query.value(crow++).toString();
    .
    .
 


hier der Code für FPC

Code: Alles auswählen

 
  TPI_SingleValue = class
    VALUE        : String;
    DESIGN_ID    : String;
    FK_FORMAT_ID : String;
    VALUE_ID     : String;
  end;
  TvalueMap  = specialize TFPGObjectList<TPI_SingleValue>;
  TvalueList = specialize TFPGList<TvalueMap>; //???????????
    .
    . 
 
var
  valuedata : TvalueList;
  val : TvalueMap;
  sval : TPI_SingleValue;
    .
    .
  val.add(valuedata.Add(piQuery.Fields[1].AsString)).ToString;
### Fehler: Error: Incompatible type for arg no. 1: Got "AnsiString", expected "TFPGObjectList<piedit.TPI_SingleValue>"
# Das Query ist eine DB Abfrage
 


Frage: wie muss die Typdeklaration aussehen das das funktioniert??

Viele Grüße

Socke
Lazarusforum e. V.
Beiträge: 3158
Registriert: Di 22. Jul 2008, 19:27
OS, Lazarus, FPC: Lazarus: SVN; FPC: svn; Win 10/Linux/Raspbian/openSUSE
CPU-Target: 32bit x86 armhf
Wohnort: Köln
Kontaktdaten:

Re: Migration von QT3 nach Freepascal

Beitrag von Socke »

Du musst in diesem Fall zuerst eine Objektinstanz erstellen

Code: Alles auswählen

var sval: TPI_SingleValue;
begin
  sval := TPI_SingleValue.Create;
  sval.VALUE := piQuery.Fields[1].AsString;
  valuedata := TValueList.Create();
  valuedata.Add(sval);
  sval := nil; // Objekt ist jetzt nur noch über Liste zugreifbar
  val.Add(valuedata); // Liste ist jetzt nur noch über Map zugreifbar.
end


TvalueList würde ich ebenfalls als Objekt-Liste auslegen:

Code: Alles auswählen

TvalueList = specialize TFPGObjectList<TvalueMap>;


Am Ende darfst du natürlich nicht vergessen, die Objekte alle wieder freizugeben. In Pascal wird der Destruktor nicht automatisch aufgerufen.
MfG Socke
Ein Gedicht braucht keinen Reim//Ich pack’ hier trotzdem einen rein

mtbf40
Beiträge: 72
Registriert: Do 3. Nov 2011, 16:39

Re: Migration von QT3 nach Freepascal

Beitrag von mtbf40 »

ich habe jetzt noch ein anders Problem an dem ich scheitere:

folgende Situation:
  • ich habe eine Unit_1 mit TForm1 -> mit Connect zu einer DB + SQL-Query + Dataset + xxx
  • ich habe eine Unit_2 mit einer Klasse die in die Unit_1 eingebunden ist
  • jetzt möchte ich in der Unit_2 ein SQL-Query absetzten mit der DB-Connection aus Unit_1 - das bekomme ich nicht hin Problem_1 gelöst
  • Problem_2 sind die Pointer... in welcher Rehenfolge man die setzt

Code: Alles auswählen

 
unit piedit;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  x,y,z,
  piHistory,pi_value;
 
type
 
  { TForm1 }
 
  TForm1 = class(TForm)
          .
    piDataSource: TDataSource;
    pi_connect: TZConnection;
    piQuery: TZQuery;
    PIHistory : TPIHistory;
          .
  private
          .
    procedure GetTable;
          .
  public
 
implementation
 
procedure TForm1.GetTable;
var
  xy...
  historydata : T_valueHistory;
begin
  if not piQuery.IsEmpty then
  begin
    piGrid_Data.Open;
   //fülle Listen u.s.w.
    while not piQuery.EOF do
    begin
      if cb_History.Checked then
        // gehe in Unit piHistory und fülle <historydata>  -> Liste
        //historydata ist ein Pointer in QT3
        //PIHistory.setValue(&historydata, sval.VALUE_ID); QT3
        //historydata wird später gebraucht um ein GRID zu füllen
        PIHistory.setValue(historydata,sval.VALUE_ID);
    end;
   PIHistory.Load
  end;
end;
 


Code: Alles auswählen

 
unit piHistory;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  Classes, SysUtils, fgl, ZDataset,
  pi_value;
 
type
  TPIHistory = Class
    public
      Tvaluestack   : T_valuestack;
      TvalueHistory : T_valueHistory;
      procedure run;
      procedure setValue(var historydata : T_valueHistory; Design : String);
      procedure Load;
    private
      m_phistorydata : ^T_valueHistory; //Problem_2
      m_pDesign : TStringList;
      m_ValueID : String;
      piQuery_history : TZQuery;
  end;
 
implementation
//Problem_1 gelöst
uses
  piedit;
 
procedure TPIHistory.run;
begin
  Load;
end;
 
procedure TPIHistory.Load;
var
  vString : String;
  s_it    : String;
  i       : Integer;
  item    : TPI_SingleValueHistory;
  hline   : T_valuestack;
begin
  for i:=0 to m_pDesign.Count -1 do
  begin
    hline := T_valuestack.Create(false);
    item  := TPI_SingleValueHistory.Create;
    m_ValueID := m_pDesign[i];
    vString := ' SELECT H.FK_VALUE_ID, V.FK_DESIGN_ID, H.VALUE, H.FK_FORMAT_ID, H.DATE_TIME, U.NAME  FROM PI_VALUE V, PI_HISTORY H, USER_NAMES U WHERE H.FK_VALUE_ID =' + m_ValueID + ' AND H.FK_VALUE_ID = V.VALUE_ID AND H.FK_USER_ID = U.USER_ID ORDER BY HISTORY_ID DESC';
 
    piQuery_history.SQL.Text:=vString;
    piQuery_history.Connection:=Form1.pi_connect;
    piQuery_history.Open;
 
    if not piQuery_history.IsEmpty then
    begin
      while not piQuery_history.EOF do
      begin
        item.FK_FORMAT_ID:='23456';
        item.VALUE:='test';
        item.DESIGN_ID:='344556';
        item.VALUE_ID:='344667';
        hline.Add(item);
        //fülle Liste
        m_phistorydata^.Add(piQuery_history.Fields[0].AsInteger,hline);
      end;
    end;
  end;
end;
 
// set history values
procedure TPIHistory.setValue(var historydata : T_valueHistory; Design : String);
begin
  m_pDesign := TStringList.Create();
  m_phistorydata^ := T_valueHistory.Create//ERROR????? Problem_2
  m_phistorydata := @historydata;            //????? Problem_2
  m_pDesign.Add(Design);
  m_ValueID := Design;
end;
 
end.
 


Code: Alles auswählen

 
unit pi_value;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  Classes, SysUtils, fgl;
 
type
  TPI_SingleValue = class
    VALUE        : String;
    DESIGN_ID    : String;
    FK_FORMAT_ID : String;
    VALUE_ID     : String;
    constructor Create;
    destructor Destroy; override;
  end;
 
  TPI_SingleValueHistory = class(TPI_SingleValue)
    DATE : String;
    USER : String;
  end;
 
  T_valuestack   = specialize TFPGObjectList<TPI_SingleValueHistory>;
  T_valueHistory = specialize TFPGMap<integer, T_valuestack>;
 
implementation
 
  constructor TPI_SingleValue.Create;
  begin
    inherited;
    TPI_SingleValue.Create;
  end;
 
  destructor TPI_SingleValue.Destroy;
  begin
    FreeAndNil(Self);
    inherited;
  end;
 
end.
 


QT3 cpp

Code: Alles auswählen

 
----header von CPIHistory-----
class CPIHistory : public QThread
{
public:
   // CPI_SingleValueHistory <->TPI_SingleValueHistory
    typedef Q3ValueList <CPI_SingleValueHistory> _valuestack;
    typedef QMap <int, _valuestack> _valueHistory;
    CPIHistory();
    ~CPIHistory();
    void run();
    void setValue(_valueHistory * historydata, QString  Design);
    void Load();
private:
    QMutex m_mutex;
 
    _valueHistory * m_phistorydata;
    QStringList m_pDesign;
    QString m_ValueID;
};
 
-------cpp-------
void CPIHistory::setValue(_valueHistory * historydata , QString  value_id)
{
    QMutexLocker locker( &m_mutex );
    m_phistorydata = historydata;
    m_pDesign.push_back(value_id);
    m_ValueID  = value_id;
}
 
void CPIHistory::Load()
{
        .
        .
       while ( query.next() )
      {
      //    qDebug("history "+ vString);         
          _valuestack hline;
          _valueHistory::Iterator hline_it = m_phistorydata->find( m_pDesign.first().toInt() );
 
          if( hline_it != m_phistorydata->end() )
          {
         hline = (*hline_it);
          }
 
          CPI_SingleValueHistory  item;
 
          item.VALUE = query.value(2).toString();
          item.DESIGN_ID = query.value(1).toString();
          item.FK_FORMAT_ID = query.value(3).toString();
          item.VALUE_ID = query.value(0).toString();
          item.DATE = query.value(4).toString();
          item.USER = query.value(5).toString();
 
          hline.push_back(item);
          m_phistorydata->insert(query.value(0).toInt(), hline);
      }
 
      m_pDesign.pop_front();
}


Fazit: ich bin mit meinem freepascal Latein am Ende :roll: :?: :?: :roll:

Antworten