您的位置:首页 > 编程语言 > Qt开发

利用QT来实现读取电脑屏幕

2015-08-05 16:34 483 查看
在本次的小程序中,我们实现了如何读取电脑屏幕,并将他保存到图片中。首先看下这个程序的demo



在这个程序中,我们用到了一个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