taaz30
المساهمات : 114 تاريخ التسجيل : 28/11/2008
| موضوع: [شرح] مشروع تطبيقي لتعلم Qt باستخدام QDevelop الأحد ديسمبر 28, 2008 11:37 am | |
| | |
|
taaz30
المساهمات : 114 تاريخ التسجيل : 28/11/2008
| موضوع: رد: [شرح] مشروع تطبيقي لتعلم Qt باستخدام QDevelop الأحد ديسمبر 28, 2008 11:45 am | |
|
الان نبدأ بكتابة الكود
اولا نحتاج ان نصرح declare عن الدالة في ملف
الراس header file
فيجب ان نضيف السطر التالي
- اقتباس :
- void addClicked();
فيصبح الملف هكذا
- اقتباس :
- #ifndef MAINWINDOWIMPL_H
#define MAINWINDOWIMPL_H // #include "ui_mainwindow.h" // class MainWindowImpl : public QMainWindow, public Ui::MainWindow { Q_OBJECT public: MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = 0 ); private slots: void addClicked(); }; #endif ويجب ان نربط حدث الضغط على الزر add بالدالة
addclicked
يكون الربط باضافة الكود التالي في ملف cpp للنافذة الرئيسية - اقتباس :
- connect(btAdd, SIGNAL(clicked()), this, SLOT(addClicked()));
الان نكتب الدالة addclicked
وهي كالاتي - اقتباس :
- void MainWindowImpl::addClicked()
{ if(edText->text() == "") // If there is no text then exit this function { return; } lbMyList->addItem(edText->text()); // Add the text typed to the list edText->setText(""); // erase the text from the box they typed in edText->setFocus(); // set the focus back to the LineEdit from the button } ناتي الان لشرح الدالة خطوة بخطوة
- اقتباس :
- if(edText->text() == "") // If there is no text then exit this function
{ return; } هذا شرط يختبر مربع النص اذا كان فاضي يخرج من الدالة بدون عمل اي شي
- اقتباس :
- lbMyList->addItem(edText->text());
هذا الكود يضيف النص المكتوب في مربع النص الى القائمة mylist - اقتباس :
- edText->setText("");
وظيفة هذا الكود هي مسح مربع النص - اقتباس :
- edText->setFocus();
وضيفة هذا الكود هي ارجاع المؤشر الى مربع النص حتى نبدأ الكتابة من جديد
وهذا ملف mainwindowimpl.cpp بعد التعديلات التي اجريناها عليه - اقتباس :
- #include "mainwindowimpl.h"
// MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f) : QMainWindow(parent, f) { setupUi(this); connect(btAdd, SIGNAL(clicked()), this, SLOT(addClicked())); } void MainWindowImpl::addClicked() { if(edText->text() == "") // If there is no text then exit this function { return; } lbMyList->addItem(edText->text()); // Add the text typed to the list edText->setText(""); // erase the text from the box they typed in edText->setFocus(); // set the focus back to the LineEdit from the button } // والان تستطيع عملBuild و Debug
وتختبر برنامجك الاول مع QDevelop
وهذي الصورة النهائية للبرنامج وهو يعمل بشكل جيد
| |
|