Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved Program crashes on QPainter::drawPixmap()

    General and Desktop
    3
    5
    1122
    Loading More Posts
    • 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.
    • J
      j_omega last edited by A Former User

      I am having problem where my Qt program crashes after calling QPainter::drawPixmap(). I have spent 2 days debugging this and have decided that I must be unintentionally abusing some feature of Qt.

      A working example of this problem can be found here.

      My code consists of a QML file that updates the following properteries:

      Q_PROPERTY(qreal longitude READ getLongitude WRITE setLongitude NOTIFY latitudeChanged)
      Q_PROPERTY(qreal latitude READ getLatitude WRITE setLatitude NOTIFY latitudeChanged)
      
      void Map::setLongitude(qreal longitude)
      {
          double diff = (this->longitude - this->pixmapCenter.longitude()) * scale;
          this->longitude = longitude;
      
          if (qFabs(diff) > 50)
          {
              MapTile result = updatePixmap(scale, longitude, latitude);
              pixmap = result.pixmap;
              pixmapCenter = result.center;
          }
          update();
      }
      
      void Map::setLatitude(qreal latitude)
      {
          this->latitude = latitude;
      }
      

      That in turn regenerates a new Pixmap

      MapTile updatePixmap(double scale, double longitude, double latitude)
      {
      
          QPixmap myPixmap(800, 400);
          myPixmap.fill(Qt::transparent);
          QPainter painter(&myPixmap);
          painter.translate(400, 240);
          QPen pen;
      
          pen.setColor(Qt::white);
          pen.setWidth(1);
          painter.setPen(pen);
          QRectF boundaries(QPointF(-91.55 , 41.55) * scale,
                           QPointF(-91.45, 41.45) * scale);
          boundaries.translate(-longitude * scale, -latitude * scale);
          painter.drawRect(boundaries);
          painter.end();
      
          QGeoCoordinate center(latitude, longitude);
          return MapTile(myPixmap, center);
      }
      

      This new pixmap is then drawn on the screen at the appropriate location. It is important to note that the program runs fine for a few seconds before it crashes.

      It crashes with a segfault error in qdrawhelper_sse2.cpp line 587.

      void Map::paint(QPainter *painter)
      {
          painter->translate(boundingRect().width()/2, boundingRect().height()/2);
          painter->scale(1, -1);
      
          painter->translate((pixmapCenter.longitude() - longitude) * scale,
                             (pixmapCenter.latitude() - latitude) * scale);
          QPoint corner(-pixmap.width()/2, -pixmap.height()/2);
      
          painter->drawPixmap(corner, this->pixmap);
      
      }
      

      Here is an image of the moment of the crash

      enter image description here

      enter image description here

      1 Reply Last reply Reply Quote 0
      • J
        j_omega last edited by

        It turns out this is a bug. See here.

        1 Reply Last reply Reply Quote 2
        • V
          VRonin last edited by

          What's in line 42 of Map::paint?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          raven-worx 1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @VRonin last edited by

            @VRonin said in Qt program crashes on QPainter::drawPixmap():

            What's in line 42 of Map::paint?

            https://github.com/kyle7119/stackoverflow_question/blob/master/map.cpp#L42

            @j_omega
            You debug window says "Source file is more recent than executable"
            Can you try a full rebuild (clean & build)

            --- 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

            J 1 Reply Last reply Reply Quote 1
            • J
              j_omega @raven-worx last edited by j_omega

              @raven-worx I saw that earlier and already tried the a clean/rebuild. I even deleted the entire project and cloned the repo again.

              I just noticed that this does not happen in release configuration. I have submitted a Qt bug report.

              My environment is MinGW 5.3.0 on Windows 7 (or 10)

              1 Reply Last reply Reply Quote 0
              • J
                j_omega last edited by

                It turns out this is a bug. See here.

                1 Reply Last reply Reply Quote 2
                • First post
                  Last post