Skip to content

Instantly share code, notes, and snippets.

@andrey-str
Created September 13, 2018 10:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrey-str/312c84c81ef50594f149a7b9f3bfd566 to your computer and use it in GitHub Desktop.
Save andrey-str/312c84c81ef50594f149a7b9f3bfd566 to your computer and use it in GitHub Desktop.
Blur QWidget window content(with any 3D on top)
namespace
{
QImage applyEffectToImage(QImage src, QGraphicsEffect *effect)
{
if(src.isNull()) return QImage(); // No need to do anything else
if(!effect) return src; // No need to do anything else
QGraphicsScene scene;
QGraphicsPixmapItem item;
item.setPixmap(QPixmap::fromImage(src));
item.setGraphicsEffect(effect);
scene.addItem(&item);
QImage res(src.size(), QImage::Format_ARGB32);
res.fill(Qt::transparent);
QPainter ptr(&res);
scene.render(&ptr, QRectF(), QRectF(0, 0, src.width(), src.height()));
return res;
}
QPixmap prepareDialogBackgroundImage(QWidget* parent)
{
QScreen *screen = QGuiApplication::primaryScreen();
auto pixmap = screen->grabWindow(parent->winId());
const auto effect = new QGraphicsBlurEffect();
effect->setBlurHints(QGraphicsBlurEffect::PerformanceHint);
effect->setBlurRadius(2);
auto image = applyEffectToImage(pixmap.toImage(), effect);
return QPixmap::fromImage(image);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment