How to add a paintEvent to a QWidget argument



  • Hi!

    I have the below code :

    @void elevationForm:: setupUi(QWidget *Form)
    {
    if (Form->objectName().isEmpty())
    Form->setObjectName(QStringLiteral("Form"));
    Form->resize(231, 231);
    Form->setStyleSheet(QStringLiteral("background-image: url(:/pics/Elevation);"));

    }@

    Now I want to paint something in Form and to do that I need to implement the paintEvent in Form. Now how can I do that?
    I have already tried the below code and the compiler sent an error:
    @Form->paintEvent =this->drawline;
    Form->repaint();@

    Where drawline is:
    @void elevationForm::drawline(QPaintEvent *)
    {
    QPainter * P = new QPainter();
    QPen pen;
    pen.setBrush(Qt::red);
    P->setPen(pen);
    P->begin(this);
    P->drawLine(10,10,50,50);
    P->end();
    }@

    Thank you in advance


  • Moderators

    You don't "call" paint event. It is called from your class whenever the widget needs to be redrawn.
    You should implement the paintEvent in your class, like so:

    @
    class elevationForm {
    ...
    void paintEvent(QPaintEvent * e);
    }

    void elevationForm::paintEvent(QPaintEvent * e) {
    //call this if you need the default drawing first
    ParentOf ThisClass::paintEvent(e);

    //you've got leak in your code, just use stack variable
    //also painter needs to know where to draw (this)
    QPainter p(this);

    //do something with p
    ...
    }
    @


  • Moderators

    or when you need to outsource the implementation out of the paintEvent() handler you should pass a pointer to a QPainter instead.
    Then you can call this method from your paintEvent() handler and also from other methods which needs painting.

    But as Chris said, a method with a QPaintEvent* argument should never be called by yourself.



  • Yes , as you can see from my code, I'm not trying to call the QpaintEvent of Form myself. I'm trying to implement it from outside of Form, and that is my question. Is there a way for me to implement the QpaintEvent function of Form from outside?


  • Moderators

    no... why can't you just subclass your form widget?



  • Hi,
    Maybe install an eventfilter on the Form, there do the redraw, but subclassing the form widget is a better suggestion. Keeps it more object orientated.


  • Moderators

    Well, to be honest you can, but this is counter-intuitive and not really Qtish. I wouldn't recommend it but for the sake of completeness here's how to do it:

    @
    //this object will do the painting
    class EventHandler : public QObject {
    Q_OBJECT
    public:
    EventHandler(QObject* parent = 0) : QObject(parent) {}
    protected:
    bool eventFilter(QObject obj, QEvent * event)
    {
    //handle only paint events
    //you should also check if obj is really a widget but I'll skip that
    if(event->type() == QEvent::Paint)
    {
    QPaintEvent
    pevent = static_cast<QPaintEvent*>(event);
    QWidget* widget = static_cast<QWidget*>(obj);
    QPainter p(widget);
    //do something with p
    ...
    //since we did all the drawing we return true
    //this prevents obj from receiving paintEvent
    return true;
    }
    return false; //ignore all other events
    }
    };

    //and then somewhere install the filter:
    myFormOrWhatever->installEventFilter(new EventHandler(parent));
    @


Log in to reply
 

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