在大多数情况下,我能够将Delphi转换为C ++,但是这个让我头疼。也许你们中的一些人可以提供帮助。
如此链接here所示,它引用了Embarcadero(FMX)中TListView的一些新功能。因为我比使用Delphi更熟悉C ++,所以使用C ++ Builder。在大多数情况下,翻译和理解并找到解决方法是非常好的。但在这里我被困住了:
procedure TForm1.FormCreate(Sender: TObject);
I: Integer;
begin
// ListView1 uses a classic Appearance
for I in [0..63] do
with ListView1.Items.Add do
begin
Text := Format('%d pages', [1000 + Random(1234567)]);
Detail := Format('%d kg of paper', [1000 + Random(1234)]);
ImageIndex := Random(ImageList1.Count);
end;
// ListView4 uses a dynamic appearance with items named
// Text1, Detail1, Portrait
for I in [0..63] do
with ListView4.Items.Add do
begin
Data['Text1'] := Format('%d pages', [1000 + Random(1234567)]);
Data['Detail1'] := Format('%d kg of paper', [1000 + Random(1234)]);
Data['Portrait'] := Random(ImageList1.Count);
end;
end;
end.
我正在努力的部分是
with ListView4.Items.Add do
begin
Data['Text1'] := Format('%d pages', [1000 + Random(1234567)]);
Data['Detail1'] := Format('%d kg of paper', [1000 + Random(1234)]);
Data['Portrait'] := Random(ImageList1.Count);
end;
这是如何翻译的,或者这个功能在c ++中根本不存在?
答案 0 :(得分:3)
With
引入了一个未命名的变量和范围。在C ++中,你必须是明确的。 Delphi片段相当于
var
li: TListItem;
begin
li := ListView4.Items.Add;
li.Data['Text1'] := Format('%d pages', [1000 + Random(1234567)]);
li.Data['Detail1'] := Format('%d kg of paper', [1000 + Random(1234)]);
li.Data['Portrait'] := Random(ImageList1.Count);
end;
(如果我没有搞砸: - ))。
答案 1 :(得分:2)
当你想要一个项目添加到ListView时,你需要首先使用Add()函数创建一个项目对象(TListViewItem *),该函数是TListView的Items属性的子项。 然后,item的Data属性期望TValue,因此您需要从字符串或其他想要放入项目的其他内容中获取TValue。 请记住在片段之前使用BeginUpdate(),然后将项目添加到ListView和EndUpdate(),以提高此操作的性能。
ListView4->BeginUpdate();
TListViewItem* item = ListView4->Items->Add();
UnicodeString string1 = "content of the String";
item->Data["Text1"] = TValue::From<UnicodeString>(string1);
item->Data["Detail1"] = TValue::From<UnicodeString>(string1);
item->Data["visitTime"] =TValue::From<int>(Random(ImageList1->Count))
ListView4->EndUpdate();
答案 2 :(得分:0)
尝试这样的事情:
// Never use the OnCreate event in C++,
// use the class constructor instead...
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
// ListView1 uses a classic Appearance
for(int i = 0; i < 64; ++i)
{
TListViewItem *Item = ListView1->Items->Add();
Item->Text = Format(L"%d pages", ARRAYOFCONST(( 1000 + Random(1234567) )) );
Item->Detail = Format(L"%d kg of paper", ARRAYOFCONST(( 1000 + Random(1234) )) );
Item->ImageIndex = Random(ImageList1->Count);
}
// ListView4 uses a dynamic appearance with items named
// Text1, Detail1, Portrait
for(int i = 0; i < 64; ++i)
{
TListViewItem *Item = ListView4->Items->Add();
Item->Data[L"Text1"] = Format(L"%d pages", ARRAYOFCONST(( 1000 + Random(1234567) )) );
Item->Data[L"Detail1"] = Format(L"%d kg of paper", ARRAYOFCONST(( 1000 + Random(1234) )) );
Item->Data[L"Portrait"] = Random(ImageList1->Count);
}
}