Главная страница | назад





Article #27039: Setting a Kylix TListView component's subitem images to nothing

Question: In Kylix, I have a TListView with a ViewStyle of vsReport that has the Images property set to a valid TImageList. Whenever I add subitems to an Item in the TListView, they have images (icons) set to them. How can I prevent this from happening?

Answer: This is due to a bug in the CLX VCL. There are two workarounds that you can do. First, you can modify the VCL source. To do this, open up QComCtrls.pas and save it in your projects current directory. Then, look for the following procedure and add the suggested lines of code:

procedure TCustomViewItem.ImageIndexChange(ColIndex, NewIndex: Integer);
var
Pixmap: QPixmapH;
begin
if ViewControlValid and HandleAllocated and Assigned(ViewControl.Images) then
begin
Pixmap := ViewControl.Images.GetPixmap(NewIndex);
if Assigned(Pixmap) then
QListViewItem_setPixmap(Handle, ColIndex, Pixmap)
// Remove the ; from the last line and add the following fix:
else
begin
Pixmap := QPixmap_create;
QListViewItem_setPixmap(Handle, ColIndex, Pixmap);
QPixmap_destroy(Pixmap);
end;
// End of the fix
end;
end;

The above solution will not work if you build with runtime packages. The fix for this must be done each time you add an item to the TListView. First, add Qt to your uses list, as we need to make some calls directly to the Qt library. Then, add the following work around whenever you add an Item to the TListView:

var
Pixmap: QPixmapH;
begin
with ListView.Items.Add do
begin
Caption := 'Some Caption for the Item';
SubItems.Add('Some sub item');
// Work around for a bug in setting the image to nothing
Pixmap := QPixmap_create;
QListViewItem_setPixmap(Handle, 1, Pixmap);
QPixmap_destroy(Pixmap);
// End work around for the bug
end;
end;
The 1 passed to QListViewItem_setPixmap is the index of the SubItem that you want fixed, with the first one being 1, the second 2, etc (so, it is NOT a 0-based index, but rather a 1-based index).

Last Modified: 09-NOV-01