有没有办法在html中插入QPixmap对象?
简单的情况:我有一个对象,它有一个 QPixmap 成员。首先创建对象(现在像素图为空),然后从数据库读取像素图并将其插入对象中。我需要在 html 代码 () 中插入该 pixmap 并在 QLabel
中显示该 html 代码,但我不知道如何制作它,因为 pixmap 的路径未知。
我知道如何从资源文件和硬盘上的文件插入图像,但事实并非如此。我在 qt 3.3.4 上使用 QMimeSourceFactory
类,但在 4.6.2 上它已被弃用。助理说:“改为使用资源系统”。但是资源系统是随app一起编译的,但是运行时需要读取图片。
我将不胜感激任何帮助。谢谢。
Simple situation: I have an object, which has a QPixmap
member. Object first created (pixmap is null now), then pixmap readed from data base and inserted in object. I need to insert that pixmap in html code () and display that html code in a QLabel
but I have no idea how to make it, because pixmap's path is unknown.
I know how to insert images from resource files and from files on my hard-disk, but it isn't that case. I was using QMimeSourceFactory
class on qt 3.3.4, but on 4.6.2 it is deprecated. Assistant says: "Use resource system instead". But resource system compiles with app, but it is needed to read images during runtime.
I will be grateful for any help. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只想在 QLabel 中显示 QPixmap,则应该使用 QLabel::setPixmap。您可以使用 QPixmap::loadFromData 在内存中构造像素图。
如果您想在 HTML 中显示内存像素图,例如在 QWebView 中,您可以使用
(未经测试)
QLabel::setText 不适用于 HTML,但适用于富文本。我不知道 Qt 富文本实现是否支持 data: 协议。
将像素图插入 QWebView 的另一种方法是使用 QNetworkAccessManager 的子类并重新实现其 createRequest() 函数来检查具有您自己的协议(“myprot:”)的 URL 并在那里插入像素图数据。但这看起来有点矫枉过正。
If you only want to display a QPixmap in a QLabel, you should use QLabel::setPixmap. You can construct the pixmap in memory by using QPixmap::loadFromData.
If you want to display a memory pixmap in HTML, e.g. in a QWebView, you can use
(untested)
QLabel::setText does not work with HTML but with rich-text. I do not know if the data: protocol is supported by the Qt rich-text implementation.
Another way to insert a pixmap into a QWebView would be to use a subclass of QNetworkAccessManager and reimplement its createRequest() function to check for URLs with your own protocol ("myprot:") and insert the pixmap data there. But this looks like overkill.
我将其放在另一个答案中以便能够格式化代码。我编写了以下程序,它按预期工作:
I put this in another answer to be able to format the code. I wrote the following program and it works as intended:
我知道这是一个老问题,但这是另一种选择。
我在 QToolTip 中的图像也遇到了类似的问题。我可以很好地引用磁盘中的图像,但默认的缩放行为不平滑并且看起来很糟糕。我重新实现了自己的工具提示类并使用了自定义的 QTextDocument 类,以便可以重写 QTextDocument::loadResource() 。
对于您的情况,您可以在
img
src 属性中指定关键字。然后在loadResource()
的实现中返回用关键字标识的 QPixmap。这是基本代码(在此上下文中未经测试):
I know this is an old question, but here is another option.
I had a similar issue with images in
QToolTip
. I could reference images from disk fine, but the default scaling behavior is non-smooth and looked terrible. I reimplemented my own tooltip class and used a customQTextDocument
class so that I could overrideQTextDocument::loadResource()
.In your case, you can specify a keyword in the
img
src attribute. Then in your implementation ofloadResource()
return the QPixmap identified with the keyword.Here is the basic code (untested in this context):
这是我关于将 QPixmap 序列化到 Base64 e 字符串/从 Base64 e 字符串反序列化的两分钱。我提供了将图像加载/保存为文本文件的方法,还提供了两个简单的
toBase64()
和fromBase64()
来帮助进行 HTML、SQL 或 JSON 编码。This is my two cents about serializing/deserializing QPixmap to/from Base64 e strings. I've included methods to load/save the image as a text file but also two simple
toBase64()
andfromBase64()
that help with HTML, SQL or JSON encoding.