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); //把图片按比例缩放.
成都创新互联是一家专业提供抚顺县企业网站建设,专注与成都网站设计、成都网站建设、外贸网站建设、成都h5网站建设、小程序制作等业务。10年已为抚顺县众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
下面是一个简单的截图器的示例代码:
首先从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#include "ui_c.h"#include #include #include #include #include #include #include #include #include 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"#includeint main(int argc, char *argv[]) { QApplication a(argc, argv); c w; w.show(); return a.exec(); }1234567891011
网页题目:Qt学习:QPixmap实现的截屏程序代码示例
分享URL:http://azwzsj.com/article/josccs.html