How to make a QGraphicsEffect show?
-
Hello Forum,
I try to blur a GraphicsView while doing some heavy lifting, but it doesn't get displayed until it's too late (upon reentering the event loop).
[Code]
QGraphicsBlurEffect *effect = new QGraphicsBlurEffect(this);
ui->myGraphicsView->viewport()->setGraphicsEffect(effect);
doSomeLongishCalculation();
ui->myGraphicsView->viewport()->setGraphicsEffect(0);
[/Code]Inserting a 'qApp->processEvents()' doesn't help.
What am I doing wrong? Thank you for any clues.
Mustapha
-
It depends where you are applying the effect.
On widgets and QGraphicsItems it's straight-forward - just setGraphicsEffect.
On QImage/QPixmap/QIcon, it's complicated, but possible. You MUST use QImage for actual rendering, that is why my function is QImage-centric.
@QImage applyEffectToImage(QImage src, QGraphicsEffect effect, int extent)
{
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()+QSize(extent2, extent2), QImage::Format_ARGB32);
res.fill(Qt::transparent);
QPainter ptr(&res);
scene.render(&ptr, QRectF(), QRectF( -extent, -extent, src.width()+extent2, src.height()+extent*2 ) );
return res;
}
@ -
It depends where you are applying the effect.
On widgets and QGraphicsItems it's straight-forward - just setGraphicsEffect.
On QImage/QPixmap/QIcon, it's complicated, but possible. You MUST use QImage for actual rendering, that is why my function is QImage-centric.
@QImage applyEffectToImage(QImage src, QGraphicsEffect effect, int extent)
{
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()+QSize(extent2, extent2), QImage::Format_ARGB32);
res.fill(Qt::transparent);
QPainter ptr(&res);
scene.render(&ptr, QRectF(), QRectF( -extent, -extent, src.width()+extent2, src.height()+extent*2 ) );
return res;
}
@