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. QWidget unwanted paintEvent
Forum Updated to NodeBB v4.3 + New Features

QWidget unwanted paintEvent

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 9.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.
  • L Offline
    L Offline
    luca
    wrote on last edited by
    #1

    Hi all,

    I have a QDialog with some child: a QWidget (subclassed in MyWidget) and a QLabel .

    The MyWidget has a custom paintEvent :

    @

    void MyWidget::paintEvent(QPaintEvent *event)
    {
    QPainter p(this);
    p.drawImage(0,0,image);
    }
    @

    Image is a big QImage so drawing the image is slow.

    My problem is that if the QLabel text or its state (enabled, disabled) change it always automatically execute MyWidget::paintEvent taking a lot of time.
    I only want manually call update to draw a new image.
    How can I avoid this?

    Thanks

    1 Reply Last reply
    0
    • I Offline
      I Offline
      iunknwn
      wrote on last edited by
      #2

      Hello Luca,

      What exactly are you trying to do? Perhaps there is another way to resolve your problem.

      Vista x64 with Qt 4.8.2 and VS2010

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luca
        wrote on last edited by
        #3

        [quote author="iunknwn" date="1282750845"]Hello Luca,

        What exactly are you trying to do? Perhaps there is another way to resolve your problem.[/quote]

        Hi iunknwn,
        the application is a kind of SCADA system that generate an image from an svg changing some svg object according on the state of something.

        The application must run on the beagleboard (linux embedded) and the problem is that "drawing" an image on a QWidget is slow in that board.

        Near the QWidget there is a QLabel that change its text frequently and every text change it generate an "update()" on the QWidget.

        1 Reply Last reply
        0
        • I Offline
          I Offline
          iunknwn
          wrote on last edited by
          #4

          Look into QPaintEvent::Region().

          From the doc:

          "Many widgets can simply repaint their entire surface when asked to, but some slow widgets need to optimize by painting only the requested region: QPaintEvent::region()."

          Alternately, if only label is changing and not the QImage, you can pass additional 4 agruments (sx, sy, sw, sh) to drawImage and specify only part of image to be re-drawn. Look into QPainter::drawImage() documentation.

          Vista x64 with Qt 4.8.2 and VS2010

          1 Reply Last reply
          0
          • L Offline
            L Offline
            luca
            wrote on last edited by
            #5

            Thanks,

            but I need to avoid the update() when don't needed also because Qt clear the QWidget before calling paintEvent and if I draw only a part of the image I can't see the other part.

            1 Reply Last reply
            0
            • I Offline
              I Offline
              iunknwn
              wrote on last edited by
              #6

              wait a minute. Why do you even need to make a custom widget and go through painting if all you have is a QImage and a QLabel. Why not make both a child of a QWidget and put them in a layout?

              @class MyWidget : public QWidget

              {

              Q_OBJECT

              private:

              QLabel* lbl;

              QImage* img;

              public:

              MyWidget(QWidget* parent = 0) {

              lbl = new QLabel();
              img = new QImage();

              QVBoxLayout* layout = new QVBoxLayout();

              layout->addWidget(lbl);

              layout->addWidget(img);

              setLayout(layout);
              }

              }
              @

              Am I missing anything?

              Vista x64 with Qt 4.8.2 and VS2010

              1 Reply Last reply
              0
              • L Offline
                L Offline
                luca
                wrote on last edited by
                #7

                I finally found this:
                @
                Qt::WA_OpaquePaintEvent

                Indicates that the widget paints all its pixels when it receives a paint event. Thus, it is not required for operations like updating, resizing, scrolling and focus changes to erase the widget before generating paint events. The use of WA_OpaquePaintEvent provides a small optimization by helping to reduce flicker on systems that do not support double buffering and avoiding computational cycles necessary to erase the background prior to painting. Note: Unlike WA_NoSystemBackground, WA_OpaquePaintEvent makes an effort to avoid transparent window backgrounds. This flag is set or cleared by the widget's author.
                @

                So I put:

                setAttribute(Qt::WA_OpaquePaintEvent);

                in MyWidget constructor to avoid erasing the widget before paintEvent and in paintEvent I put:
                @
                void MyWidget::paintEvent(QPaintEvent *event)
                {
                if(update_required)
                {
                QPainter p(this);
                p.drawImage(0,0,image);
                }
                }
                @

                This way I can avoid the unwanted drawImage.

                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