简体   繁体   English

QImage文件路径

[英]QImage file path

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. 我有一个函数接受QImages矢量作为参数。 Each QImage was loaded from disk at some point and has not been edited--it is just read from. 每个QImage都在某个时刻从磁盘加载而且还没有被编辑 - 它只是从中读取。 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. 理想情况下,我想要做的是遍历所有QImages并将其文件路径列表输出到XML文件。

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? 所以我的问题是,是否有可能只给出一个QImage来确定QImage最初加载的文件路径是什么?

Something along the lines of: 有点像:

QString QImage::getOriginalFilepath(); 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.) (顺便说一下,我正在使用Qt 4.7。)

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. QImage :: load(和构造函数)使用QImageReader(fileName,format).read(),然后将结果复制到自身。 The QImageReader is set to delete the device representation (the opened file object) as soon as it's finished reading. QImageReader设置为在完成读取后立即删除设备表示(打开的文件对象)。

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. 同样对我来说,似乎QImage没有提供接口来获取它的路径。 I also think that this is not a missing feature, because there's no need to tie a QImage to a specific path. 我也认为这不是缺失的功能,因为没有必要将QImage绑定到特定路径。 In the majority of all use cases the QImage has no corresponding physical file. 在大多数用例中,QImage没有相应的物理文件。

So the best way in your situation would be to subclass QImage and add this feature: 因此,在您的情况下,最好的方法是继承QImage并添加此功能:

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. 我通常创建一个结构来保存QImage和图像路径。

struct Image
{
    QImage imageData;
    QString filename;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM