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. Impossible to call paintEvent()???
Qt 6.11 is out! See what's new in the release blog

Impossible to call paintEvent()???

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 14.7k 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.
  • raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by
    #5

    [quote author="Greyze" date="1369399559"]
    I have used update() as I mentioned with no success, what exactly do you mean by next event loop iteration? surely every time I enter the mouse function this should happen?[/quote]
    You need to understand what the "Qt event loop":http://doc.qt.digia.com/qq/qq11-events.html is. But basically it is called with a short delay when you use update().

    And your event handlers should be protected instead of public!!

    --- 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
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #6

      Edit: deleted due to double post.

      --- 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
      • G Offline
        G Offline
        Greyze
        wrote on last edited by
        #7

        [quote author="raven-worx" date="1369399904"][quote author="Greyze" date="1369399559"]
        I have used update() as I mentioned with no success, what exactly do you mean by next event loop iteration? surely every time I enter the mouse function this should happen?[/quote]
        You need to understand what the "Qt event loop":http://doc.qt.digia.com/qq/qq11-events.html is. But basically it is called with a short delay when you use update().

        And your event handlers should be protected instead of public!!

        [/quote]

        I'm quite sure I understand them, reading that link just tells me what I already knew, and as it says: update() should eventually call paintEvent() and repaint() should force call paintEvent() immediately. And unfortunately neither of these work.

        Unless i'm being really stupid here, I can't see a problem with my code.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Santosh Reddy
          wrote on last edited by
          #8

          Ok, Have a look at this example, run it on your machine. Note that the paintEvent() is not called direcdtly but the text is updated every 100 ms. :)

          @class Widget : public QWidget
          {
          public:
          explicit Widget(QWidget * parent = 0)
          : QWidget(parent)
          , mNumber(0)
          , mText(QString::number(mNumber))
          {
          startTimer(100);
          }

          protected:
          void paintEvent(QPaintEvent * event)
          {
          QPainter painter(this);
          painter.drawText(event->rect(), mText);
          }

          void timerEvent(QTimerEvent * /*event*/)
          {
              mText = QString::number(mNumber++);
              update();
          }
          

          private:
          int mNumber;
          QString mText;
          };

          int main(int argc, char *argv[])
          {
          QApplication app(argc, argv);

          Widget widget;
          widget.show();
          
          return app.exec();
          

          }@

          SS

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

            well you said: "what exactly do you mean by next event loop iteration?" ... this seems to me like you do not understand what the event loop is...

            Did you try to make your event handlers protected instead of public as i stated?!

            --- 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
            • G Offline
              G Offline
              Greyze
              wrote on last edited by
              #10

              @Santosh
              That seems fine, i've also looked at the QT basicdrawing example which overrides paintEvent(); that program functions perfectly of course while my program has practically the same code.. no change :(

              @raven-worx
              Yeah I made the changes, that was silly of me having them as public. Didn't solve the problem however :(

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

                can you share the whole project? There has to be something wrong in your code which you didn't have posted yet...

                --- 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
                • G Offline
                  G Offline
                  Greyze
                  wrote on last edited by
                  #12

                  I've stripped down my project just to the barebones of this problem a link to a zip file is here: "http://sdrv.ms/13PKSnG":http://sdrv.ms/13PKSnG

                  The project is based on QT 4.8.4, and is built for Visual studio 2012, to run in VS2010 you need to change Platform Toolset to "v100" in the project properties (general).

                  I'm trying to create a colour sampler.
                  The pixel colour of the mouse position is taken and used to colour the frame on the form.
                  The code to take the pixel colour is WIN32 and is 100% functional.
                  When the app is running, the colour is taken whenever you click outside of the window, doing this calls a function to my QWidget, which in turn calls update().
                  You can see the colour code working by resizing the window, or minimizing/maximizing the window as QT by default calls paintEvent when these actions happen.

                  Thanks btw!

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Santosh Reddy
                    wrote on last edited by
                    #13

                    You missed to call the base class QMainWindow::event() in the ColourSampler::event(), which will cause the whole QObject (and QWidget) events to be blocked, and hence re-painting is also blocked.

                    Here are the recommended changes

                    @bool ColourSampler::event(QEvent event)
                    {
                    /

                    if (event->type() == QEvent::WindowDeactivate)
                    {
                    forceUpdate();
                    }
                    */
                    if (event->type() == QEvent::ActivationChange)
                    {
                    if(QApplication::activeWindow() != this)
                    {
                    forceUpdate();
                    this->show();
                    }
                    }

                    return QMainWindow::event(event); //<<<<<<<<<<<<<<
                    

                    }

                    void PaintBox::attemptRepaint()
                    {
                    getColour();
                    update();
                    }

                    void PaintBox::paintEvent( QPaintEvent * event)
                    {
                    QPainter paint(this);
                    paint.setBrush(QColor(_red, _green, _blue));
                    paint.drawRect(0, 0, width(), height());
                    }@

                    SS

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      Greyze
                      wrote on last edited by
                      #14

                      blurrrrrrrrrghhhhhhhhhhh

                      I removed that code a while back.. haven't got a clue why I did that.

                      Thanks very much for the help!

                      /high five to all!

                      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