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. custom widget update() and repaint() will not work
Qt 6.11 is out! See what's new in the release blog

custom widget update() and repaint() will not work

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 766 Views 2 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.
  • A Offline
    A Offline
    AI_Messiah
    wrote on last edited by
    #1

    So I have a widget for an image. I know that it does work in relation to the first call of the paint event but when I send in an image I can't get this to happen again

    header:

    #ifndef IMAGEDISPLAY_H
    #define IMAGEDISPLAY_H
    
    #include <QWidget>
    #include <QImage>
    
    class QPaintEvent;
    
    class ImageDisplay : public QWidget
    {
        Q_OBJECT
    public:
        explicit ImageDisplay(QWidget *parent = nullptr);
    
        virtual void paintEvent(QPaintEvent* event);
        void setImage(QImage img);
    signals:
    
    private:
        QImage toShow;
    };
    
    #endif // IMAGEDISPLAY_H
    
    

    source

    #include "imagedisplay.h"
    #include <QtGui>
    
    ImageDisplay::ImageDisplay(QWidget *parent)
        : QWidget{parent}
    {
        //toShow = QImage(":/Images/Images/Columbine-Flower-Colors-768x768-tile.jpg");
    
    }
    
    void ImageDisplay::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        painter.setPen(QColor::fromRgb(255, 255, 255, 255));
        QBrush solid = QBrush(QColor::fromRgb(255, 255, 255, 255), Qt::BrushStyle::SolidPattern);
        auto size = this->size();
        auto offset = 4;
        painter.drawImage(QRect(0, 0, 768, 768), toShow);
    
    }
    
    void ImageDisplay::setImage(QImage img)
    {
        toShow = img;
        this->repaint();
    }
    
    
    C SGaistS 2 Replies Last reply
    0
    • A AI_Messiah

      So I have a widget for an image. I know that it does work in relation to the first call of the paint event but when I send in an image I can't get this to happen again

      header:

      #ifndef IMAGEDISPLAY_H
      #define IMAGEDISPLAY_H
      
      #include <QWidget>
      #include <QImage>
      
      class QPaintEvent;
      
      class ImageDisplay : public QWidget
      {
          Q_OBJECT
      public:
          explicit ImageDisplay(QWidget *parent = nullptr);
      
          virtual void paintEvent(QPaintEvent* event);
          void setImage(QImage img);
      signals:
      
      private:
          QImage toShow;
      };
      
      #endif // IMAGEDISPLAY_H
      
      

      source

      #include "imagedisplay.h"
      #include <QtGui>
      
      ImageDisplay::ImageDisplay(QWidget *parent)
          : QWidget{parent}
      {
          //toShow = QImage(":/Images/Images/Columbine-Flower-Colors-768x768-tile.jpg");
      
      }
      
      void ImageDisplay::paintEvent(QPaintEvent *event)
      {
          QPainter painter(this);
          painter.setPen(QColor::fromRgb(255, 255, 255, 255));
          QBrush solid = QBrush(QColor::fromRgb(255, 255, 255, 255), Qt::BrushStyle::SolidPattern);
          auto size = this->size();
          auto offset = 4;
          painter.drawImage(QRect(0, 0, 768, 768), toShow);
      
      }
      
      void ImageDisplay::setImage(QImage img)
      {
          toShow = img;
          this->repaint();
      }
      
      
      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      "Will not work" is not a good description of what is happening versus what was expected.
      Does it behave differently if you call QWidget::update() rather than QWidget::repaint()?

      void ImageDisplay::setImage(const QImage &img)
      {
          toShow = img;
          update();
      }
      

      Without code we cannot see if you are doing something that blocks the event loop or painting.

      1 Reply Last reply
      1
      • A AI_Messiah

        So I have a widget for an image. I know that it does work in relation to the first call of the paint event but when I send in an image I can't get this to happen again

        header:

        #ifndef IMAGEDISPLAY_H
        #define IMAGEDISPLAY_H
        
        #include <QWidget>
        #include <QImage>
        
        class QPaintEvent;
        
        class ImageDisplay : public QWidget
        {
            Q_OBJECT
        public:
            explicit ImageDisplay(QWidget *parent = nullptr);
        
            virtual void paintEvent(QPaintEvent* event);
            void setImage(QImage img);
        signals:
        
        private:
            QImage toShow;
        };
        
        #endif // IMAGEDISPLAY_H
        
        

        source

        #include "imagedisplay.h"
        #include <QtGui>
        
        ImageDisplay::ImageDisplay(QWidget *parent)
            : QWidget{parent}
        {
            //toShow = QImage(":/Images/Images/Columbine-Flower-Colors-768x768-tile.jpg");
        
        }
        
        void ImageDisplay::paintEvent(QPaintEvent *event)
        {
            QPainter painter(this);
            painter.setPen(QColor::fromRgb(255, 255, 255, 255));
            QBrush solid = QBrush(QColor::fromRgb(255, 255, 255, 255), Qt::BrushStyle::SolidPattern);
            auto size = this->size();
            auto offset = 4;
            painter.drawImage(QRect(0, 0, 768, 768), toShow);
        
        }
        
        void ImageDisplay::setImage(QImage img)
        {
            toShow = img;
            this->repaint();
        }
        
        
        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @AI_Messiah hi,

        Might be a silly question but since you don't seem to be anything special with the image, why not use QLabel ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

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