Screenshot Example

来源:互联网 发布:通信工程概预算软件 编辑:程序博客网 时间:2024/06/05 14:18
#ifndef SCREENSHOT_H#define SCREENSHOT_H#include <QPixmap>#include <QWidget>class QCheckBox;class QGridLayout;class QGroupBox;class QHBoxLayout;class QLabel;class QPushButton;class QSpinBox;class QVBoxLayout;class Screenshot:public QWidget{ Q_OBJECTpublic: Screenshot();protected: void resizeEvent(QResizeEvent* event);private slots: void new_screenshot(); void save_screenshot(); void shoot_screenshot(); void update_checkbox();private: void create_options_groupbox(); void create_buttons_layout(); QPushButton* create_button(const QString &text,QWidget* receiver, const char* members); void update_screenshot_label(); QPixmap original_pixmap_; QLabel* screenshot_label_; QGroupBox* options_groupbox_; QSpinBox* delay_spinbox_; QLabel* delay_spinbox_label_; QCheckBox* hide_this_window_checkbox_; QPushButton* new_screenshot_button_; QPushButton* save_screenshot_button_; QPushButton* quit_screenshot_button_; QVBoxLayout* main_layout_; QGridLayout* options_groupbox_layout_; QHBoxLayout* butttons_layout_;};#endif#include "screenshot.h"#include <QtGui>Screenshot::Screenshot(){ screenshot_label_ = new QLabel; screenshot_label_->setSizePolicy(QSizePolicy::Expanding,  QSizePolicy::Expanding); screenshot_label_->setAlignment(Qt::AlignCenter); screenshot_label_->setMinimumSize(240, 160); create_options_groupbox(); create_buttons_layout(); main_layout_ = new QVBoxLayout; main_layout_->addWidget(screenshot_label_); main_layout_->addWidget(options_groupbox_); main_layout_->addLayout(butttons_layout_); setLayout(main_layout_); shoot_screenshot(); delay_spinbox_->setValue(5); setWindowTitle(tr("Screenshot")); resize(300, 200);}void Screenshot::resizeEvent(QResizeEvent* event){ QSize scaled_size = original_pixmap_.size(); scaled_size.scale(screenshot_label_->size(), Qt::KeepAspectRatio); if (!screenshot_label_->pixmap() || scaled_size != screenshot_label_->pixmap()->size()) {  update_screenshot_label(); }}void Screenshot::new_screenshot(){ if (hide_this_window_checkbox_->isChecked()) {  hide(); } new_screenshot_button_->setDisabled(true); QTimer::singleShot(delay_spinbox_->value() * 1000, this, SLOT(shoot_screenshot()));}void Screenshot::save_screenshot(){ QString format = "png"; QString initial_path = QDir::currentPath() + tr("/untitled.") + format; QString file_name = QFileDialog::getSaveFileName(this, tr("Save As"),  initial_path,  tr("%1 Files (*.%2);;All Files (*)")  .arg(format.toUpper())  .arg(format)); if (!file_name.isEmpty())  original_pixmap_.save(file_name, format.toAscii());}void Screenshot::shoot_screenshot(){ if (delay_spinbox_->value() != 0)  qApp->beep(); original_pixmap_ = QPixmap(); // clear image for low memory situations // on embedded devices. original_pixmap_ = QPixmap::grabWindow(QApplication::desktop()->winId()); update_screenshot_label(); new_screenshot_button_->setDisabled(false); if (hide_this_window_checkbox_->isChecked())  show();}void Screenshot::update_checkbox(){ if (delay_spinbox_->value() == 0) {  hide_this_window_checkbox_->setDisabled(true);  hide_this_window_checkbox_->setChecked(false); } else  hide_this_window_checkbox_->setDisabled(false);}void Screenshot::create_options_groupbox(){ options_groupbox_ = new QGroupBox(tr("Options")); delay_spinbox_ = new QSpinBox; delay_spinbox_->setSuffix(tr(" s")); delay_spinbox_->setMaximum(60); connect(delay_spinbox_, SIGNAL(valueChanged(int)), this, SLOT(updateCheckBox())); delay_spinbox_label_ = new QLabel(tr("Screenshot Delay:")); hide_this_window_checkbox_ = new QCheckBox(tr("Hide This Window")); options_groupbox_layout_ = new QGridLayout; options_groupbox_layout_->addWidget(delay_spinbox_label_, 0, 0); options_groupbox_layout_->addWidget(delay_spinbox_, 0, 1); options_groupbox_layout_->addWidget(hide_this_window_checkbox_, 1, 0, 1, 2); options_groupbox_->setLayout(options_groupbox_layout_);}void Screenshot::create_buttons_layout(){ new_screenshot_button_ = create_button(tr("New Screenshot"),  this, SLOT(new_screenshot())); save_screenshot_button_ = create_button(tr("Save Screenshot"),  this, SLOT(save_screenshot())); quit_screenshot_button_ = create_button(tr("Quit"), this, SLOT(close())); butttons_layout_ = new QHBoxLayout; butttons_layout_->addStretch(); butttons_layout_->addWidget(new_screenshot_button_); butttons_layout_->addWidget(save_screenshot_button_); butttons_layout_->addWidget(quit_screenshot_button_);}QPushButton *Screenshot::create_button(const QString &text, QWidget *receiver,           const char *member){ QPushButton *button = new QPushButton(text); button->connect(button, SIGNAL(clicked()), receiver, member); return button;}void Screenshot::update_screenshot_label(){ screenshot_label_->setPixmap(original_pixmap_.scaled(screenshot_label_->size(),  Qt::KeepAspectRatio,  Qt::SmoothTransformation));} #include "screenshot.h"#include <QtGui/QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); Screenshot w; w.show(); return a.exec();}

原创粉丝点击