Qt学习: QPixmap实现的截屏程序代码示例

重要函数:
1.bool isNull(); //判断图片是否为空白.
2.bool save(QString); //图片保存到参数的路径.
3.QPixmap grabWidget(WId,x=0,y=0,w=-1,h=-1); //截取图片.
4.QPixmap scaled(QSize); //把图片按比例缩放.图片本身并没有缩放,缩放后的图片作为返回值了.

下面是一个简单的截图器的示例代码:

首先从Qt设计师拖拽出如下界面,并且进行布局.
这里写图片描述


以下是”c.cpp下的代码:”

#include "c.h"

c::c(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    //设置按钮的图标.可不设置.只是为了好看.
    ui.grabScreenButton->setIcon(QIcon("Icons/cut.png"));
    ui.saveButton->setIcon(QIcon("Icons/save.png"));

    //设置按钮的快捷键.
    ui.grabScreenButton->setShortcut(tr("Ctrl+G"));
    ui.saveButton->setShortcut(tr("Ctrl+S"));
    ui.cancelButton->setShortcut(tr("Ctrl+Q"));

    //连接信号与槽.
    connect(ui.grabScreenButton, SIGNAL(clicked()), this, SLOT(cutScreenSlot()));
    connect(ui.saveButton, SIGNAL(clicked()), this, SLOT(savePictureSlot()));
    connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(closeSlot()));
}

c::~c()
{

}
//保存图片的槽.
void c::savePictureSlot()
{
    if (this->isSaved)
        QMessageBox::information(this, "warning!", QString::fromLocal8Bit("没有可以被保存的图片!"));
    else
    {
        this->savePicture();
    }
}
void c::cutScreenSlot()
{
    //先隐藏窗口.
    this->hide();
    //延迟3秒钟.
    Sleep(3000);

    //截取当前屏幕的图片.
    pixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
    //让label框自动填满内容.
    ui.label->setScaledContents(true);
    //让label框应用图片,并且自动缩放像素.
    ui.label->setPixmap(pixmap.scaled(ui.label->size()));
    this->isSaved = false;
    //显示窗口.
    this->show();
}
void c::closeSlot()
{
    if (this->isSaved)
    {
        this->close();
    }
    else
    {
        //设置退出提示框.
        QMessageBox temp(QMessageBox::NoIcon, QString::fromLocal8Bit("是否要退出"), QString::fromLocal8Bit("你的图片尚未保存,是否要保存?"));
        temp.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        temp.setButtonText(QMessageBox::Yes, QString::fromLocal8Bit("保存"));
        temp.setButtonText(QMessageBox::No, QString::fromLocal8Bit("退出"));
        int status = temp.exec();
        if (status == QMessageBox::Yes)
        {
            this->savePicture();
        }
        else if (status == QMessageBox::No)
        {
            this->close();
        }
    }
}
void c::closeEvent(QCloseEvent*event)
{
    closeSlot();
}

void c::savePicture()
{
    //调用文件的类.设置了标题和路径.
    QFileDialog temp(this, "Save Picture", "c:/users/administrator/desktop");
    //修改模式为保存模式.
    temp.setAcceptMode(QFileDialog::AcceptSave);
    //自动添加后缀为"jpg".
    temp.setDefaultSuffix("jpg");
    int status = temp.exec();
    if (status == QDialog::Accepted)
    {
        QString path = temp.selectedFiles()[0];
        //图片保存到这个路径里去.
        bool ok = pixmap.save(path);
        if (ok)
            QMessageBox::information(this, QString::fromLocal8Bit("保存成功"), QString::fromLocal8Bit("图片已成功保存!"));
        this->isSaved = true;
    }
}

以下是”c.h下的代码:”

#ifndef C_H
#define C_H

#include <QtWidgets/QMainWindow>
#include "ui_c.h"
#include <QDesktopWidget>
#include <QPixmap>
#include <QLabel>
#include <QPushButton>
#include <QIcon>
#include <windows.h>
#include <QMessageBox>
#include <QFileDialog>
#include <QCloseEvent>

class c : public QMainWindow
{
    Q_OBJECT

public:
    c(QWidget *parent = 0);
    ~c();
    void savePicture();
private slots:
    void cutScreenSlot();
    void savePictureSlot();
    void closeSlot();
protected:
    void closeEvent(QCloseEvent*event);
private:
    Ui::cClass ui;
    QPixmap pixmap;
    bool isSaved = true;
};

#endif // C_H

最后是”main.cpp下的代码:”

#include "c.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    c w;
    w.show();
    return a.exec();
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值