السلام عليكم ورحمة الله وبركاته
طبعا تاتي مع مكتبة Qt مجموعة كبيرة من الـstandard dialogs سوف نقوم بكتابة برنامج يوضح كيفية استدعائها والتعامل معها
نبدأ بإنشاء مشروع جديد من خلال QDevelop

ثم نفتح الملف dialog.ui بواسطة Qt Designer وذلك بالضغط المزدوج على اسم الملف

نحذف كل الـWidgets اللي بداخل الفورم ونضيف الاتي
عدد ١٥ PushButton
عدد ١٥ label
اعد تسمية الـopjectName لجميع الـPushButton بنفس الترتيب من اعلى الى اسفل كالاتي
integerButton
doubleButton
itemButton
textButton
colorButton
fontButton
directoryButton
openFileNameButton
openFileNamesButton
saveFileNameButton
criticalButton
informationButton
questionButton
warningButton
errorButton
اعد تسمية الـtext لجميع الـPushButton بنفس الترتيب من اعلى الى اسفل كالاتي
QInputDialog::get&Integer()
QInputDialog::get&Double()
QInputDialog::getIte&m()
QInputDialog::get&Text()
QColorDialog::get&Color()
QFontDialog::get&Font()
QFileDialog::getE&xistingDirectory()
QFileDialog::get&OpenFileName()
QFileDialog::&getOpenFileNames()
QFileDialog::get&SaveFileName()
QMessageBox::critica&l()
QMessageBox::i&nformation()
QMessageBox::&question()
QMessageBox::&warning()
QErrorMessage::show&M&essage()
والان عد تسمية الـopjectName لجميع الـlabel بنفس الترتيب من اعلى الى اسفل كالاتي
integerLabel
doubleLabel
itemLabel
textLabel
colorLabel
fontLabel
directoryLabel
openFileNameLabel
openFileNamesLabel
saveFileNameLabel
criticalLabel
informationLabel
questionLabel
warningLabel
errorLabel
والان غير خاصية الـframeShape لجميع الـlabel الى QFrame::Box
وامسح الـtext لكل الـlable
الان ازل التحديد عن جميع الـwidgets ثم اضغط على LayOut in Grid
حتى يصبح الفورم بهذا الشكل

الان احفظ جميع التغييرات واغلق Qt Designer وارجع الى QDevelop
نبدأ الان بكنابة الكود
سوف نكتب لكل button دالة خاصة فيه لها وظيفة محددة
الدالة الاولى
في اليداية عرفنا متغير من نوع bool طبعا له قيمتين اما true او false
ثم عرضنا مربع ادخال ويتم اسناد القيمة التي يختارها المستخدم للمتغير i من نوع int
اذا ضغط المستخدم على ok سوف تصبح قيمة المتغير ok = true
في حال كانت قيمة المتغير ok = true سوف يتم تنفيذ الشرط
ويتم عرض قيمة المتغير i في intrherLabel
لكن استخدمنا دالة جديدة وهي arg() هذه الدالة تعرض المتغير i ضمن عبارة من نوع نص بطريقة سهله
فهي تضع قيمة المتغير i مكان 1% فهي تساعدنا كثيرا في حالة اردنا عرض متغيرات رقمية داخل عبارة نصية
المثال التالي يوضح كيفية عمل هذه الدالة
في هذا المثال عندنا متغيرين عددية ومتغير نصي وضيفة arg هي وضع القيمة الاولى اللي هي age مكان %1 ووضع القيمة الثانية
اللي هي weight مكان 2% فهكذا راح تكون قيمة المتغير النصي هي
I am 25 old,My weight is 65.8 kg
طبعا لا تدققوا في الانجليزية المكسره هو انا فالح في العربي حتى افلح في الانجليزي
طيب نجي للدالة الثانية الا وهي ()tr
هذه الدالة نضعها حول النصوص التي يتم عرضها في البرنامج في حال رغبتنا في ترجمة البرنامج
الى لغة اخرى وهي خاصية جيدة مع Qt راح اقرأ عنها اكثر واحاول اشرحها لاحقا.
الدالة الثانية
هذه الدالة ماتفرق عن الدالة الاولى كثير فقط غيرنا المتغير int الى double
الدالة الثالثة
في هذه الدالة قمنا بتعريف متغير من نوع QStringList
واسندنا له قائمة ثم عرضنا للمستخدم مربع ادخال يختار فيه عنصر واحد من القائمة
ووضعنا شرط يتحقق من ان المستخدم ضغط على زر الموافقة
ليتم عرض العنصر المحدد في مربع العنوان
الدالة الرابعة
ما تفرق كثير عن الدالتين الاولى والثانية لكن في هذه الحالة نطلب من المستخدم ادخال قيمة نصية
الدالة الخامسة
في هذه الدالة نعرض للمستخدم مربع لاختيار اللون واذا اختار المستخدم لون يتم اسناد قيمة اللون المختار
الى المتغير QColor
ثم يتم عرض قيمة المتغير في مربع العنوان و يتم تغيير لون مربع العنوان بنفس القيمة المحددة
الدالة السادسة
في هذه الدالة يتم عرض مربع اختيار الخط
وفي حال اختار المستخدم نوع الخط يتم عرض معلومات عن الخط الذي اختارة في مربع العنوان المجاور وتغيير نوع الخط
الى الخط الحالي الذي تم اختيارة
الدالة السابعة
في البداية عرفنا متغير لخيارات مربع الملفات
الخيار الاول QFileDialog::DontResolveSymlinks نجعل مربع الملفات يهمل الاختصارات مثلا في الحالة الافتراضية سوف يتم
اتباع الاختصار اذا ضغط عليه المستخدم
القيمة الثانية التي اسندناها للمتغير options هي QFileDialog::ShowDirsOnly وهي لعرض المجلدات فقط
ثم يتم عرض مربع الملفات ليختار المستخدم مجلد فيتم اسناد المسار الذي يختارة المستخدم للمتغير directory
اذاضغط المستخدم على زر الموافقة يتم عرض المسار في مربع العنوان المجاور
الدالة الثامنة
تشبه الدالة السابقة ولكن هنا يختار المستخدم ملف ليتم عرض مساره
وايضا هنا تم تحديد فلتر للملفات التي يتم عرضها وهو اما ان يختار المستخدم عرض جميع الملفات *
او يختار عرض الملفات من نوع txt.*
الدالة التاسعة
تشبه الدالة السابقة ولكن هنا يستطيع المستخدم تحديد اكثر من ملف
الدالة العاشرة
هنا يتم عرض مربع لحفظ الملف ليستطيع المستخدم حفظ الملف بصيغة نصية او اي صيغة اخرى في المكان الذي يحدده
وسوف يتم عرض مسار الملف الذي حفظه المستخدم في مربع العنوان المجاور
الدالة الحادية عشرة
في البداية عرفنا المتغير reply من نوع StandardButton
ثم عرضنا للمستخدم مربع الرسالة الحرجة critical message
عنوانها هو QMessageBox::critical()
ونص الرسالة داخلها هو قيمة الثابت MESSAGE
هذا الثابت راح نعرفه في بداية الملف لنستطيع استخدامه مع اكثر من داله ويكون بالشكل التالي
عندما يضغط المستخدم على اي زر من الازرار الثلاثة Abort , Retry او Ignore
يتم تغيير قيمة المتغير بحسب اختيار المستخدم
ثم وضعنا شرط يتحقق من الزر الذي ضغطه المستخدم ليتم عرض رساله توضح اختيار المستخدم
الدوال الاربعة الاخيره
ماتفرق كثير عن الدالة الحادية عشرة
الا في نوع مربع الرساله
حيث اننا استخدمنا الانواع
informationMessage()
questionMessage()
warningMessage()
errorMessage()
وهكذا انتهينا من كتابة جميع الدوال
الان يجب علينا ربط حدث الضغط على كل زر بالدالة المناسبة له كالاتي
طبعا كل التعديلات السابقة كانت في الملف dialogimpl.cpp
وهذا هو الملف بعد اجراء التعديلات السابقة
الان نحتاج ان نصرح عن جميع الدوال التي انشناها في الملف dialogimpl.h
وهذا ملف dialogimp.h بعد التعديلات
وهذا هو الشكل النهائي للبرنامج يعمل بجميع وضائفه

ملاحظه: البرنامج موجود ضمن الامثله التي تاتي مع Qt لكن اعدت بنائه من جديد باستخدام Qt Designer حتى نتعلم على الخطوات
بشكل مناسب وبوجود مرجع صحيح
واي ملاحظة او استفسار انا موجود وبالخدمه
طبعا تاتي مع مكتبة Qt مجموعة كبيرة من الـstandard dialogs سوف نقوم بكتابة برنامج يوضح كيفية استدعائها والتعامل معها
نبدأ بإنشاء مشروع جديد من خلال QDevelop

ثم نفتح الملف dialog.ui بواسطة Qt Designer وذلك بالضغط المزدوج على اسم الملف

نحذف كل الـWidgets اللي بداخل الفورم ونضيف الاتي
عدد ١٥ PushButton
عدد ١٥ label
اعد تسمية الـopjectName لجميع الـPushButton بنفس الترتيب من اعلى الى اسفل كالاتي
integerButton
doubleButton
itemButton
textButton
colorButton
fontButton
directoryButton
openFileNameButton
openFileNamesButton
saveFileNameButton
criticalButton
informationButton
questionButton
warningButton
errorButton
اعد تسمية الـtext لجميع الـPushButton بنفس الترتيب من اعلى الى اسفل كالاتي
QInputDialog::get&Integer()
QInputDialog::get&Double()
QInputDialog::getIte&m()
QInputDialog::get&Text()
QColorDialog::get&Color()
QFontDialog::get&Font()
QFileDialog::getE&xistingDirectory()
QFileDialog::get&OpenFileName()
QFileDialog::&getOpenFileNames()
QFileDialog::get&SaveFileName()
QMessageBox::critica&l()
QMessageBox::i&nformation()
QMessageBox::&question()
QMessageBox::&warning()
QErrorMessage::show&M&essage()
والان عد تسمية الـopjectName لجميع الـlabel بنفس الترتيب من اعلى الى اسفل كالاتي
integerLabel
doubleLabel
itemLabel
textLabel
colorLabel
fontLabel
directoryLabel
openFileNameLabel
openFileNamesLabel
saveFileNameLabel
criticalLabel
informationLabel
questionLabel
warningLabel
errorLabel
والان غير خاصية الـframeShape لجميع الـlabel الى QFrame::Box
وامسح الـtext لكل الـlable
الان ازل التحديد عن جميع الـwidgets ثم اضغط على LayOut in Grid
حتى يصبح الفورم بهذا الشكل

الان احفظ جميع التغييرات واغلق Qt Designer وارجع الى QDevelop
نبدأ الان بكنابة الكود
سوف نكتب لكل button دالة خاصة فيه لها وظيفة محددة
الدالة الاولى
كود:
void DialogImpl::setInteger() { bool ok; int i = QInputDialog::getInteger(this, tr("QInputDialog::getInteger()"), tr("Percentage:"), 25, 0, 100, 1, &ok); if (ok) integerLabel->setText(tr("%1%").arg(i)); }
ثم عرضنا مربع ادخال ويتم اسناد القيمة التي يختارها المستخدم للمتغير i من نوع int
اذا ضغط المستخدم على ok سوف تصبح قيمة المتغير ok = true
في حال كانت قيمة المتغير ok = true سوف يتم تنفيذ الشرط
ويتم عرض قيمة المتغير i في intrherLabel
لكن استخدمنا دالة جديدة وهي arg() هذه الدالة تعرض المتغير i ضمن عبارة من نوع نص بطريقة سهله
فهي تضع قيمة المتغير i مكان 1% فهي تساعدنا كثيرا في حالة اردنا عرض متغيرات رقمية داخل عبارة نصية
المثال التالي يوضح كيفية عمل هذه الدالة
كود:
int age; double weight; string s; age = 25; b = 65.8; s = tr("I am %1 old,My weight is %2 kg").arg(age) .arg(weight);
اللي هي weight مكان 2% فهكذا راح تكون قيمة المتغير النصي هي
I am 25 old,My weight is 65.8 kg
طبعا لا تدققوا في الانجليزية المكسره هو انا فالح في العربي حتى افلح في الانجليزي

طيب نجي للدالة الثانية الا وهي ()tr
هذه الدالة نضعها حول النصوص التي يتم عرضها في البرنامج في حال رغبتنا في ترجمة البرنامج
الى لغة اخرى وهي خاصية جيدة مع Qt راح اقرأ عنها اكثر واحاول اشرحها لاحقا.
الدالة الثانية
كود:
void DialogImpl::setDouble() { bool ok; double d = QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"), tr("Amount:"), 37.56, -10000, 10000, 2, &ok); if (ok) doubleLabel->setText(QString("$%1").arg(d)); }
الدالة الثالثة
كود:
void DialogImpl::setItem() { QStringList items; items << tr("Spring") << tr("Summer") << tr("Fall") << tr("Winter"); bool ok; QString item = QInputDialog::getItem(this, tr("QInputDialog::getItem()"), tr("Season:"), items, 0, false, &ok); if (ok && !item.isEmpty()) itemLabel->setText(item); }
واسندنا له قائمة ثم عرضنا للمستخدم مربع ادخال يختار فيه عنصر واحد من القائمة
ووضعنا شرط يتحقق من ان المستخدم ضغط على زر الموافقة
ليتم عرض العنصر المحدد في مربع العنوان
الدالة الرابعة
كود:
void DialogImpl::setText() { bool ok; QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"), tr("User name:"), QLineEdit::Normal, QDir::home().dirName(), &ok); if (ok && !text.isEmpty()) textLabel->setText(text); }
الدالة الخامسة
كود:
void DialogImpl::setColor() { QColor color = QColorDialog::getColor(Qt::green, this); if (color.isValid()) { colorLabel->setText(color.name()); colorLabel->setPalette(QPalette(color)); colorLabel->setAutoFillBackground(true); } }
الى المتغير QColor
ثم يتم عرض قيمة المتغير في مربع العنوان و يتم تغيير لون مربع العنوان بنفس القيمة المحددة
الدالة السادسة
كود:
void DialogImpl::setFont() { bool ok; QFont font = QFontDialog::getFont(&ok, QFont(fontLabel->text()), this); if (ok) { fontLabel->setText(font.key()); fontLabel->setFont(font); } }
وفي حال اختار المستخدم نوع الخط يتم عرض معلومات عن الخط الذي اختارة في مربع العنوان المجاور وتغيير نوع الخط
الى الخط الحالي الذي تم اختيارة
الدالة السابعة
كود:
void DialogImpl::setExistingDirectory() { QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly; QString directory = QFileDialog::getExistingDirectory(this, tr("QFileDialog::getExistingDirectory()"), directoryLabel->text(), options); if (!directory.isEmpty()) directoryLabel->setText(directory); }
الخيار الاول QFileDialog::DontResolveSymlinks نجعل مربع الملفات يهمل الاختصارات مثلا في الحالة الافتراضية سوف يتم
اتباع الاختصار اذا ضغط عليه المستخدم
القيمة الثانية التي اسندناها للمتغير options هي QFileDialog::ShowDirsOnly وهي لعرض المجلدات فقط
ثم يتم عرض مربع الملفات ليختار المستخدم مجلد فيتم اسناد المسار الذي يختارة المستخدم للمتغير directory
اذاضغط المستخدم على زر الموافقة يتم عرض المسار في مربع العنوان المجاور
الدالة الثامنة
كود:
void DialogImpl::setOpenFileName() { QFileDialog::Options options; QString selectedFilter; QString fileName = QFileDialog::getOpenFileName(this, tr("QFileDialog::getOpenFileName()"), openFileNameLabel->text(), tr("All Files (*);;Text Files (*.txt)"), &selectedFilter, options); if (!fileName.isEmpty()) openFileNameLabel->setText(fileName); }
وايضا هنا تم تحديد فلتر للملفات التي يتم عرضها وهو اما ان يختار المستخدم عرض جميع الملفات *
او يختار عرض الملفات من نوع txt.*
الدالة التاسعة
كود:
void DialogImpl::setOpenFileNames() { QFileDialog::Options options; QString selectedFilter; QStringList files = QFileDialog::getOpenFileNames( this, tr("QFileDialog::getOpenFileNames()"), openFilesPath, tr("All Files (*);;Text Files (*.txt)"), &selectedFilter, options); if (files.count()) { openFilesPath = files[0]; openFileNamesLabel->setText(QString("[%1]").arg(files.join(", "))); } }
الدالة العاشرة
كود:
void DialogImpl::setSaveFileName() { QFileDialog::Options options; QString selectedFilter; QString fileName = QFileDialog::getSaveFileName(this, tr("QFileDialog::getSaveFileName()"), saveFileNameLabel->text(), tr("All Files (*);;Text Files (*.txt)"), &selectedFilter, options); if (!fileName.isEmpty()) saveFileNameLabel->setText(fileName); }
وسوف يتم عرض مسار الملف الذي حفظه المستخدم في مربع العنوان المجاور
الدالة الحادية عشرة
كود:
void DialogImpl::criticalMessage() { QMessageBox::StandardButton reply; reply = QMessageBox::critical(this, tr("QMessageBox::critical()"), MESSAGE, QMessageBox::Abort | QMessageBox::Retry | QMessageBox::Ignore); if (reply == QMessageBox::Abort) criticalLabel->setText(tr("Abort")); else if (reply == QMessageBox::Retry) criticalLabel->setText(tr("Retry")); else criticalLabel->setText(tr("Ignore")); }
ثم عرضنا للمستخدم مربع الرسالة الحرجة critical message
عنوانها هو QMessageBox::critical()
ونص الرسالة داخلها هو قيمة الثابت MESSAGE
هذا الثابت راح نعرفه في بداية الملف لنستطيع استخدامه مع اكثر من داله ويكون بالشكل التالي
كود:
#define MESSAGE \ DialogImpl::tr("<p>Message boxes have a caption, a text, " \ "and any number of buttons, each with standard or custom texts." \ "<p>Click a button to close the message box. Pressing the Esc button " \ "will activate the detected escape button (if any).")
يتم تغيير قيمة المتغير بحسب اختيار المستخدم
ثم وضعنا شرط يتحقق من الزر الذي ضغطه المستخدم ليتم عرض رساله توضح اختيار المستخدم
الدوال الاربعة الاخيره
كود:
void DialogImpl::informationMessage() { QMessageBox::StandardButton reply; reply = QMessageBox::information(this, tr("QMessageBox::information()"), MESSAGE); if (reply == QMessageBox::Ok) informationLabel->setText(tr("OK")); else informationLabel->setText(tr("Escape")); } void DialogImpl::questionMessage() { QMessageBox::StandardButton reply; reply = QMessageBox::question(this, tr("QMessageBox::question()"), MESSAGE, QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); if (reply == QMessageBox::Yes) questionLabel->setText(tr("Yes")); else if (reply == QMessageBox::No) questionLabel->setText(tr("No")); else questionLabel->setText(tr("Cancel")); } void DialogImpl::warningMessage() { QMessageBox msgBox(QMessageBox::Warning, tr("QMessageBox::warning()"), MESSAGE, 0, this); msgBox.addButton(tr("Save &Again"), QMessageBox::AcceptRole); msgBox.addButton(tr("&Continue"), QMessageBox::RejectRole); if (msgBox.exec() == QMessageBox::AcceptRole) warningLabel->setText(tr("Save Again")); else warningLabel->setText(tr("Continue")); } void DialogImpl::errorMessage() { errorMessageDialog->showMessage( tr("This dialog shows and remembers error messages. " "If the checkbox is checked (as it is by default), " "the shown message will be shown again, " "but if the user unchecks the box the message " "will not appear again if QErrorMessage::showMessage() " "is called with the same message.")); errorLabel->setText(tr("If the box is unchecked, the message " "won't appear again.")); }
الا في نوع مربع الرساله
حيث اننا استخدمنا الانواع
informationMessage()
questionMessage()
warningMessage()
errorMessage()
وهكذا انتهينا من كتابة جميع الدوال
الان يجب علينا ربط حدث الضغط على كل زر بالدالة المناسبة له كالاتي
كود:
connect(integerButton, SIGNAL(clicked()), this, SLOT(setInteger())); connect(doubleButton, SIGNAL(clicked()), this, SLOT(setDouble())); connect(itemButton, SIGNAL(clicked()), this, SLOT(setItem())); connect(textButton, SIGNAL(clicked()), this, SLOT(setText())); connect(colorButton, SIGNAL(clicked()), this, SLOT(setColor())); connect(fontButton, SIGNAL(clicked()), this, SLOT(setFont())); connect(directoryButton, SIGNAL(clicked()),this, SLOT(setExistingDirectory())); connect(openFileNameButton, SIGNAL(clicked()),this, SLOT(setOpenFileName())); connect(openFileNamesButton, SIGNAL(clicked()),this, SLOT(setOpenFileNames())); connect(saveFileNameButton, SIGNAL(clicked()),this, SLOT(setSaveFileName())); connect(criticalButton, SIGNAL(clicked()), this, SLOT(criticalMessage())); connect(informationButton, SIGNAL(clicked()),this, SLOT(informationMessage())); connect(questionButton, SIGNAL(clicked()), this, SLOT(questionMessage())); connect(warningButton, SIGNAL(clicked()), this, SLOT(warningMessage())); connect(errorButton, SIGNAL(clicked()), this, SLOT(errorMessage()));
وهذا هو الملف بعد اجراء التعديلات السابقة
كود:
#include "dialogimpl.h" #include <QtGui> #define MESSAGE \ DialogImpl::tr("<p>Message boxes have a caption, a text, " \ "and any number of buttons, each with standard or custom texts." \ "<p>Click a button to close the message box. Pressing the Esc button " \ "will activate the detected escape button (if any).") // DialogImpl::DialogImpl( QWidget * parent, Qt::WFlags f) : QDialog(parent, f) { setupUi(this); errorMessageDialog = new QErrorMessage(this); connect(integerButton, SIGNAL(clicked()), this, SLOT(setInteger())); connect(doubleButton, SIGNAL(clicked()), this, SLOT(setDouble())); connect(itemButton, SIGNAL(clicked()), this, SLOT(setItem())); connect(textButton, SIGNAL(clicked()), this, SLOT(setText())); connect(colorButton, SIGNAL(clicked()), this, SLOT(setColor())); connect(fontButton, SIGNAL(clicked()), this, SLOT(setFont())); connect(directoryButton, SIGNAL(clicked()),this, SLOT(setExistingDirectory())); connect(openFileNameButton, SIGNAL(clicked()),this, SLOT(setOpenFileName())); connect(openFileNamesButton, SIGNAL(clicked()),this, SLOT(setOpenFileNames())); connect(saveFileNameButton, SIGNAL(clicked()),this, SLOT(setSaveFileName())); connect(criticalButton, SIGNAL(clicked()), this, SLOT(criticalMessage())); connect(informationButton, SIGNAL(clicked()),this, SLOT(informationMessage())); connect(questionButton, SIGNAL(clicked()), this, SLOT(questionMessage())); connect(warningButton, SIGNAL(clicked()), this, SLOT(warningMessage())); connect(errorButton, SIGNAL(clicked()), this, SLOT(errorMessage())); } // void DialogImpl::setInteger() { bool ok; int i = QInputDialog::getInteger(this, tr("QInputDialog::getInteger()"), tr("Percentage:"), 25, 0, 100, 1, &ok); if (ok) integerLabel->setText(tr("%1%").arg(i)); } void DialogImpl::setDouble() { bool ok; double d = QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"), tr("Amount:"), 37.56, -10000, 10000, 2, &ok); if (ok) doubleLabel->setText(QString("$%1").arg(d)); } void DialogImpl::setItem() { QStringList items; items << tr("Spring") << tr("Summer") << tr("Fall") << tr("Winter"); bool ok; QString item = QInputDialog::getItem(this, tr("QInputDialog::getItem()"), tr("Season:"), items, 0, false, &ok); if (ok && !item.isEmpty()) itemLabel->setText(item); } void DialogImpl::setText() { bool ok; QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"), tr("User name:"), QLineEdit::Normal, QDir::home().dirName(), &ok); if (ok && !text.isEmpty()) textLabel->setText(text); } void DialogImpl::setColor() { QColor color = QColorDialog::getColor(Qt::green, this); if (color.isValid()) { colorLabel->setText(color.name()); colorLabel->setPalette(QPalette(color)); colorLabel->setAutoFillBackground(true); } } void DialogImpl::setFont() { bool ok; QFont font = QFontDialog::getFont(&ok, QFont(fontLabel->text()), this); if (ok) { fontLabel->setText(font.key()); fontLabel->setFont(font); } } void DialogImpl::setExistingDirectory() { QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly; QString directory = QFileDialog::getExistingDirectory(this, tr("QFileDialog::getExistingDirectory()"), directoryLabel->text(), options); if (!directory.isEmpty()) directoryLabel->setText(directory); } void DialogImpl::setOpenFileName() { QFileDialog::Options options; QString selectedFilter; QString fileName = QFileDialog::getOpenFileName(this, tr("QFileDialog::getOpenFileName()"), openFileNameLabel->text(), tr("All Files (*);;Text Files (*.txt)"), &selectedFilter, options); if (!fileName.isEmpty()) openFileNameLabel->setText(fileName); } void DialogImpl::setOpenFileNames() { QFileDialog::Options options; QString selectedFilter; QStringList files = QFileDialog::getOpenFileNames( this, tr("QFileDialog::getOpenFileNames()"), openFilesPath, tr("All Files (*);;Text Files (*.txt)"), &selectedFilter, options); if (files.count()) { openFilesPath = files[0]; openFileNamesLabel->setText(QString("[%1]").arg(files.join(", "))); } } void DialogImpl::setSaveFileName() { QFileDialog::Options options; QString selectedFilter; QString fileName = QFileDialog::getSaveFileName(this, tr("QFileDialog::getSaveFileName()"), saveFileNameLabel->text(), tr("All Files (*);;Text Files (*.txt)"), &selectedFilter, options); if (!fileName.isEmpty()) saveFileNameLabel->setText(fileName); } void DialogImpl::criticalMessage() { QMessageBox::StandardButton reply; reply = QMessageBox::critical(this, tr("QMessageBox::critical()"), MESSAGE, QMessageBox::Abort | QMessageBox::Retry | QMessageBox::Ignore); if (reply == QMessageBox::Abort) criticalLabel->setText(tr("Abort")); else if (reply == QMessageBox::Retry) criticalLabel->setText(tr("Retry")); else criticalLabel->setText(tr("Ignore")); } void DialogImpl::informationMessage() { QMessageBox::StandardButton reply; reply = QMessageBox::information(this, tr("QMessageBox::information()"), MESSAGE); if (reply == QMessageBox::Ok) informationLabel->setText(tr("OK")); else informationLabel->setText(tr("Escape")); } void DialogImpl::questionMessage() { QMessageBox::StandardButton reply; reply = QMessageBox::question(this, tr("QMessageBox::question()"), MESSAGE, QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); if (reply == QMessageBox::Yes) questionLabel->setText(tr("Yes")); else if (reply == QMessageBox::No) questionLabel->setText(tr("No")); else questionLabel->setText(tr("Cancel")); } void DialogImpl::warningMessage() { QMessageBox msgBox(QMessageBox::Warning, tr("QMessageBox::warning()"), MESSAGE, 0, this); msgBox.addButton(tr("Save &Again"), QMessageBox::AcceptRole); msgBox.addButton(tr("&Continue"), QMessageBox::RejectRole); if (msgBox.exec() == QMessageBox::AcceptRole) warningLabel->setText(tr("Save Again")); else warningLabel->setText(tr("Continue")); } void DialogImpl::errorMessage() { DialogImpl::errorMessageDialog->showMessage( tr("This dialog shows and remembers error messages. " "If the checkbox is checked (as it is by default), " "the shown message will be shown again, " "but if the user unchecks the box the message " "will not appear again if QErrorMessage::showMessage() " "is called with the same message.")); errorLabel->setText(tr("If the box is unchecked, the message " "won't appear again.")); }
كود:
private slots: void setInteger(); void setDouble(); void setItem(); void setText(); void setColor(); void setFont(); void setExistingDirectory(); void setOpenFileName(); void setOpenFileNames(); void setSaveFileName(); void criticalMessage(); void informationMessage(); void questionMessage(); void warningMessage(); void errorMessage();
كود:
#ifndef DIALOGIMPL_H #define DIALOGIMPL_H // #include "ui_dialog.h" #include <QDialog> class QErrorMessage; // class DialogImpl : public QDialog, public Ui::Dialog { Q_OBJECT public: DialogImpl( QWidget * parent = 0, Qt::WFlags f = 0 ); private slots: void setInteger(); void setDouble(); void setItem(); void setText(); void setColor(); void setFont(); void setExistingDirectory(); void setOpenFileName(); void setOpenFileNames(); void setSaveFileName(); void criticalMessage(); void informationMessage(); void questionMessage(); void warningMessage(); void errorMessage(); private: QErrorMessage *errorMessageDialog; QString openFilesPath; }; #endif

ملاحظه: البرنامج موجود ضمن الامثله التي تاتي مع Qt لكن اعدت بنائه من جديد باستخدام Qt Designer حتى نتعلم على الخطوات
بشكل مناسب وبوجود مرجع صحيح
واي ملاحظة او استفسار انا موجود وبالخدمه
تعليق