为什么它不保存任何文件?
#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();
}
最佳答案
您要查找的文件应该与可执行文件位于同一文件夹中。
如果您从 Qtcreator 运行代码,它应该位于构建目录中,如项目页面的build设置中指定的那样。
关于c++ - Qt5 - 为什么不截图并保存到文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47617618/