有什么办法可以在HTML中插入QPixmap对象? [英] is there any way to insert QPixmap object in html?
问题描述
简单情况:我有一个对象,该对象具有QPixmap
成员.首先创建对象(现在pixmap为null),然后从数据库中读取pixmap并将其插入对象中.我需要将pixmap插入html代码()中并在QLabel
中显示该html代码,但是我不知道如何制作它,因为pixmap的路径未知.
我知道如何从资源文件和硬盘上的文件插入图像,但是不是这种情况.我在qt 3.3.4上使用了QMimeSourceFactory
类,但在4.6.2上已弃用.助手说:改为使用资源系统".但是资源系统可以通过app进行编译,但是需要在运行时读取图像.
我将不胜感激.谢谢.
我知道这是一个老问题,但这是另一个选择.
我在QToolTip
中的图像上也遇到了类似的问题.我可以从磁盘上精细地引用图像,但是默认的缩放行为是不平滑的,看起来很糟糕.我重新实现了自己的工具提示类,并使用了自定义的QTextDocument
类,以便可以覆盖QTextDocument::loadResource()
.
根据您的情况,您可以在img
src属性中指定一个关键字.然后在loadResource()
的实现中,返回用关键字标识的QPixmap.
这是基本代码(在此情况下未经测试):
class MyTextDocument : public QTextDocument
{
protected:
virtual QVariant loadResource(int type, const QUrl &name)
{
QString t = name.toString();
if (t == myKeyword)
return myPixmap;
return QTextDocument::loadResource(type, name);
}
};
class MyLabel : public QFrame
{
public:
MyLabel(QWidget *parent)
: QFrame(parent)
, m_doc(new MyTextDocument(this))
{ }
virtual void paintEvent(QPaintEvent *e)
{
QStylePainter p(this);
// draw the frame if needed
// draw the contents
m_doc->drawContents(&p);
}
};
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.
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 custom QTextDocument
class so that I could override QTextDocument::loadResource()
.
In your case, you can specify a keyword in the img
src attribute. Then in your implementation of loadResource()
return the QPixmap identified with the keyword.
Here is the basic code (untested in this context):
class MyTextDocument : public QTextDocument
{
protected:
virtual QVariant loadResource(int type, const QUrl &name)
{
QString t = name.toString();
if (t == myKeyword)
return myPixmap;
return QTextDocument::loadResource(type, name);
}
};
class MyLabel : public QFrame
{
public:
MyLabel(QWidget *parent)
: QFrame(parent)
, m_doc(new MyTextDocument(this))
{ }
virtual void paintEvent(QPaintEvent *e)
{
QStylePainter p(this);
// draw the frame if needed
// draw the contents
m_doc->drawContents(&p);
}
};
这篇关于有什么办法可以在HTML中插入QPixmap对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!