有没有办法在html中插入QPixmap对象?

发布于 2024-11-18 21:15:39 字数 335 浏览 2 评论 0原文

简单的情况:我有一个对象,它有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寂寞笑我太脆弱 2024-11-25 21:15:40

如果您只想在 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 并在那里插入像素图数据。但这看起来有点矫枉过正。

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

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

(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.

剩余の解释 2024-11-25 21:15:40

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

#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();
}

I put this in another answer to be able to format the code. I wrote the following program and it works as intended:

#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();
}
遇见了你 2024-11-25 21:15:40

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

我在 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);
  }
};

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);
  }
};
魂归处 2024-11-25 21:15:40

这是我关于将 QPixmap 序列化到 Base64 e 字符串/从 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);
}

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() and fromBase64() that help with HTML, SQL or JSON encoding.

#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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文