c++ - Qt5 - 为什么不截图并保存到文件中?

标签 c++ windows qt qt5 qpixmap

为什么它不保存任何文件?

#include "mainwindow.h"
#include <QApplication>
#include <QPixmap>
#include <QPainter>
#include <QList>
#include <QScreen>


QPixmap grabScreens() {
  auto screens = QGuiApplication::screens();
  QList<QPixmap> scrs;
  int w = 0, h = 0, p = 0;
  foreach (auto scr, screens) {
    QPixmap pix = scr->grabWindow(0);
    w += pix.width();
    if (h < pix.height()) h = pix.height();
    scrs << pix;
  }
  QPixmap final(w, h);
  QPainter painter(&final);
  final.fill(Qt::black);
  foreach (auto scr, scrs) {
    painter.drawPixmap(QPoint(p, 0), scr);
    p += scr.width();
  }
  return final;
}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QPixmap pixmap = grabScreens();

    QFile file("file.jpg");
    file.open(QIODevice::WriteOnly);
    pixmap.save(&file, "JPG", 1);

    MainWindow w;
    w.show();

    return a.exec();
}

enter image description here

最佳答案

您要查找的文件应该与可执行文件位于同一文件夹中。

如果您从 Qtcreator 运行代码,它应该位于构建目录中,如项目页面的build设置中指定的那样。

关于c++ - Qt5 - 为什么不截图并保存到文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47617618/

相关文章:

python - 调用 Python 3.x 命令的跨平台方式

c++ - Qt:信号和槽 vs C++:消息传递

c++ - QQuickImageProvider::requestImage图像缩放,如何处理

c++ - istream::ignore 和 getline() 混淆

c++ - 测量分支被错误预测的频率

c# - File.Open for read access denied on executing file in Windows

c++ - 解析 XML 样式表处理指令

c++ - Qt & SqlLite - 错误的数据库路径

c++ - 递归函数以反向字符串C++

windows - 在 Windows 上的 STDIN 上使用 IO::Select