Search code examples
htmlqtqpixmap

is there any way to insert QPixmap object in html?


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.


Solution

  • 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);
      }
    };