با سلام
می خواستم بدونم نقش توابع ست و گت در کیوتی چیست ؟ با توجه به اینکه برای تغییر ساختار یک کنترل هم QML داریم و هم Stylesheet ؟
خیلی ممنون از کمکتون
Printable View
با سلام
می خواستم بدونم نقش توابع ست و گت در کیوتی چیست ؟ با توجه به اینکه برای تغییر ساختار یک کنترل هم QML داریم و هم Stylesheet ؟
خیلی ممنون از کمکتون
متوجه منظورت نشدم. میشه بگی کجا همچین چیزی دیدی؟ میشه کد رو کپی کنی؟
با تشکر
اینچیزی که گفتم رو در یک پی دی اف خوندم من نیز تازه کار هستم و برای متوجه شدن این مباحث یک خورده ای زمان می بره. این کدهاشه:
QString text()
{
return m_text;
}
void setText(Qstring newText)
{
m_text=newText;
}
اینم توضیحات پی دی اف:
برای اینکه برنامه نویس نتواند Property ها را دستی تغییر دهد و کنترل برنامه را بهم بریزد،
Property ها مستقیما قابل تغییر نیستند. برای تغییر هر متغیری که Property است، باید از تابع
استفاده کرد. بنابراین شما اصلا متغیر m_text مربوط به txtInput را نم یبینید، بلکه با تابع setText
مقدار آنرا تغییر می دهید و اگرم یخواستید مقدار آنرا بدانید یا جایی استفاده کنید، بازهم از تابع text
استفاده می کنید و هیچگاه مستقیما به m_text دسترسی ندارد.شاید به نظر برسد وجود این دو تابع معقول نیست و کارکردن مستقیم با متغیر ساده تر است،
اما معمولا توابع get و set دارای کدهای دیگری جهت کنترل برنامه هستند (مثلا hide نه تنها _ m
visible را false می کند، بلکه دکمه را یکبار دیگر نیز رسم می کند)
من دقیقا باز هم متوجه منظورتون نشدم!!
ولی در واقع ما بلاک هایی در C# به عنوان get و Set داشتیم که بطور مثال اینطور استفاده میشد:
privatestring _current_filename;
publicstring current_filename
{
get { return _current_filename; }
}
در مثال بالا اگر بنامه نویسی دیگه ای از کلاس من استفاده کنه دیگه نمیتونه به خصوصیت Current file name ، مقدار خاصی بده (برای جلوگیری از اشتباه در استفاده از این کلاس!!) و در صورتی که اینکار کنه با این خطا زیر مواجه میشه :
و برای Set :نقل قول:
Error 2 Property or indexer 'Map.Classes.Layer1.current_filename' cannot be assigned to -- it is read only G:\Program\C#\Map\Map\Form1.cs 63 13 Map
public
string x
{
set {
try
{
temp_x =
Convert.ToInt32(value);
}
//End Try
catch
{
err =
true; //Error
}
//End Catch
}
//End Set
}
//End Varible Block
(البته این دو مثال از دو کلاس جدا بود و ربطی به هم ندارند و فقط صرفا کاربرد get و Set رو نشون میده)
اما بعید میدونم همچین چیزی تو C++ وجود داشته باشه!؟
اگه داری از property ها توی Qt استفاده می کنی می تونی به آدرس زیر مراجعه کنی تا هم با سیستم property های Qt و همچنین نقش کامل توابع set , get توی درسترسی به property ها آشنا شی
http://doc.qt.digia.com/4.4/properties.html