Qt grab widget and save image
Solution 1
Here is the simplest way to save a widget as an image. This approach works on Qt 5:
ui->myWidget->grab().save("image.png");
Qt doc.:
- QPixmap QWidget::grab(const QRect &rectangle = QRect(QPoint(0, 0), QSize(-1, -1)))
- bool QPixmap::save(const QString &fileName, const char *format = nullptr, int quality = -1) const.
Solution 2
The plugins don't go in the application EXE folder directly, but a "plugins" folder under the application folder. I've also had to place it in an "imageformats" folder instead of "plugins" at least once, I believe. That might have been a different platform/special build.
See this Qt deployment guide for windows.
Also, your "new char[]" call is probably going to eventually crash. You need to reserve space for the null character:
char * buffer = new char[current_string.length() + 1];
Also also, you don't need all the std::string stuff just to get an extension. That's likely to just be frustrating over time.
QString saveFilename = QFileDialog::getSaveFileName(this, "Save as", "Choose a filename", "PNG(*.png);; TIFF(*.tiff *.tif);; JPEG(*.jpg *.jpeg)");
QString saveExtension = "PNG";
int pos = saveFilename.lastIndexOf('.');
if (pos >= 0)
saveExtension = saveFilename.mid(pos + 1);
if(!QPixmap::grabWidget(m_widget).save(saveFilename, qPrintable(saveExtension)))
{
// since you have a widget, just use grabWidget() here. winId() would possibly have
// portability issues on other platforms. qPrintable(saveExtension) is effectively
// the same as saveExtension.toLocal8Bit().constData()
QMessageBox::warning(this, "File could not be saved", "ok", QMessageBox::Ok);
}
Related videos on Youtube
CmasterG
Updated on September 15, 2022Comments
-
CmasterG over 1 year
I have the following problem. I want to grab a widget and save it as an image. I want to save it as png, jpeg, and tiff. I have written the following code:
QString s = QFileDialog::getSaveFileName(this, "Save as", "Choose a filename", "PNG(*.png);; TIFF(*.tiff *.tif);; JPEG(*.jpg *.jpeg)"); std::string current_string = s.toLocal8Bit().constData(); //current_string = current_string + ".png"; char * buffer = new char[current_string.length()]; std::string temp = buffer; char* temp2 = &temp[0]; strcpy(buffer, current_string.c_str()); char* pch = strtok (temp2,"."); pch = strtok (NULL, "."); if(!QPixmap::grabWindow(m_widget->winId()).save(buffer,pch)) { QMessageBox::warning(this, "File could not be saved", "ok", QMessageBox::Ok); }
This works fine on my laptop. When I make a Visual Studio Setup it also works fine on my laptop, but when I install it on another pc, then the png format works fine (saves the right image), but jpeg and tif can't be saved. Then I tried it on a further pc, but there I tried it directly in Visual Studio with the project file. There I have all project settings like on my pc etc. and there jpeg and tif don't work. PNG works but it only saves a white image on that pc. Further I also tried there the installation file and its the same PNG = white image.
Can anyone help me?
-
Admin almost 11 yearsYou need image plugins, see for example this question: stackoverflow.com/questions/2241141/…
-
-
CmasterG almost 11 yearsThanks a lot for the answer. This works. But I still have the problem that on 5 pcs the grab window works, and on one pc it saves a white image.
-
darron almost 11 yearsHmm.. strange. Is there anything different about that PC? (Different video card, video driver version, OS... ?)
-
CmasterG almost 11 yearsIt is a Nvidia GTX470. The other PCs are also Nvidia and one of them has a AMD card.
-
G. Cito almost 10 yearsNice short answer - it could be improved by adding a link to the documentation and some summary/description of its contents (since links disappear or move) - such as the section or version of the docs where you found the reference. Welcome to SO.