|
|
|
[4] القوائم وشريط الأدوات في Qt4 |
|
في هذا الدرس سوف نتحدث عن القوائم menus وشريط الأدوات toolbar في برامج وتطبيقات الـ Qt
القائمه هي واحدة من أكثر الأجزاء التي نراها في التطبيقات ، فهي تلك المجموعة من الأوامر الواقعة في قوائم مختلفة بالجزء الأعلى من الكثير من البرامج. بينما في تطبيقات لوحة التحكم "console" عليك أن تتذكر كل تلك الأوامر ، هنا لدينا معظم هذه الأوامر مرتبة في أجزاء منطقية. القوائم ترتب الأوامر- التي تستخدمها في التطبيق- في مجموعات. وشريط الأدوات يوفر وصول سريع لأكثر الأوامر استخداما.
قائمة بسيطة
مثالنا الأول يعرض قائمة بسيطة:
#ifndef SIMPLEMENU_H #define SIMPLEMENU_H #include <QMainWindow> { public: }; #endif
|
هذا هو الهيدر لمثالنا
#include "simplemenu.h" #include <QMenu> #include <QMenuBar> #include <QApplication> SimpleMenu ::SimpleMenu(QWidget *parent ){ file = menuBar()->addMenu("&File"); file->addAction(quit); connect(quit, SIGNAL(triggered()), qApp, SLOT(quit())); }
|
هنا لدينا شريط قوائم menubar ، وعمل أو أكشن "action" . وحتى نتعامل مع القوائم يجب أن نرث من الكائن QMainWindow .
QAction *quit = new QAction("&Quit", this);
|
هذه التعليمة تنشئ QAction وهو كائن يمثل العمل الذي سينجز . كل كائن QMenu لديه كائن QAction أو أكثر.
QMenu *file;
file = menuBar()->addMenu("&File");
|
هنا أنشأنا كائن من الـ QMenu واضفنا قائمة الـ File
وأضفنا عمل (أكشن) داخل القائمة
connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
|
وربطنا حدث الخروج معه، اذا اخترناه من القائمة نخرج من البرنامج
#include "simplemenu.h" #include <QDesktopWidget> #include <QApplication> { int x, y; int screenWidth; int screenHeight; int width, height; width = widget.frameGeometry().width(); height = widget.frameGeometry().height(); screenWidth = desktop->width(); screenHeight = desktop->height(); x = (screenWidth - width) / 2; y = (screenHeight - height) / 2; widget.move( x, y ); } int main(int argc, char *argv[]) { SimpleMenu window; window.setWindowTitle("Simple menu"); window.show(); center(window); return app.exec(); }
|
ملف الـ main

والنتيجة
أيقونات icons ، اختصارات shotrcut ، فواصل separators
في المثال التالي سوف نعزز تطبيقنا السابق باضافة الأيقونات إلى القوائم وسنستخدم الاختصارات والفواصل.
#ifndef ANOTHERMENU_H #define ANOTHERMENU_H #include <QMainWindow> { public: }; #endif
|
هذا ملف الهيدر للمثال
#include "anothermenu.h" #include <QMenu> #include <QMenuBar> #include <QApplication> AnotherMenu ::AnotherMenu(QWidget *parent ){ quit->setShortcut(tr("CTRL+Q")); file = menuBar()->addMenu("&File"); file->addAction(newa); file->addAction(open); file->addSeparator(); file->addAction(quit); connect(quit, SIGNAL(triggered()), qApp, SLOT(quit())); }
|
في مثالنا لدينا قائمة واحدة وثلا أعمال. العمل quit الوحيد الذي سيقوم فعليا بعمل اذا اخترناه. أنشأنا أيضا فاصل ، واختصار [CTRL+Q] سوف ينهي البرنامج .
QPixmap newpix("new.png");
QPixmap openpix("open.png");
QPixmap quitpix("quit.png"); |
هذه الصور التي سنستخدمها في القوائم كأيقونات
QAction *newa = new QAction(newpix, "&New", this);
QAction *open = new QAction(openpix, "&Open", this);
QAction *quit = new QAction(quitpix, "&Quit", this); |
وهذه التعليمة أضفنا في باني العمل (constructor) صورة كوسيط أول
| quit->setShortcut(tr("CTRL+Q")); |
هنا ننشئ اختصار للوحة المفاتيح. عند ضغطنا لهذا الاختصار سوف نطلق عمل الخروج والذي سيتولى بدوره انهاء البرنامج.
وننشئ فاصل. الفاصل هو خط أفقي يسمح لنا بترتيب الأوامر في القائمة الواحدة بترتيب منطقي.
#include "anothermenu.h" #include <QDesktopWidget> #include <QApplication> { int x, y; int screenWidth; int screenHeight; int width, height; width = widget.frameGeometry().width(); height = widget.frameGeometry().height(); screenWidth = desktop->width(); screenHeight = desktop->height(); x = (screenWidth - width) / 2; y = (screenHeight - height) / 2; widget.move( x, y ); } int main(int argc, char *argv[]) { AnotherMenu window; window.setWindowTitle("Another menu"); window.show(); center(window); return app.exec(); }
|
ملف الـ main

النتيجة
قائمة الاختيارات
في المثال التالي سوف ننشئ قائمة اختيار و التي هي عبارة عن قائمة من الأعمال مع كل عمل مربع اختيار.
هذا الخيار سوف يتحكم بظهور شريط الحالة في مثالنا.
#ifndef CHECKABLE_H #define CHECKABLE_H #include <QMainWindow> { public: CheckableMenu (QWidget *parent = 0); }; #endif
|
ملف الهيدر
#include "checkable.h" #include <QMenu> #include <QMenuBar> #include <QStatusBar> #include <QApplication> Checkable ::Checkable(QWidget *parent ){ viewst = new QAction("&View statusbar", this); viewst->setCheckable(true); viewst->setChecked(true); file = menuBar()->addMenu("&File"); file->addAction(viewst); statusBar(); connect(viewst, SIGNAL(triggered()), this, SLOT(toggleStatusbar())); } void Checkable::toggleStatusbar() { if (viewst->isChecked()) statusBar()->show(); else statusBar()->hide(); }
|
viewst = new QAction("&View statusbar", this);
viewst->setCheckable(true);
viewst->setChecked(true);
|
أنشأنا عمل وجعلناه قابل للاختيار باستخدام setCheckable() . وجعلنا هذا الخيار هو المُختار
if (viewst->isChecked())
statusBar()->show();
else
statusBar()->hide();
|
وداخل الـ toggleStatusbar نحدد ما اذا كان هذا العمل قد اختير ونخفي أو نعرض شريط الأدوات تبعا لذلك
#include "checkable.h" #include <QDesktopWidget> #include <QApplication> { int x, y; int screenWidth; int screenHeight; int WIDTH = 250; int HEIGHT = 170; screenWidth = desktop->width(); screenHeight = desktop->height(); x = (screenWidth - WIDTH) / 2; y = (screenHeight - HEIGHT) / 2; widget.setGeometry(x, y, WIDTH, HEIGHT); } int main(int argc, char *argv[]) { Checkable window; window.setWindowTitle("Checkable menu"); window.show(); center(window); return app.exec(); }
|
ملف الـ main

النتيجة
QToolBar
فئة الـ QToolBar توفر لوحة قابلة للحركة تحتوي على مجموعة من متحكمات توفر وصول سريع لأعمال التطبيقات كتلك الموجودة في القوائم.
#ifndef TOOLBAR_H #define TOOLBAR_H #include <QMainWindow> { Q_OBJECT public: }; #endif
|
وهذا ملف الهيدر
#include "toolbar.h" #include <QToolBar> #include <QApplication> #include <QIcon> #include <QAction> { QToolBar *toolbar = addToolBar ("main toolbar"); toolbar ->addAction(QIcon(newpix ), "New File"); toolbar ->addAction(QIcon(openpix ), "Open File"); toolbar->addSeparator(); "Quit Application"); connect(quit, SIGNAL(triggered()), qApp, SLOT(quit())); }
|
ننشئ شريط أدوات ولا ننسى أن نرث من كائن الـ QMainWindow حتى نتمكن من انشاء شريط الأدوات.
QToolBar *toolbar = addToolBar("main toolbar");
|
addToolBar() تنشئ شريط أدوات وترجع مؤشر اليه
toolbar->addAction(QIcon(newpix), "New File");
toolbar->addAction(QIcon(openpix), "Open File");
toolbar->addSeparator();
|
هنا أضفنا عملين وفاصل لشريط الأدوات
#include "toolbar.h" #include <QDesktopWidget> #include <QApplication> { int x, y; int screenWidth; int screenHeight; int WIDTH = 250; int HEIGHT = 170; screenWidth = desktop->width(); screenHeight = desktop->height(); x = (screenWidth - WIDTH) / 2; y = (screenHeight - HEIGHT) / 2; widget.setGeometry(x, y, WIDTH, HEIGHT); } int main(int argc, char *argv[]) { Toolbar window; window.setWindowTitle("QToolBar"); window.show(); center(window); return app.exec(); }
|
ملف الـ main

والنتيجة
هيكل تطبيق
وفي نهاية هذا الدرس سوف ننشئ هيكل أو مخطط تطبيق سيكون حاويا لكل ما تعلمناه بهذا الدرس.
هذا المثال سيكون مبني أساسا على الـ QMainWindow
#ifndef SKELETON_H #define SKELETON_H #include <QMainWindow> { Q_OBJECT public: }; #endif
|
ملف الهيدر للمثال
#include "skeleton.h" #include <QToolBar> #include <QApplication> #include <QIcon> #include <QAction> #include <QMenu> #include <QMenuBar> #include <QStatusBar> #include <QTextEdit> Skeleton ::Skeleton(QWidget *parent ){ file = menuBar()->addMenu("&File"); file->addAction(quit); connect(quit, SIGNAL(triggered()), qApp, SLOT(quit())); QToolBar *toolbar = addToolBar ("main toolbar"); toolbar ->addAction(QIcon(newpix ), "New File"); toolbar ->addAction(QIcon(openpix ), "Open File"); toolbar->addSeparator(); "Quit Application"); setCentralWidget(edit); statusBar()->showMessage("Ready"); connect(quit, SIGNAL(triggered()), qApp, SLOT(quit())); connect(quit2, SIGNAL(triggered()), qApp, SLOT(quit())); }
|
هنا أنشأنا قائمة ، شريط أدوات ، وشريط حالة
QTextEdit *edit = new QTextEdit(this);
setCentralWidget(edit); |
ننشئ QTextEdit ونضعها في الجزء الوسطي من الـ QMainWindow
#include "skeleton.h" #include <QDesktopWidget> #include <QApplication> { int x, y; int screenWidth; int screenHeight; int WIDTH = 350; int HEIGHT = 250; screenWidth = desktop->width(); screenHeight = desktop->height(); x = (screenWidth - WIDTH) / 2; y = (screenHeight - HEIGHT) / 2; widget.setGeometry(x, y, WIDTH, HEIGHT); } int main(int argc, char *argv[]) { Skeleton window; window.setWindowTitle("Application skeleton"); window.show(); center(window); return app.exec(); }
|
ملف الـ main

والنتيجه
والحمدلله رب العالمين
حمدا كما ينبغي لجلال وجهه وعظيم سلطانه
أتمنى أن أكون وُفقت في هذا الدرس الصغير
والله ولي التوفيق
| إسم الكاتب |
تاريخ الإضافة |
التقييم / المقيمين |
زيارات الدرس |
| Shanx |
21/12/2008 |
27 / 3 |
1285 |
|
|
|
|