Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add a paintEvent to a QWidget argument
Forum Updated to NodeBB v4.3 + New Features

How to add a paintEvent to a QWidget argument

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vahidnateghi
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      ...
      }
      @

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vahidnateghi
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

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

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • JeroentjehomeJ Offline
              JeroentjehomeJ Offline
              Jeroentjehome
              wrote on last edited by
              #6

              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.

              Greetz, Jeroen

              1 Reply Last reply
              0
              • Chris KawaC Online
                Chris KawaC Online
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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));
                @

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved