先来看下效果:
选择“隐藏窗口”,点击“新建截图”,就可以实现截取屏幕,点击保存截图也可以保存
代码具体实现:
ScreenShot::ScreenShot(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ScreenShot)
{
ui->setupUi(this);
//把一个定时器和一个槽函数关联起来
QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(timeIsOut()));
}
ScreenShot::~ScreenShot()
{
delete ui;
}
void ScreenShot::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
//点击新建按钮触发的槽函数
void ScreenShot::on_NewPicButton_clicked()
{
if (ui->HidecheckBox->checkState())
{
this->hide();
timer.start(ui->TimespinBox->value()*1000);
}
}
//定时时间到时触发的槽函数
void ScreenShot::timeIsOut()
{
pixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
ui->pixmaplabel->setPixmap(pixmap.scaled(ui->pixmaplabel->size()));
timer.stop();
this->show();
}
void ScreenShot::on_ExitButton_clicked()
{
this->close();
}
//保存图片
void ScreenShot::on_SavePicButton_clicked()
{
QString filename = QFileDialog::getSaveFileName(this, "Save File",
QDir::currentPath(), "*.jpg;;*.bmp");
pixmap.save(filename);
}
阅读(1546) | 评论(0) | 转发(1) |