* TCustomComboBox memleaks fix, reimplemented private TStringList handling.

git-svn-id: trunk@11524 -
This commit is contained in:
zeljko 2007-07-16 13:28:33 +00:00
parent 72d601e943
commit 4fbaaf4f4e
3 changed files with 80 additions and 31 deletions

View File

@ -618,6 +618,10 @@ begin
end;
constructor TQtComboStrings.Create(ComboBoxH: QComboBoxH; TheOwner: TWinControl);
var
AQList: QStringListH;
i: Integer;
Str: WideString;
begin
inherited Create;
@ -629,11 +633,29 @@ begin
FStringList := TStringList.Create;
FQtComboBox := ComboBoxH;
FStringList.Text := TCustomComboBox(TheOwner).Items.Text;
FOwner:=TheOwner;
AQList := QStringList_create;
try
for i := 0 to FStringList.Count - 1 do
begin
Str := UTF8Encode(FStringList.Strings[i]);
QStringList_append(AQList, @Str);
end;
QComboBox_addItems(FQtComboBox, AQList);
finally
QStringList_destroy(AQList);
end;
FOwner := TheOwner;
end;
destructor TQtComboStrings.Destroy;
begin
Clear;
FStringList.Free;
inherited Destroy;
end;
@ -646,7 +668,10 @@ procedure TQtComboStrings.Clear;
begin
FUpdating := True;
FStringList.Clear;
QComboBox_clear(FQtComboBox);
if not (csDestroying in FOwner.ComponentState)
and not (csFreeNotification in FOwner.ComponentState)
then
QComboBox_clear(FQtComboBox);
FComboChanged := False;
FUpdating := False;
IsChanged;
@ -659,12 +684,18 @@ begin
if Index < FStringList.Count then
begin
FStringList.Delete(Index);
ExternalUpdate(FStringList,True);
FComboChanged := False;
QComboBox_removeItem(FQtComboBox, Index);
FUpdating := False;
IsChanged;
FUpdating := False;
FComboChanged:=False;
end;
end;
procedure TQtComboStrings.Insert(Index: integer; const S: string);
var
Str: WideString;
data: QVariantH;
begin
if FComboChanged then InternalUpdate;
@ -672,9 +703,18 @@ begin
if Index <= FStringList.Count then
begin
FStringList.Insert(Index,S);
ExternalUpdate(FStringList,True);
FComboChanged := False;
FStringList.Insert(Index, S);
Str := UTF8Encode(S);
data := QVariant_create(0); //Creates dummy data
try
QComboBox_insertItem(FQtComboBox, Index, @Str, Data);
finally
QVariant_destroy(data);
end;
FUpdating := False;
IsChanged;
FUpdating := False;
FComboChanged:=False;
end;
end;

View File

@ -429,6 +429,8 @@ type
protected
function CreateWidget(const AParams: TCreateParams):QWidgetH; override;
public
FList: TStrings;
FSavedItemIndex: Integer;
destructor Destroy; override;
procedure SetColor(const Value: PQColor); override;
function currentIndex: Integer;
@ -3838,32 +3840,13 @@ function TQtComboBox.CreateWidget(const AParams: TCreateParams): QWidgetH;
var
Parent: QWidgetH;
Str: WideString;
i: Integer;
data: QVariantH;
begin
{------------------------------------------------------------------------------
Creates dummy data
This data is required, passing nil to QComboBox_addItem will cause a crash
------------------------------------------------------------------------------}
data := QVariant_create(10);
// Creates the widget
{$ifdef VerboseQt}
WriteLn('TQtComboBox.Create');
{$endif}
Parent := TQtWidget(LCLObject.Parent.Handle).GetContainerWidget;
Result := QComboBox_create(Parent);
// Add the items to the combo box
for i := 0 to (LCLObject as TCustomComboBox).Items.Count - 1 do
begin
Str := UTF8Decode((LCLObject as TCustomComboBox).Items.Strings[i]);
QComboBox_addItem(QComboBoxH(Result), @Str, data);
end;
// Clean up
QVariant_destroy(data);
end;
{------------------------------------------------------------------------------

View File

@ -93,10 +93,10 @@ type
class procedure SetSelLength(const ACustomComboBox: TCustomComboBox; NewLength: integer); override;}
class procedure SetItemIndex(const ACustomComboBox: TCustomComboBox; NewIndex: integer); override;
{ class procedure SetMaxLength(const ACustomComboBox: TCustomComboBox; NewLength: integer); override;
class procedure SetStyle(const ACustomComboBox: TCustomComboBox; NewStyle: TComboBoxStyle); override;
class procedure SetReadOnly(const ACustomComboBox: TCustomComboBox; NewReadOnly: boolean); override;}
class procedure SetStyle(const ACustomComboBox: TCustomComboBox; NewStyle: TComboBoxStyle); override;}
class function GetItems(const ACustomComboBox: TCustomComboBox): TStrings; override;
class procedure SetReadOnly(const ACustomComboBox: TCustomComboBox; NewReadOnly: boolean); override;
// class procedure Sort(const ACustomComboBox: TCustomComboBox; AList: TStrings; IsSorted: boolean); override;
end;
@ -1327,6 +1327,13 @@ var
begin
QtComboBox := TQtComboBox.Create(AWinControl, AParams);
QtComboBox.AttachEvents;
// create our FList helper
QtComboBox.FList := TQtComboStrings.Create(QComboBoxH(QtComboBox.Widget), TCustomComboBox(AWinControl));
// we must save itemindex, since GetItems() should delete it.
QtComboBox.FSavedItemIndex := TCustomComboBox(AWinControl).ItemIndex;
Result := THandle(QtComboBox);
end;
@ -1364,20 +1371,39 @@ end;
class procedure TQtWSCustomComboBox.SetItemIndex(
const ACustomComboBox: TCustomComboBox; NewIndex: integer);
begin
TQtComboBox(ACustomComboBox.Handle).setCurrentIndex(NewIndex);
TQtComboBox(ACustomComboBox.Handle).setCurrentIndex(TQtComboBox(ACustomComboBox.Handle).FSavedItemIndex);
TQtComboBox(ACustomComboBox.Handle).FSavedItemIndex := NewIndex;
end;
{------------------------------------------------------------------------------
Method: TQtWSCustomComboBox.GetItems
Params: None
Returns: The state of the control
Returns: ComboBox items
------------------------------------------------------------------------------}
class function TQtWSCustomComboBox.GetItems(const ACustomComboBox: TCustomComboBox): TStrings;
var
ComboBoxH: QComboBoxH;
begin
if not Assigned(TQtComboBox(ACustomComboBox.Handle).FList) then
begin
ComboBoxH := QComboBoxH((TQtComboBox(ACustomComboBox.Handle).Widget));
TQtComboBox(ACustomComboBox.Handle).FList := TQtComboStrings.Create(ComboBoxH, ACustomComboBox);
end;
{TODO: ask why GetItems() always clears the list, that's why FSavedItemIndex exists. }
Result := TQtComboBox(ACustomComboBox.Handle).FList;
end;
{------------------------------------------------------------------------------
Method: TQtWSCustomComboBox.SetReadOnly
Params: None
Returns: Nothing
------------------------------------------------------------------------------}
class procedure TQtWSCustomComboBox.SetReadOnly(const ACustomComboBox: TCustomComboBox; NewReadOnly: boolean);
var
ComboBoxH: QComboBoxH;
begin
ComboBoxH := QComboBoxH((TQtWidget(ACustomComboBox.Handle).Widget));
Result := TQtComboStrings.Create(ComboBoxH, ACustomComboBox);
QComboBox_setEditable(ComboBoxH, not NewReadOnly);
end;
initialization