在运行时分配已发布属性的确切时间?
When exactly are published properties assigned at run time?
我有一个自定义控件,该控件在运行时加载时需要根据已发布的属性执行一些操作。但是,我遇到了一个问题,每当我检查已发布的属性时,它还没有设置,并且始终是默认值。
我首先尝试检查控件的构造函数中的属性,但很快发现它们尚未加载。 我知道当控件显示在屏幕上时,属性设置正确,因此根本不加载属性的问题。
我接下来尝试覆盖加载的方法,但仍然遇到同样的问题,所以我认为这不是我正在寻找的。
void __fastcall TFmSearchBar::Loaded()
{
TEdit::Loaded(); //call base class loaded
if( MyProperty )
{
//do stuff
}
}
这些已发布的属性实际上是在什么时候设置的?
一旦正确设置了属性,我可以/应该挂钩到什么方法,以便基于这些属性在我的控件中执行一些逻辑?
如果我在控件的构造函数中检查该属性,则该属性始终是默认值,即使我在设计器中另行指定也是如此。
正确,因为尚未分配其设计时值。
这些已发布的属性实际上是在什么时候设置的?
构造所有者(窗体、框架或数据模块)时。 它加载自己的 DFM 资源并对其进行解析,构造存储的子组件并读取其属性值。
例如,假设您有以下 DFM:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
...
object Edit1: TEdit
Left = 136
Top = 64
Width = 121
Height = 21
TabOrder = 0
end
object Button1: TButton
Left = 263
Top = 62
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
end
end
DFM 流式处理过程大致转换为以下等效代码(为简单起见,我省略了许多内部细节):
__fastcall TCustomForm::TCustomForm(TComponent *Owner)
: TScrollingWinControl(Owner)
{
this->FFormState << fsCreating;
try
{
// locate, load, and parse the "Form1" DFM resource ...
this->FComponentState << csLoading;
this->Parent = ...;
this->Name = L"Form1":
this->FComponentState << csReading;
this->Left = 0;
this->Top = 0;
this->Caption = L"Form1";
...
TEdit *e = new TEdit(this);
try
{
e->FComponentState << csLoading;
e->Parent = this;
e->Name = L"Edit1"; // <-- sets the derived Form's 'Edit1' member to this object
e->FComponentState << csReading;
e->Left = 136;
e->Top = 64;
e->Width = 121;
e->Height = 21;
e->TabOrder = 0;
e->FComponentState >> csReading;
}
catch (...)
{
delete e;
throw;
}
TButton *b = new TButton(this);
try
{
b->FComponentState << csLoading;
b->Parent = this;
b->Name = L"Button1"; // <-- sets the derived Form's 'Button1' member to this object
b->FComponentState << csReading;
b->Left = 263;
b->Top = 62;
b->Width = 75;
b->Height = 25;
b->Caption = L"Button1";
b->TabOrder = 1;
b->FComponentState >> csReading;
}
catch (...)
{
delete b;
throw;
}
this->FComponentState >> csReading;
...
e->Loaded();
b->Loaded();
this->Loaded();
}
__finally
{
this->FFormState >> fsCreating;
}
}
因此,如您所见,当调用组件的构造函数时,组件的属性值尚不可用。
一旦正确设置了属性,我可以/应该挂钩到什么方法,以便基于这些属性在我的控件中执行一些逻辑?
这取决于属性需要做什么。 如果他们需要立即执行操作,您可以直接在其属性库中执行此操作。 但是,如果他们需要等到首先加载其他属性(如果一个属性依赖于另一个属性的值),则改为重写虚拟Loaded()
方法,该方法将在 DFM 流式处理完成后自动调用。 属性资源库可以检查ComponentState
属性的标志,以了解组件当前是否在设计时在窗体设计器中运行、当前是否正在流式传输 DFM 等,然后根据需要采取相应的操作。
我尝试覆盖加载的方法,但仍然遇到同样的问题
这到底是什么? 你没有解释你的实际问题是什么。 请编辑您的问题以提供这些详细信息。
所以我不认为这正是我想要的。
很可能是,您可能只是没有正确使用它。
- C++为构建时间获取QDateTime的可靠方法
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- 使用简单类型列表实现的指数编译时间.为什么
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- C++概念:如何使用'concept'检查模板化结构的属性?
- 在已经使用Git的情况下减少编译时间
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 通过指向指针数组的指针访问子类的属性
- 从文本文件中读取时钟时间和事件时间并进行处理
- MSVC是否支持C++11样式的属性而不是__declspec
- 具有未知值时的时间复杂性
- 如何减少花费的时间
- C++在变量给定的指定时间内关闭电脑
- 在运行时分配已发布属性的确切时间?
- 按时间间隔分析音频数据的属性