5

简单的情况:我有一个对象,它有一个QPixmap成员。首先创建对象(现在像素图为空),然后从数据库中读取像素图并插入对象中。我需要在 html 代码 () 中插入该像素图并在 a 中显示该 html 代码,QLabel但我不知道如何制作它,因为像素图的路径是未知的。

我知道如何从资源文件和硬盘上的文件中插入图像,但事实并非如此。我QMimeSourceFactory在 qt 3.3.4 上使用类,但在 4.6.2 上它已被弃用。助理说:“改用资源系统”。但是资源系统是用app编译的,但是需要在运行时读取图片。

我将不胜感激任何帮助。谢谢。

4

4 回答 4

12

如果只想在 QLabel 中显示 QPixmap,则应使用 QLabel::setPixmap。您可以使用 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 富文本实现是否支持 data: 协议。

将像素图插入 QWebView 的另一种方法是使用 QNetworkAccessManager 的子类并重新实现其 createRequest() 函数以使用您自己的协议(“myprot:”)检查 URL 并在那里插入像素图数据。但这看起来有点矫枉过正。

于 2011-07-07T12:21:23.350 回答
5

我把它放在另一个答案中以便能够格式化代码。我编写了以下程序,它按预期工作:

#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();
}
于 2011-07-08T07:27:34.250 回答
3

我知道这是一个老问题,但这是另一种选择。

我在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);
  }
};
于 2014-03-11T21:25:34.633 回答
0

这是我关于将 QPixmap 序列化/反序列化到 Base64 e 字符串的两分钱。我已经包含了将图像加载/保存为文本文件的方法,还有两个简单的方法toBase64()fromBase64()它们有助于 HTML、SQL 或 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);
}
于 2019-09-07T08:53:17.703 回答