Screenshot

فى ال Examples الموجودة مع QT هتلقة برنامج Screenshot بيستخدم فى اخد screenshot الأول قبل مانفكر فى الكود نفكر فى الLogic اللى هيتصمم بيه البرنامج
1- زى مانت شايف Qwidget عليها ال Components
2- Image او تقدر تقول Scaled Image
3- GroupBox وبيشمل
a)QLabel ->Screenshot Delay
b)QSpinBox , Suffix = s
c)QCheckBox -> Hide This Window
4- 3 Buttons
دا ال Components اللى هنستخدمها والObjects names كالتالى
;
وال Layouts اعتقد إنها اسهل حاجة
وفى Slots خاصة بالبرنامج يبقة لاحظ إنك هتستخدم Q_OBJECT Macro !
ال Slots خاصة ب اخد Screenshot وعمل Save والتعديل على ال CheckBox بحيث إن تظهر ال Form فى ال Screenshot او لأ
فى كام method هنستخدمهم فى إننا نعمل ال GUI بتاع البرنامج وتظبيط ال Layout وعمل update لل Screenshot label
ال Class هيكون شكله كالتالى
نكتب ال Implementation الخاص بال Class
اولا ال Constructor
قمنا بإعداد ال screenshot label وإستدعينا الميثودز المسئولة عن ال GUI وهى createOptionsGroupBox و createButtonsLayout
نظبط ال Layout هنبدأ ب Vertically
فهنضيف ال screenshot label وبعده ال optionsGroupBox ونضيف ال layout الخاص بال buttons بإستخدام addLayout method!
ونخلى ال mainLayout هو ال Layout الخاص بال Form بتاعتنا
هنستدعى ال shootScreen فى ال Constructor عادى جدا
ونخلى القيمة ال default لل delaySpinBox = 5
setWindowTitle بتخليك تحدد الTitle الخاص بال Window
وتحجم ال Window بإستخدام resize
نضيف هندلر لل resizeEvent فى حال لو المستخدم حب يعدل ا لsize الخاص بال
فتتظبط الصورة على المساحة الفاضية
لما المستخدم يحاول يعمل new Screenshot
لاحظ إن ال New Screenshot button مش هيهندل غير Screenshot واحدة لحد ماتنتهى مدة ال delay فهنخليه disabled
لاحظ إننا ربطنا فترة ال delay مضروبة فى 1000 طبعا لأنها بالملى سكند بال shootScreen Slot بإستخدام ال singleShot method من ال Qtimer Class
فى حالة لو حب يعمل save
1- بنحدد الإمتداد اللى هنحفظ عليه وهو png
2- بنحدد ال Initial Path بQDir::currentPath
3- بنظهر ال save dialog ! وفى حال لو ليه قيمة هنحفظ الصورة الأصـلية بإستخدام save method من ال Qpixmap Object
ال shootscreenshot method
qApp->beep method المفروض بتعمل beep لكن مش شغاله عندى :S
اخذ ال Screenshot وتخزينها فى ال originalPixmap بإستخدام ال grabWindow static method بتدينا ال Contents الخاصة بال Window اللى هتتباصى ليها ك Argument
وبعد ماتتاخد ال Screenshot هنرجع ال newScreenshotButton Enabled تانى بننا نباصى false ل setDisabled method
الباقى هو مجرد ال Implementation لل createGroupBox و createButtonsLayout !
الصورة النهائية ليه هتكون بالشكل دا
طبعا كل الحقوق محفوظة ل trolltech انا حبيت اعلق على السورس مش اكتر.

فى ال Examples الموجودة مع QT هتلقة برنامج Screenshot بيستخدم فى اخد screenshot الأول قبل مانفكر فى الكود نفكر فى الLogic اللى هيتصمم بيه البرنامج
1- زى مانت شايف Qwidget عليها ال Components
2- Image او تقدر تقول Scaled Image
3- GroupBox وبيشمل
a)QLabel ->Screenshot Delay
b)QSpinBox , Suffix = s
c)QCheckBox -> Hide This Window
4- 3 Buttons
دا ال Components اللى هنستخدمها والObjects names كالتالى
كود:
QPixmap originalPixmap; QLabel *screenshotLabel; QGroupBox *optionsGroupBox; QSpinBox *delaySpinBox; QLabel *delaySpinBoxLabel; QCheckBox *hideThisWindowCheckBox; QPushButton *newScreenshotButton; QPushButton *saveScreenshotButton; QPushButton *quitScreenshotButton
وال Layouts اعتقد إنها اسهل حاجة
كود:
QVBoxLayout *mainLayout; QGridLayout *optionsGroupBoxLayout; QHBoxLayout *buttonsLayout
كود:
private slots: void newScreenshot(); void saveScreenshot(); void shootScreen(); void updateCheckBox();
فى كام method هنستخدمهم فى إننا نعمل ال GUI بتاع البرنامج وتظبيط ال Layout وعمل update لل Screenshot label
كود:
void createOptionsGroupBox(); void createButtonsLayout(); QPushButton *createButton(const QString &text, QWidget *receiver, const char *member); void updateScreenshotLabel()
كود:
class Screenshot : public QWidget { Q_OBJECT public: Screenshot(); protected: void resizeEvent(QResizeEvent *event); private slots: void newScreenshot(); void saveScreenshot(); void shootScreen(); void updateCheckBox(); private: void createOptionsGroupBox(); void createButtonsLayout(); QPushButton *createButton(const QString &text, QWidget *receiver, const char *member); void updateScreenshotLabel(); QPixmap originalPixmap; QLabel *screenshotLabel; QGroupBox *optionsGroupBox; QSpinBox *delaySpinBox; QLabel *delaySpinBoxLabel; QCheckBox *hideThisWindowCheckBox; QPushButton *newScreenshotButton; QPushButton *saveScreenshotButton; QPushButton *quitScreenshotButton; QVBoxLayout *mainLayout; QGridLayout *optionsGroupBoxLayout; QHBoxLayout *buttonsLayout; };
اولا ال Constructor
كود:
Screenshot::Screenshot() { screenshotLabel = new QLabel; screenshotLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); screenshotLabel->setAlignment(Qt::AlignCenter); screenshotLabel->setMinimumSize(240, 160); createOptionsGroupBox(); createButtonsLayout(); mainLayout = new QVBoxLayout; mainLayout->addWidget(screenshotLabel); mainLayout->addWidget(optionsGroupBox); mainLayout->addLayout(buttonsLayout); setLayout(mainLayout); shootScreen(); delaySpinBox->setValue(5); setWindowTitle(tr("Screenshot")); resize(300, 200); }
نظبط ال Layout هنبدأ ب Vertically
فهنضيف ال screenshot label وبعده ال optionsGroupBox ونضيف ال layout الخاص بال buttons بإستخدام addLayout method!
ونخلى ال mainLayout هو ال Layout الخاص بال Form بتاعتنا
هنستدعى ال shootScreen فى ال Constructor عادى جدا
ونخلى القيمة ال default لل delaySpinBox = 5
setWindowTitle بتخليك تحدد الTitle الخاص بال Window
وتحجم ال Window بإستخدام resize
نضيف هندلر لل resizeEvent فى حال لو المستخدم حب يعدل ا لsize الخاص بال
كود:
Window void Screenshot::resizeEvent(QResizeEvent * /* event */) { QSize scaledSize = originalPixmap.size(); scaledSize.scale(screenshotLabel->size(), Qt::KeepAspectRatio); if (!screenshotLabel->pixmap() || scaledSize != screenshotLabel->pixmap()->size()) updateScreenshotLabel(); }
لما المستخدم يحاول يعمل new Screenshot
كود:
void Screenshot::newScreenshot() { if (hideThisWindowCheckBox->isChecked()) hide(); newScreenshotButton->setDisabled(true); QTimer::singleShot(delaySpinBox->value() * 1000, this, SLOT(shootScreen())); }
لاحظ إننا ربطنا فترة ال delay مضروبة فى 1000 طبعا لأنها بالملى سكند بال shootScreen Slot بإستخدام ال singleShot method من ال Qtimer Class
فى حالة لو حب يعمل save
كود:
void Screenshot::saveScreenshot() { QString format = "png"; QString initialPath = QDir::currentPath() + tr("/untitled.") + format; QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), initialPath, tr("%1 Files (*.%2);;All Files (*)") .arg(format.toUpper()) .arg(format)); if (!fileName.isEmpty()) originalPixmap.save(fileName, format.toAscii()); }
2- بنحدد ال Initial Path بQDir::currentPath
3- بنظهر ال save dialog ! وفى حال لو ليه قيمة هنحفظ الصورة الأصـلية بإستخدام save method من ال Qpixmap Object
ال shootscreenshot method
كود:
void Screenshot::shootScreen() { if (delaySpinBox->value() != 0) qApp->beep(); originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId()); updateScreenshotLabel(); newScreenshotButton->setDisabled(false); if (hideThisWindowCheckBox->isChecked()) show(); }
اخذ ال Screenshot وتخزينها فى ال originalPixmap بإستخدام ال grabWindow static method بتدينا ال Contents الخاصة بال Window اللى هتتباصى ليها ك Argument
وبعد ماتتاخد ال Screenshot هنرجع ال newScreenshotButton Enabled تانى بننا نباصى false ل setDisabled method
الباقى هو مجرد ال Implementation لل createGroupBox و createButtonsLayout !
الصورة النهائية ليه هتكون بالشكل دا
كود:
#include <QtGui> #include "screenshot.h" Screenshot::Screenshot() { screenshotLabel = new QLabel; screenshotLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); screenshotLabel->setAlignment(Qt::AlignCenter); screenshotLabel->setMinimumSize(240, 160); createOptionsGroupBox(); createButtonsLayout(); mainLayout = new QVBoxLayout; mainLayout->addWidget(screenshotLabel); mainLayout->addWidget(optionsGroupBox); mainLayout->addLayout(buttonsLayout); setLayout(mainLayout); shootScreen(); delaySpinBox->setValue(5); setWindowTitle(tr("Screenshot")); resize(300, 200); } void Screenshot::resizeEvent(QResizeEvent * /* event */) { QSize scaledSize = originalPixmap.size(); scaledSize.scale(screenshotLabel->size(), Qt::KeepAspectRatio); if (!screenshotLabel->pixmap() || scaledSize != screenshotLabel->pixmap()->size()) updateScreenshotLabel(); } void Screenshot::newScreenshot() { if (hideThisWindowCheckBox->isChecked()) hide(); newScreenshotButton->setDisabled(true); QTimer::singleShot(delaySpinBox->value() * 1000, this, SLOT(shootScreen())); } void Screenshot::saveScreenshot() { QString format = "png"; QString initialPath = QDir::currentPath() + tr("/untitled.") + format; QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), initialPath, tr("%1 Files (*.%2);;All Files (*)") .arg(format.toUpper()) .arg(format)); if (!fileName.isEmpty()) originalPixmap.save(fileName, format.toAscii()); } void Screenshot::shootScreen() { if (delaySpinBox->value() != 0) qApp->beep(); originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId()); updateScreenshotLabel(); newScreenshotButton->setDisabled(false); if (hideThisWindowCheckBox->isChecked()) show(); } void Screenshot::updateCheckBox() { if (delaySpinBox->value() == 0) hideThisWindowCheckBox->setDisabled(true); else hideThisWindowCheckBox->setDisabled(false); } void Screenshot::createOptionsGroupBox() { optionsGroupBox = new QGroupBox(tr("Options")); delaySpinBox = new QSpinBox; delaySpinBox->setSuffix(tr(" s")); delaySpinBox->setMaximum(60); connect(delaySpinBox, SIGNAL(valueChanged(int)), this, SLOT(updateCheckBox())); delaySpinBoxLabel = new QLabel(tr("Screenshot Delay:")); hideThisWindowCheckBox = new QCheckBox(tr("Hide This Window")); optionsGroupBoxLayout = new QGridLayout; optionsGroupBoxLayout->addWidget(delaySpinBoxLabel, 0, 0); optionsGroupBoxLayout->addWidget(delaySpinBox, 0, 1); optionsGroupBoxLayout->addWidget(hideThisWindowCheckBox, 1, 0, 1, 2); optionsGroupBox->setLayout(optionsGroupBoxLayout); } void Screenshot::createButtonsLayout() { newScreenshotButton = createButton(tr("New Screenshot"), this, SLOT(newScreenshot())); saveScreenshotButton = createButton(tr("Save Screenshot"), this, SLOT(saveScreenshot())); quitScreenshotButton = createButton(tr("Quit"), this, SLOT(close())); buttonsLayout = new QHBoxLayout; buttonsLayout->addStretch(); buttonsLayout->addWidget(newScreenshotButton); buttonsLayout->addWidget(saveScreenshotButton); buttonsLayout->addWidget(quitScreenshotButton); } QPushButton *Screenshot::createButton(const QString &text, QWidget *receiver, const char *member) { QPushButton *button = new QPushButton(text); button->connect(button, SIGNAL(clicked()), receiver, member); return button; } void Screenshot::updateScreenshotLabel() { screenshotLabel->setPixmap(originalPixmap.scaled(screenshotLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)); }
تعليق