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);
}
}