- 截取窗口:适用做视频通话截图
cpp
QScreen *screen=QGuiApplication::primaryScreen();
QPixmap pixmap=screen->grabWindow(ui->widget->winId());//ui->widget->winId()你需要截图的窗口id
pixmap.save("./1.jpg","jpg");
- 全屏截图
cpp
QScreen *screen = QGuiApplication::primaryScreen();
if (screen) {
// 截取整个屏幕
QPixmap screenshot = screen->grabWindow(0);
// 可选择保存路径和文件名
QString fileName = QFileDialog::getSaveFileName(nullptr, "保存截图",
QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss") + ".png",
"Images (*.png *.xpm *.jpg)");
if (!fileName.isEmpty()) {
// 保存截图
screenshot.save(fileName);
}
}
- 全屏截图加水印
cpp
QScreen *screen = QGuiApplication::primaryScreen();
if (screen) {
// 截取整个屏幕
QPixmap screenshot = screen->grabWindow(0);
// 创建 QPainter 对象在截图上绘制
QPainter painter(&screenshot);
// 设置水印样式
painter.setPen(QColor(255, 0, 0, 128)); // 红色,透明度为128
painter.setFont(QFont("Arial", 30, QFont::Bold));
// 添加水印文本
QString watermarkText = "小gpt";
painter.drawText(screenshot.rect(), Qt::AlignVCenter | Qt::AlignHCenter, watermarkText);
// 结束绘制
painter.end();
// 可选择保存路径和文件名
QString fileName = QFileDialog::getSaveFileName(nullptr, "保存截图",
QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss") + ".png",
"Images (*.png *.xpm *.jpg)");
if (!fileName.isEmpty()) {
// 保存带水印的截图
screenshot.save(fileName);
}
}
- 需要源码可以到这里下载
添加链接描述