Qt grab widget and save image

14,800

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.:

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);
}
Share:
14,800

Related videos on Youtube

CmasterG
Author by

CmasterG

Updated on September 15, 2022

Comments

  • CmasterG
    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?

  • CmasterG
    CmasterG almost 11 years
    Thanks 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
    darron almost 11 years
    Hmm.. strange. Is there anything different about that PC? (Different video card, video driver version, OS... ?)
  • CmasterG
    CmasterG almost 11 years
    It is a Nvidia GTX470. The other PCs are also Nvidia and one of them has a AMD card.
  • G. Cito
    G. Cito almost 10 years
    Nice 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.