Created
September 13, 2018 10:56
-
-
Save andrey-str/312c84c81ef50594f149a7b9f3bfd566 to your computer and use it in GitHub Desktop.
Blur QWidget window content(with any 3D on top)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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