QImage file path - c++

I don't see this in the documentation anywhere, so it probably doesn't exist, but just in case:
I have a function that accepts as a parameter a vector of QImages. Each QImage was loaded from disk at some point and has not been edited--it is just read from. Ideally what I'd like to do is loop over all the QImages and output a list of their file paths to an XML file.
Unfortunately, I'm not seeing in the documentation any way to get at the original file path that the image was loaded from. So my question is, is it possible, given only a QImage, to figure out what file path the QImage was originally loaded from or not?
Something along the lines of:
QString QImage::getOriginalFilepath();
I know this is probably a futile question, but its always worth a shot to ask, I suppose.
(I'm using Qt 4.7, by the way.)

I looked through the code, and that information doesn't appear to be saved anywhere. QImage::load (and the constructor) uses QImageReader(fileName,format).read() and then copies the result to itself. The QImageReader is set to delete the device representation (the opened file object) as soon as it's finished reading.
So, in summary, it appears that this is not possible.

Also for me it seems that QImage doesn't provide an interface to get its path. I also think that this is not a missing feature, because there's no need to tie a QImage to a specific path. In the majority of all use cases the QImage has no corresponding physical file.
So the best way in your situation would be to subclass QImage and add this feature:
class MyImage : public QImage {
QString path_;
public:
MyImage(const QString& path);
QString path(); // getter
};
I omit the implementation details.

I usually create a struct for hold the QImage and the image path.
struct Image
{
QImage imageData;
QString filename;
};

Related

Qt stylesheet to use an image from memory

Qt stylesheets allow customizing of icons, for example the drop-down icon in a combo box. But all examples and docs which I have yet seen require having the image stored in a url. Then you can write for example QComboBox::down-arrow { image: url(path-to-file.png); }.
My question is: isn't there any trick which would allow to work around the fact that the file must be stored somewhere and use for example a pixmap from the memory?
I am asking because recently I found a nice hack which allows using QPixmap data to be used when displaying images in widgets which otherwise accept richtext (HTML formatted). See this code:
QPixmap preview;
// ... generate the pixmap here
QByteArray data;
QBuffer buffer(&data);
preview.save(&buffer, "PNG");
QString img = QStringLiteral("<img src='data:image/png;base64, %1'/>").arg(QString::fromLatin1(data.toBase64()));
//... and now you can display the image anywhere Qt accepts HTML formatted text,
// e.g. in QToolTip (which is my usecase).
This way I can use data from memory without saving it to file.
I am curious if there isn't any similar trick for images used in Qt style sheets.
There's nothing special, just put path to your image:
setStyleSheet("background-image: url(/path/to/file.png);");

Get QPixmap dynamic image path from std::string

I have a problem with the QPixmap under Qt. I want to set the Pixmap Image Path dynamically from a std::string. I have tried some variations, but without success.
This is my code:
QPixmap pixmap("data/25eaad8879f1d018caec98546279804f.png"); // this is working
// label
ui->imageSite1->setPixmap(pixmap); //working
But if I try to set the pixmappath dynamically, I don't see the image (inside the label).
e.g.
string path = "data/"+imgname+".png"; // imgname is a dynamic parameter
QPixmap pixmap(path); // this is not working
// label
ui->imageSite1->setPixmap(pixmap);
How can I do this correctly?
If you take a look at the Documentation of QPixmap, you'll see the load method declared as taking a QString as parameter. So you either have to build a QString directly (judging by your example, I think this is the better way) or convert the std::string to a QString with a constructor of the latter.
I have to load the filepath from a textfile and at the end of the line was a \n. They can't load the image in this case.
I removed this and load the pixmap via the load method. Now it's working.
string path = "data/"+imgfilepath;
QString testPath = QString::fromStdString(path);
qDebug() << testPath;
QPixmap pixmap;
pixmap.load(testPath);

How to save a QPixmap as a picture in a folder with C++ and Qt?

I'm trying to code a C++ function to save a selected picture to my program directory, using a Qt GUI.
So far, the "save" function I'm using on my QPixmap object won't save anything, and I can't figure out why.
Here is the code :
qImage = new QPixmap(path);
QPixmap qImage2 = qImage->scaled(this->width(),this->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
qImage2.toImage();
qImage2.save(QDir::currentPath());
qDebug()<<QDir::currentPath();
Can anyone help me ? :)
QDir::currentPath() returns the current working directory. Obviously, the filename itself is not specified. Simply append the needed filename, for example: QDir::currentPath() + "/123.png"
QPixmap::toImage() is a const method returning a QImage converted from a QPixmap. It literally does nothing useful in your code, remove it or use QImage instead.
QDir::currentPath() returns the current working directory which is not necessarily an application executable directory. Use QCoreApplication::applicationDirPath() instead if you need an executable directory.
Also, as pointed out by Violet Giraffe, there could be write permission issues.

wxWidgets - How to embed .ico and .png images and use to paint toolbar items

I want to create toolbar items and paint them from images in program memory rather than from files on disk. I would prefer to embed the images using a resource file (.rc), but if I need to roll my own somehow that's okay too.
The image files are .ico and .png.
Assuming I can use stuff from the .rc file, how do I complete this code?
void MyFrame::AddToolBarItem(int ID, wxSize sz, const char* file, const char* short_help, const char *long_help) {
const char *end = file+strlen(file);
const char *dot = std::find(file, end, '.');
if(dot != end) {
// It's an external file, with an extention, like .ico or .png
wxImage im(file);
im.Rescale(sz.x, sz.y, wxIMAGE_QUALITY_HIGH);
wxBitmap bmp(im);
my_tool_bar->AddTool(ID, short_help, bmp, long_help);
} else {
// It's a resource
// What now, StackOverflow???
}
}
EDIT: Okay, I'll make it easier. Forget about Rescale. I can do that with Gimp picture editor. Assume the images are the right size. You may also assume they are .ico or whatever format is convenient to wxWidgets.
EDIT 2: I am accepting an answer, but I have decided it is best just to embed the image "by hand", avoiding the .rc concept altogether. I wrote a little program to create a .cpp file with static initializers, but one can find them on the net. When compiled into the program, the .cpp file creates a copy of the image file in read-only memory. The .rc file is a Windows-specific thing, so it would be a good idea to avoid using it for custom icons and cursors, for portability. However, it does make sense to have a .rc file in the MS project that contains only the line "#include &LT;wx/msw/wx.rc>" That will give access to some stock cursors and so forth that are available by default on other platforms.
There is no support for loading PNG from resources in wx itself but it's easy to do it in your code, see http://wiki.wxwidgets.org/Embedding_PNG_Images#Embedding_PNG_images_into_Windows_rc_file
The Bitmaps and Icons overview gives a pretty good explanation of this. Basically once you have embedded the resource in your .rc file all you need to do is:
wxBitmap bmp(wxBITMAP(bmpnameinrcfile));
similarly for ico
wxIcon icon(wxICON(iconameinrcfile));

How to get the file/resource path for a QIcon

So let's say I do something like this:
QIcon myIcon(":/resources/icon.ico");
How can I later determine the path for that icon, e.g.
QString path = myIcon.getPath();
The problem is, there is no getPath() member, and I can't find anything similar, but surely there must be a way!
I guess I could inherit the QIcon class and add this functionality, but it's probably built in and I'm overlooking it?
The filename isn't stored in QIcon, it's only used for constructing the image.