利用QT来实现读取电脑屏幕
2015-08-05 16:34
483 查看
在本次的小程序中,我们实现了如何读取电脑屏幕,并将他保存到图片中。首先看下这个程序的demo
![](https://img-blog.csdn.net/20150805162702878?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
在这个程序中,我们用到了一个QLabel类来显示图片,然后用到了一个GroupBox , 其中包括一耳光QLabel , QSpinBox , 一个QCheckBox。
然后还有一个就是三个按钮。
具体代码如下:
#ifndef SCREENSHPT_H
#define SCREENSHPT_H
#include <QWidget>
#include<QPixmap>
QT_BEGIN_NAMESPACE
class QCheckBox;
class QGridLayout;
class QGroupBox;
class QHBoxLayout;
class QLabel;
class QPushButton;
class QSpinBox;
class QVBoxLayout;
QT_END_NAMESPACE
class Screenshot : public QWidget
{
Q_OBJECT
public:
explicit Screenshot(QWidget *parent = 0);
~Screenshot();
protected:
void resizeEvent(QResizeEvent* event)Q_DECL_OVERRIDE;
signals:
private slots:
void newScreenshot();
void saveScreenshot();
void shootScreen();
void updateCheckBox();
private:
void createOptionGroupBox();
void createButtonLayout();
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;
};
#endif // SCREENSHPT_H
#include "screenshot.h"
#include<QtWidgets>
Screenshot::Screenshot(QWidget *parent) : QWidget(parent)
{
screenshotLabel = new QLabel;
screenshotLabel->setSizePolicy(QSizePolicy::Expanding , QSizePolicy::Expanding);
screenshotLabel->setAlignment(Qt::AlignCenter);
screenshotLabel->setMinimumSize(240 , 160);
createOptionGroupBox();
createButtonLayout();
mainlayout = new QVBoxLayout;
mainlayout->addWidget(screenshotLabel);
mainlayout->addWidget(optionsGroupBox);
mainlayout->addLayout(buttonsLayout);
setLayout(mainlayout);
shootScreen();
delaySpinBox->setValue(5);
resize(300 , 200);
}
Screenshot::~Screenshot()
{
}
void Screenshot::resizeEvent(QResizeEvent *event)
{
QSize scaleSize = originalPixmap.size();
scaleSize.scale(screenshotLabel->size() , Qt::KeepAspectRatio);
if(!screenshotLabel->pixmap() ||scaleSize != 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.toLatin1().constData());
}
}
void Screenshot::shootScreen(){
if(delaySpinBox->value() != 0){
qApp->beep();
}
originalPixmap = QPixmap();
QScreen * screen = QGuiApplication::primaryScreen();
if(screen){
originalPixmap = screen->grabWindow(0);
}
updateScreenshotLabel();
newScreenshotButton->setDisabled(false);
if(hideThisWindowCheckBox->isChecked())
show();
}
void Screenshot::updateCheckBox()
{
if(delaySpinBox->value() == 0){
hideThisWindowCheckBox->setDisabled(true);
hideThisWindowCheckBox->setChecked(false);
}else{
hideThisWindowCheckBox->setDisabled(false);
}
}
//void Screenshot::updateCheckBox()
//{
// if(delaySpinBox->value() == 0){
// hideThisWindowCheckBox->setDisabled(true);
// hideThisWindowCheckBox->setChecked(false);
// }else{
// hideThisWindowCheckBox->setDisabled(false);
// }
//}
void Screenshot::createOptionGroupBox(){
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::createButtonLayout(){
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));
}
其中对一些函数进行了封装,在日常开发中也可以这样做,这样做减少了代码的重复
在这个程序中,我们用到了一个QLabel类来显示图片,然后用到了一个GroupBox , 其中包括一耳光QLabel , QSpinBox , 一个QCheckBox。
然后还有一个就是三个按钮。
具体代码如下:
#ifndef SCREENSHPT_H
#define SCREENSHPT_H
#include <QWidget>
#include<QPixmap>
QT_BEGIN_NAMESPACE
class QCheckBox;
class QGridLayout;
class QGroupBox;
class QHBoxLayout;
class QLabel;
class QPushButton;
class QSpinBox;
class QVBoxLayout;
QT_END_NAMESPACE
class Screenshot : public QWidget
{
Q_OBJECT
public:
explicit Screenshot(QWidget *parent = 0);
~Screenshot();
protected:
void resizeEvent(QResizeEvent* event)Q_DECL_OVERRIDE;
signals:
private slots:
void newScreenshot();
void saveScreenshot();
void shootScreen();
void updateCheckBox();
private:
void createOptionGroupBox();
void createButtonLayout();
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;
};
#endif // SCREENSHPT_H
#include "screenshot.h"
#include<QtWidgets>
Screenshot::Screenshot(QWidget *parent) : QWidget(parent)
{
screenshotLabel = new QLabel;
screenshotLabel->setSizePolicy(QSizePolicy::Expanding , QSizePolicy::Expanding);
screenshotLabel->setAlignment(Qt::AlignCenter);
screenshotLabel->setMinimumSize(240 , 160);
createOptionGroupBox();
createButtonLayout();
mainlayout = new QVBoxLayout;
mainlayout->addWidget(screenshotLabel);
mainlayout->addWidget(optionsGroupBox);
mainlayout->addLayout(buttonsLayout);
setLayout(mainlayout);
shootScreen();
delaySpinBox->setValue(5);
resize(300 , 200);
}
Screenshot::~Screenshot()
{
}
void Screenshot::resizeEvent(QResizeEvent *event)
{
QSize scaleSize = originalPixmap.size();
scaleSize.scale(screenshotLabel->size() , Qt::KeepAspectRatio);
if(!screenshotLabel->pixmap() ||scaleSize != 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.toLatin1().constData());
}
}
void Screenshot::shootScreen(){
if(delaySpinBox->value() != 0){
qApp->beep();
}
originalPixmap = QPixmap();
QScreen * screen = QGuiApplication::primaryScreen();
if(screen){
originalPixmap = screen->grabWindow(0);
}
updateScreenshotLabel();
newScreenshotButton->setDisabled(false);
if(hideThisWindowCheckBox->isChecked())
show();
}
void Screenshot::updateCheckBox()
{
if(delaySpinBox->value() == 0){
hideThisWindowCheckBox->setDisabled(true);
hideThisWindowCheckBox->setChecked(false);
}else{
hideThisWindowCheckBox->setDisabled(false);
}
}
//void Screenshot::updateCheckBox()
//{
// if(delaySpinBox->value() == 0){
// hideThisWindowCheckBox->setDisabled(true);
// hideThisWindowCheckBox->setChecked(false);
// }else{
// hideThisWindowCheckBox->setDisabled(false);
// }
//}
void Screenshot::createOptionGroupBox(){
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::createButtonLayout(){
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));
}
其中对一些函数进行了封装,在日常开发中也可以这样做,这样做减少了代码的重复
相关文章推荐
- QT学习 第一章:基本对话框
- 使用Shiboken为C++和Qt库创建Python绑定
- Qt定时器和随机数详解
- Qt实现图片移动实例(图文教程)
- Qt for Android开发实例教程
- QModelIndex/Role/Model介紹<二>
- 基于PyQt5的快速开发框架QFramer
- ok6410开发板移植DirectFB手记
- 【笔记】给Qt内嵌一个Chrome吧
- 【算法】最短路径之A*搜索
- qt入门必备
- 在 Qt4 中使用 C++11
- Hello Word ~ v0.2.2 背单词软件发布 -- By WHYPRO
- ubuntu下opencv和qt的安装配置
- linux下opengl的安装(with qt)
- qt 学习小节
- QT的命名风格
- QT 中的sleep
- QT 串口的监控
- 在VS2010下配置QT和ITK+VTK