//-- QTDIR/examples/desktop/screenshot/screenshot.cpp/#192void Screenshot::updateScreenshotLabel(){ screenshotLabel->setPixmap(originalPixmap.scaled(screenshotLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)); const QPixmap *pixmap = screenshotLabel->pixmap(); if( pixmap && !pixmap->isNull() ) { QString imgSrcTmpl = QString( "<img src=\"%3\" width=%1 height=%2>" ) .arg( pixmap->width() ) .arg( pixmap->height() ) .arg( "%1" ); QString strToolTip; QString tmpFileName( "./test.png" ); if( pixmap->save( tmpFileName, "PNG" ) ) { QString str( "image (file path):<br>%1<br>" ); strToolTip.append( str.arg( imgSrcTmpl.arg( tmpFileName ) ) ); } QBuffer imageBuffer; if (imageBuffer.open(QBuffer::ReadWrite) && pixmap->save(&imageBuffer, "PNG")) { QString str( "image (base64):<br>%1<br>" ); strToolTip.append( str.arg( imgSrcTmpl.arg( "data:image/png;base64," +QLatin1String( imageBuffer.buffer().toBase64() ) ) ) ); } screenshotLabel->setToolTip( strToolTip ); }}
C++ (Qt)void registerPixmap( const QPixmap& pm, const QString& name ){ QFileInfo info; QString key = QString( "qt_pixmap_:%1_%2_0_0" ).arg( name ). arg( QString::number( info.lastModified().toTime_t() ) ); QPixmapCache::insert( key, pm );} void Screenshot::updateScreenshotLabel(){ . . . registerPixmap( *screenshotLabel->pixmap(), "test" ); screenshotLabel->setToolTip( "<img src=':test'>" ); // перед именем файла обязательно вставить ':', чтобы Qt искала в своих ресурсах. . . .}
C++ (Qt)#include <QPixmapCache>#include <private/qtextimagehandler_p.h> static QImage richTextImageLoader( const QString& name, const QString &context ){ QImage img; QPixmap pix; if( QPixmapCache::find( name, pix ) ) return pix.toImage(); return QImage();} void Screenshot::updateScreenshotLabel(){ . . . static bool init; if( !init++ ) QTextImageHandler::externalLoader = richTextImageLoader; QPixmapCache::insert( "test", *screenshotLabel->pixmap() ); screenshotLabel->setToolTip( "<img src='test'>" ); . . .}