Overriding a classes paintevent function when its called from within a class.



  • Hi, How would you go about overriding the paintevent of a class when its called within another class.
    e.g.
    @
    myClass::myClass(QWidget *parent) : QWidget(parent)
    {
    this->progressBar = new QProgressBar(this);
    }
    @

    I wish to override the progressBar paintevent from within the myClass class.

    Thanks.



  • Hi,

    I'm not sure what you want to do. If you want to override the panitEvent of the QProgressBar you will have to derive a subclass from it and use that.

    bq. I wish to override the progressBar paintevent from within the myClass class.

    So you want to provide an implementation to overrider method x which is a member of class A from method y which is a member of class B. C++ is renown to allow all sort of fuzzy stuff, but I still doubt that is possible.

    Or didn't I get you point?!?

    Best

    Soraltan



  • It is possible in Qt, because it is an event. Events can be intercepted by using an eventFilter. To do that, you do something like this:

    @
    myClass::myClass(QWidget *parent) : QWidget(parent)
    {
    this->progressBar = new QProgressBar(this);
    progressBar->installEventFilter(this);
    }

    bool myClass::eventFilter(QObject* o, QEvent* e)
    {
    if (o==progressBar && e && e->type() == QEvent::PaintEvent) {
    //handle the paint event for the widget
    return true; // if you want to stop the original paint event from arriving
    }

    // call base class implementation
    return QWidget::eventFilter(o, e);
    

    }
    @

    However, I think that it is probably the wrong way to do whatever you want to achieve. I think you should probably subclass QProgressBar, use a custom style on the progress bar, or use a style sheet on it. It depends on what you want to achieve, and there may be more than one way to do it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.