Apply QGraphicsBlurEffect to a window
-
Hello,
In my application, my final goal is to blur background the current window when a modal QDialog pops up.
To start, I want to apply it to the current mainwindow but I don't really understand how QGraphicsBlurEffect really works.
Can someone explain to my how to use it to blur a window. I try to pass the window as argument but it's not working.Thanks a lot.
-
Does the following example help?
@#include <QtGui>
class Demo : public QWidget {
Q_OBJECT
public:
Demo(){
setLayout(new QVBoxLayout);
QPushButton *pb = new QPushButton("Toggle Blur");
layout()->addWidget(pb);
pb->setCheckable(true);
connect (pb, SIGNAL(clicked(bool)), SLOT(testSlot(bool)));
}private slots:
void testSlot(bool blurred){
QGraphicsBlurEffect *effect = new QGraphicsBlurEffect(this) ;
setGraphicsEffect(blurred? effect : 0);
}
};#include "main.moc"
int main( int argc, char** argv ){
QApplication app( argc, argv );
Demo w;
w.show();
return app.exec();
}@
-
Yes Sigrid, It helped a lot, I didn't realized it was that simple.
However, my final goal to apply it on a QGraphicView but according to what I find on tn the doc it does not seems to be possible. Am I right? -
Which part of the documentation are you referring to? Does setting the effect on the view's viewport() or on the item itself work for you?
@#include <QtGui>
class Demo : public QWidget {
Q_OBJECT
public:
Demo(){
setLayout(new QVBoxLayout);
QPushButton *pb = new QPushButton("Toggle Blur");
layout()->addWidget(pb);
pb->setCheckable(true);view = new QGraphicsView(this);
scene = new QGraphicsScene(view);
view->setScene(scene);
item1 = new QGraphicsEllipseItem();
QRect rectangle(10, 20, 80, 60);
item1->setRect(rectangle);
scene->addItem(item1);layout()->addWidget(view);
connect (pb, SIGNAL(clicked(bool)), SLOT(testSlot(bool)));
}private slots:
void testSlot(bool blurred){
QGraphicsBlurEffect *effect = new QGraphicsBlurEffect(this) ;
setGraphicsEffect(blurred? effect : 0);QGraphicsBlurEffect *effect2 = new QGraphicsBlurEffect(view) ;
view->viewport()->setGraphicsEffect(blurred? effect2 : 0);
}
private:
QGraphicsView *view;
QGraphicsScene *scene;
QGraphicsEllipseItem *item1;
};#include "main.moc"
int main( int argc, char** argv ){
QApplication app( argc, argv );
Demo w;
w.show();
return app.exec();
}@
-
ouch
I wasn't applying the effect to the viewport port directly to the QGraphicsView
I shall test your way soon thanks