QT中使用定时器来截图

来源:互联网 发布:锋云搜歌文件 生成软件 编辑:程序博客网 时间:2024/06/06 02:37

先来看下效果:
http://blog.chinaunix.net/photo/116111_101004190629.jpg
 
选择“隐藏窗口”,点击“新建截图”,就可以实现截取屏幕,点击保存截图也可以保存
 
代码具体实现:
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);
}