简体   繁体   English

有没有办法将此delphi代码转换为c ++ builder

[英]Is there a way to convert this delphi code to c++builder

In most cases I am able to convert Delphi to C++, but this one gives me some headache. 在大多数情况下,我能够将Delphi转换为C ++,但这使我有些头痛。 Maybe some of you could help. 也许有些人可以帮忙。

As seen in this link here , which references some new functions on TListView in Embarcadero (FMX). 可以看出在这个环节在这里 ,它引用上的TListView在Embarcadero公司(FMX)一些新的功能。 As I am much more comfortable with C++ than Delphi I use C++Builder. 因为我对C ++的了解比对Delphi的了解要多得多,所以我使用C ++ Builder。 In most cases this is quite Ok to translate and understand, and find workarounds. 在大多数情况下,翻译和理解并找到解决方法是完全可以的。 But here I am stuck: 但是在这里我被困住了:

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. 

The section I am struggling with is 我正在努力的部分是

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;

How is this translated, or is this functionality which simply doesn't exists in c++ ? 它是如何翻译的,或者是c ++中根本不存在的功能?

With kind of introduces an unnamed variable and a scope for it. With样的介绍一位不愿透露姓名的变量,并为它的范围。 In C++ you have to be explicit. 在C ++中,您必须明确。 The Delphi fragment is equivalent to 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;

(if I didn't mess up :-)). (如果我没有弄乱:-))。

When you want to add an item to a ListView you need to first create an item object (TListViewItem*) using Add() function that is child of TListView's Items property. 当您要将项目添加到ListView时,首先需要使用Add()函数创建项目对象(TListViewItem *),该函数是TListView的Items属性的子级。 Then, the Data property of item is expecting TValue, so you need to get TValue from a string or something else you want to put in the item. 然后,item的Data属性需要TValue,因此您需要从字符串或要放入该项目的其他内容中获取TValue。 Remember to use BeginUpdate() before fragment where you are adding items to ListView and EndUpdate() after to improve the performance of this operation. 请记住在片段之前使用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();

Try something like this: 尝试这样的事情:

// 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM