SBGlass(QObject *parent = 0); ~SBGlass(); virtual void install(QWidget* w); virtual void remove(); void enableColor(const QColor& color = QColor(111, 111, 100)); void disableColor(); void enableOpacity(qreal opacity = 0.5); void disableOpacity(); void enableInfoBlock(QMovie* movie = 0, const QString& text = QString::null); void disableInfoBlock(); void enableAnimationBlock(QMovie* movie = 0); void disableAnimationBlock(); void enableInfoTextBlock(const QString& text = QString::null); void disableInfoTextBlock(); QLabel& getInfoTextBlock(); void setMovie(QMovie* movie);
C++ (Qt)#include <QWidget>#include <QBackingStore>#include <QPainter>#include <QGridLayout> class GridManager : public QWidget{ bool in_paint_ = false;public: GridManager( QWidget* parent = nullptr ) : QWidget( parent ) { setLayout( new QGridLayout() ); } void addWidget( QWidget* w, int row, int column ) { qobject_cast< QGridLayout* >( layout() )->addWidget( w, row, column ); } protected: bool event( QEvent* e ) { if( e->type() == QEvent::ChildAdded ) static_cast< QChildEvent* >( e )->child()->installEventFilter( this ); } bool eventFilter( QObject* o, QEvent* e ) { if( e->type() == QEvent::Paint ) { if( !in_paint_ ) { drawChild( qobject_cast< QWidget* >( o ) ); return true; } } return QWidget::eventFilter( o, e ); } void drawChild( QWidget* w ) { w->setAttribute( Qt::WA_WState_InPaintEvent, false ); auto bs = window()->backingStore(); auto r = w->rect().translated( - w->mapFrom( w->window(), QPoint( 0, 0 ) ) ); bs->beginPaint( QRegion( r ) ); QPainter p( bs->paintDevice() ); p.translate( r.topLeft() ); in_paint_ = true; w->render( &p ); in_paint_ = false; drawOverChild( &p, w ); bs->endPaint(); } virtual void drawOverChild( QPainter* p, QWidget* w ) { p->setBrush( QColor( 0xff, 0xff, 0xff, 200 ) ); p->drawRect( w->rect() ); }};
C++ (Qt)class Dummy : public QWidget {public: using QWidget::paintEvent;}; bool eventFilter( QObject* o, QEvent* e ){ if (e->type() == QEvent::Paint) { Dummy * w = static_cast<Dummy*> (o); w->paintEvent(static_cast<QPaintEvent *> (e)); QPainter painter(w); drawOverChild(&painter, w); return true; } return QWidget::eventFilter( o, e );}