QT实现将截屏保存为图片实践
  Y8XIq1u6ceQW 2023年11月19日 31 0

QT代实现将截屏保存为图片实践

使用QGuiApplication::primaryScreen()可以取得当前屏幕,取得将QScreen->grabWindow(0)存入QPixmap即可。

这里可以看到QT的封装非常方便我们取得屏幕截图。

 开始截屏 按扭代码

void TestWidget::on_pushButton_clicked()
{
    this->hide();//隐藏当前窗口
    Sleep(300);//延时
    //QPixmap  screenPixmap = QPixmap();
    QScreen *scr = QGuiApplication::primaryScreen();
    if (scr)
    {
        screenPixmap = scr->grabWindow(0);
        QLabel *lb=ui->label;
        lb->setPixmap(screenPixmap);
        lb->setScaledContents(true);
    }
    this->show();//显示当前窗口
}

保存为 按扭代码

void TestWidget::on_pushButton_2_clicked()
{
    QDateTime current_date_time = QDateTime::currentDateTime();
    QString current_date = current_date_time.toString("yyyyMMddhhmmsszzz");
    QString format = "jpg";
    QString currentPath = QDir::currentPath() + tr("/截屏")+current_date+tr(".") + format;

    QString fileName = QFileDialog::getSaveFileName(this, tr("保存为..."), currentPath,
                                                    tr("%1 文件(*.%2);;所有文件(*)")
                                                    .arg(format.toUpper())
                                                    .arg(format));
    if (!fileName.isEmpty())
           screenPixmap.save(fileName, format.toLatin1().constData());
}

测试效果

QT实现将截屏保存为图片实践_屏幕截图

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

推荐阅读
  lA9hNJ8xYnRP   2023年12月12日   30   0   0 Qt
  lA9hNJ8xYnRP   2023年12月06日   31   0   0 Qt
  nIBmcPWZzwbL   2023年11月13日   32   0   0 Qt
  nIBmcPWZzwbL   2023年11月13日   44   0   0 Qt
  lA9hNJ8xYnRP   2023年12月06日   34   0   0 构造函数Qt
  lA9hNJ8xYnRP   2023年12月07日   30   0   0 Qt
  lA9hNJ8xYnRP   2023年12月11日   27   0   0 Qt
  lA9hNJ8xYnRP   2023年11月25日   38   0   0 Qt数据
  lA9hNJ8xYnRP   2023年11月30日   30   0   0 Qt表视图
  nIBmcPWZzwbL   2023年11月13日   30   0   0 Qt
  nIBmcPWZzwbL   2023年11月13日   35   0   0 Qt
  nIBmcPWZzwbL   2023年11月13日   44   0   0 Qt
Y8XIq1u6ceQW