Solved Adding your own function to QFrame or any other class
-
How can i add my own function to QFrame Class so that i can use it with the QFrame object.
-
Used it where ?
You can always subclass a QFrame. -
@mrjj I simply wanna add a function namely "Drawshadow" of my own which would draw shadow around the borders of QFrame object.
This is what i wanna do:
QFrame test ; test.drawshadow(...);
but before i use drawshadow(); function i must have to first define it in the QFrame class so that QFrame object ( in this case test ) would work.
-
@Ahti
Hi
You can just create your own subclass#include <QFrame> class MyFrame : public QFrame { Q_OBJECT public: void drawshadow(.....) /// your func MyFrame(QWidget* parent = 0, Qt::WindowFlags f = 0): QFrame(parent, f) { } protected: virtual void paintEvent(QPaintEvent* e) override { QFrame::paintEvent(e); // pass event to base class } };
But there a note:
You cannot draw outside of the QFrame. So keep that in mind.
Also you cannot use a Painter object outside of paintEvent.
But if you use the Effect classes, it might work.
http://doc.qt.io/qt-5/qgraphicsdropshadoweffect.htmlSo if its ok the shadow is inside the QFrame area, then u can use the paintEvent.
Elese the qgraphicsdropshadoweffect might help you out.If you just need a shadow, it can be done with no function inside class.
QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect; effect->setBlurRadius(5); effect->setXOffset(5); effect->setYOffset(5); effect->setColor(Qt::black); ui->frame->setAutoFillBackground(true); ui->frame->setGraphicsEffect(effect);
Updated code to use setAutoFillBackground so drop is not drawn inside.
-
@mrjj Thanks :)
-
@Ahti
Np :)
If you need many of them , just make it a function and call it pr Frame.void AddDrop(QFrame * frame ) { if (!frame) return; QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect; effect->setBlurRadius(5); effect->setXOffset(5); effect->setYOffset(5); effect->setColor(Qt::black); frame->setAutoFillBackground(true); frame->setGraphicsEffect(effect); }
and call it from mainwin (after setupUI ) like
AddDrop(ui->frame1);
AddDrop(ui->frame2);
AddDrop(ui->frame3);
etc -
@mrjj I already created that generic function :D thanks anyways :) what does autofillbackground method does btw ?
-
@Ahti said in Adding your own function to QFrame or any other class:
autofillbackground
http://doc.qt.io/qt-5/qwidget.html#autoFillBackground-prop
Basically, it controls if the background is filled before the Paint is called.