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. SEGFAULT in paintEvent() and drawImage() [SOLVED]
Qt 6.11 is out! See what's new in the release blog

SEGFAULT in paintEvent() and drawImage() [SOLVED]

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

    Hi, i have created a widget that inherits from QWidget and reimplements paintEvent. The only purpose of this widget is to show a QImage as faster as possible. This is the code;

    @
    void ImageDisplay::paintEvent(QPaintEvent *event)
    {
    QWidget::paintEvent(event);

    QImage image = this->m_image; 
    
    QSize size = image.size();
    size.scale(this->size(), Qt::KeepAspectRatio);
    QRect rect(QPoint(), size);
    rect.moveCenter(this->rect().center());
    
    QPainter painter(this);
    
    painter.drawImage(rect,
                      image,
                      image.rect());
    

    }

    void ImageDisplay::setImage(const QImage &image)
    {
    this->m_image = image;

    this->update();
    

    }
    @

    But, it seams to cause a SEGFAULT when calling setImage in certain cases, specially when calling from inside a slot .
    When i debugg it with GDB, it give me the error in this line:

    @
    painter.drawImage(rect,
    image,
    image.rect());
    @

    But all parameters in rect and image are correct.
    When I change the line from:

    @ QImage image = this->m_image; @

    to (for example):

    @ QImage image = QImage("whatever.jpg"); @

    or when i comment the line:

    @ this->m_image = image; @

    It doesn't crashes.
    If i replace:

    @ this->update(); @

    with:

    @ this->repaint(); @

    In certain conditions it crashes, and in other works.

    So, I'm very confused, and no idea on how to solve this, ¿any ideas please?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hipersayan_x
      wrote on last edited by
      #2

      Ok, based in "this":https://qt-project.org/doc/qt-4.8/qimage.html#QImage-6:

      bq. The buffer must remain valid throughout the life of the QImage. The image does not delete the buffer at destruction.

      I has been deleting the QImage pixel buffer before rendering it with drawImage().
      A quick solution could be (not the best way), change the line:

      @
      void ImageDisplay::setImage(const QImage &image)
      {
      this->m_image = image;

      this->update();
      

      }
      @

      to:

      @
      void ImageDisplay::setImage(const QImage &image)
      {
      this->m_image = image.copy();

      this->update();
      

      }
      @

      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