Qt學習: QPixmap實現的截屏程序代碼示例

重要函數: 
1.bool isNull(); //判斷圖片是否爲空白. 
2.bool save(QString); //圖片保存到參數的路徑. 
3.QPixmap grabWidget(WId,x=0,y=0,w=-1,h=-1); //截取圖片. 
4.void 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;
    }
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101

以下是”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_OBJECTpublic:
    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_H12345678910111213141516171819202122232425262728293031323334353637

最後是”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();
}1234567891011


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章