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(){}//保存图片的槽.voidc::savePictureSlot(){if(this->isSaved)QMessageBox::information(this,"warning!",QString::fromLocal8Bit("没有可以被保存的图片!"));else{this->savePicture();}}voidc::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();}voidc::closeSlot(){if(this->isSaved){this->close();}else{//设置退出提示框.QMessageBoxtemp(QMessageBox::NoIcon,QString::fromLocal8Bit("是否要退出"),QString::fromLocal8Bit("你的图片尚未保存,是否要保存?"));temp.setStandardButtons(QMessageBox::Yes|QMessageBox::No);temp.setButtonText(QMessageBox::Yes,QString::fromLocal8Bit("保存"));temp.setButtonText(QMessageBox::No,QString::fromLocal8Bit("退出"));intstatus=temp.exec();if(status==QMessageBox::Yes){this->savePicture();}elseif(status==QMessageBox::No){this->close();}}}voidc::closeEvent(QCloseEvent*event){closeSlot();}voidc::savePicture(){//调用文件的类.设置了标题和路径.QFileDialogtemp(this,"SavePicture","c:/users/administrator/desktop");//修改模式为保存模式.temp.setAcceptMode(QFileDialog::AcceptSave);//自动添加后缀为"jpg".temp.setDefaultSuffix("jpg");intstatus=temp.exec();if(status==QDialog::Accepted){QStringpath=temp.selectedFiles()[0];//图片保存到这个路径里去.boolok=pixmap.save(path);if(ok)QMessageBox::information(this,QString::fromLocal8Bit("保存成功"),QString::fromLocal8Bit("图片已成功保存!"));this->isSaved=true;}}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
以下是”c.h下的代码:”
#ifndefC_H#defineC_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>classc:publicQMainWindow{Q_OBJECTpublic:c(QWidget*parent=0);~c();voidsavePicture();privateslots:voidcutScreenSlot();voidsavePictureSlot();voidcloseSlot();protected:voidcloseEvent(QCloseEvent*event);private:Ui::cClassui;QPixmappixmap;boolisSaved=true;};#endif//C_H12345678910111213141516171819202122232425262728293031323334353637
最后是”main.cpp下的代码:”
#include"c.h"#include<QtWidgets/QApplication>intmain(intargc,char*argv[]){QApplicationa(argc,argv);cw;w.show();returna.exec();}1234567891011
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。