Is there a way to insert a QPixmap object in html?

Simple situation: I have an object that has a member QPixmap. First, an object is created (pixmap is null), then pixmap is read from the database and inserted into the object. I need to insert this pixmap in html code () and display this html code in QLabel, but I have no idea how to do this because the pixmap path is unknown.

I know how to embed images from resource files and from files on my hard drive, but this is not the case. I used the class QMimeSourceFactoryon qt 3.3.4, but in 4.6.2 it is deprecated. The assistant says, "Use a resource system instead." But the resource system is compiled using the application, but it is necessary for reading images at runtime.

I would be grateful for any help. Thank.

+5
source share
4 answers

I know this is an old question, but here is another option.

I had a similar problem with images in QToolTip. I could reference the images from the disk perfectly, but the default zoom behavior is nonsmooth and looked awful. I overridden my own tooltip class and used a custom class QTextDocumentso that I could override QTextDocument::loadResource().

In your case, you can specify the keyword in the imgsrc attribute . Then in your implementation loadResource()return the QPixmap identified with the keyword.

Here is the base 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);
  }
};
+2

QPixmap QLabel, QLabel:: setPixmap. pixmap QPixmap:: loadFromData.

HTML, . QWebView

    QByteArray byteArray;
    QBuffer buffer(&byteArray);
    pixmap.save(&buffer, "PNG");
    QString url = QString("<img src=\"data:image/png;base64,") + byteArray.toBase64() + "\"/>";

()

QLabel:: setText HTML, . , : Qt rich-text.

pixmap QWebView QNetworkAccessManager createRequest(), URL- ( "myprot:" ) pixmap. .

+11

, . :

#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWidget window;
    window.resize(320, 240);
    window.show();
    window.setWindowTitle(
        QApplication::translate("toplevel", "Top-level widget"));
    QLabel* label = new QLabel(&window);
    label->setTextFormat(Qt::RichText);
    QString text = "<html><h1>Test</h1>here is an image: ";
    QPixmap pixmap("testicon.jpg");
    QByteArray byteArray;
    QBuffer buffer(&byteArray);
    pixmap.save(&buffer, "PNG");
    QString url = QString("<img src=\"data:image/png;base64,") + byteArray.toBase64() + "\"/>";
    text += url;
    text += "</html>";
    label->setText(text);

    label->move(100, 100);
    label->show();
    return app.exec();
}
+5

These are my two cents about serializing / deserializing QPixmap to / from Base64 e strings. I have included methods for loading / saving the image to a text file, as well as two simple toBase64()and fromBase64()to help with coding HTML, SQL, or JSON.

#include "b64utils.h"
#include <QBuffer>
#include <QFile>
#include <QTextStream>

/**
 * Serializes a QPixmap object into a Base64 string
 */
QString B64Utils::toBase64(QPixmap *pixmap) {
    // Convert the pixel map into a base64 byte array
    QBuffer *buffer = new QBuffer;
    pixmap->save(buffer, "png");
    QByteArray b64 = buffer->data().toBase64();
    QString *b64Str = new QString(b64);
    return *b64Str;
}

/**
 * Serializes a QPixmap object into a Base64 string and save it to a file
 */
bool B64Utils::savePixmapToBase64(QPixmap *pixmap, QString filePath) {
    // Opens a file for writing text
    QFile file(filePath);
    if (!file.open(QIODevice::WriteOnly | QFile::Text)) return false;

    // Write the Base64 string into the file
    QTextStream stream(&file);
    stream << toBase64(pixmap);
    file.close();

    return true;
}

/**
 * Deserializes a Base64 string, representing an image, into a QPixmap
 */
QPixmap* B64Utils::fromBase64(QString b64Str) {
    QPixmap *pixmap = new QPixmap;
    pixmap->loadFromData(QByteArray::fromBase64(b64Str.toUtf8()));
    return pixmap;
}

/**
 * Retrieves a Base64 string, representing an image, from a file and deserializes it into a QPixmap
 */
QPixmap* B64Utils::loadPixmapFromBase64(QString filePath) {
    // Opens a file for reading text
    QFile file(filePath);
    if (!file.open(QFile::ReadOnly | QFile::Text)) return nullptr;

    // Reads the contents of the file into a string
    QTextStream in(&file);
    QString b64Str = in.readAll();
    file.close();

    return fromBase64(b64Str);
}
0
source

All Articles