Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt grab widget and save image

Tags:

c++

qt

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?

like image 206
CmasterG Avatar asked May 03 '13 15:05

CmasterG


2 Answers

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.
like image 168
Danio Avatar answered Oct 31 '22 22:10

Danio


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);
}
like image 1
darron Avatar answered Oct 31 '22 22:10

darron