Qt Forum

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

    Can't draw a tilemap on QWidget

    Mobile and Embedded
    5
    7
    3287
    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.
    • B
      Beder last edited by

      Hi, I'm starting to create apps for S60v5 using Qt.
      I have a problem with drawing tiles on QWidget. I'm drawing them in a loop in paint event. If I test it on simulator, it works, but on phone I get black screen. Png file (137x308) with tiles is loaded to a Qpixmap object. Basing on a two-dimensional array i chose witch tile to draw.

      Here is my code:
      @
      void Plansza::paintEvent(QPaintEvent ){
      int x,y,i=0,j=0;
      QPainter painter(this);
      painter.setPen(Qt::black);
      QRect sourcea(1,1,16, 16);
      QRect sourceb(18,1,16, 16);
      QRect source(1,1,16, 16);
      if(Xoff>0){
      Xoff=-32;
      if(Xv>0) Xv--;
      }
      else if(Xoff<-64){
      Xoff=-32;
      if(Xv<99) Xv++;
      }
      if(Yoff>0){
      Yoff=-32;
      if(Yv>0) Yv--;
      }
      else if(Yoff<-64){
      Yoff=-32;
      if(Yv<99) Yv++;
      }
      for(x=Xv;x<22+Xv;x++){
      for(y=Yv;y<13+Yv;y++){
      QRect target(32
      i+Xoff,32*j+Yoff,32,32);
      if(mapa[x][y]->tile==1) source=sourcea;
      if(mapa[x][y]->tile==0) source=sourceb;
      painter.drawPixmap(target,tileMap,source);
      j++;
      }
      j=0;
      i++;
      }
      i=j=0;
      }
      @
      So, my question is why am I getting a black screen?

      1 Reply Last reply Reply Quote 0
      • D
        dalaing last edited by

        What happens if you add
        @painter.end();@
        to the end of the paintEvent(...) method?

        David Laing
        Location API team
        Qt Mobility

        1 Reply Last reply Reply Quote 0
        • G
          giesbert last edited by

          This is done automatically in the destructor of QPainter (as it should be for objects :-) ):

          @
          QPainter::~QPainter()
          {
          d_ptr->inDestructor = true;
          QT_TRY {
          if (isActive())
          end();
          else if (d_ptr->refcount > 1)
          d_ptr->detachPainterPrivate(this);
          } QT_CATCH(...) {
          // don't throw anything in the destructor.
          }
          if (d_ptr) {
          ...
          }
          }
          @

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply Reply Quote 0
          • D
            dalaing last edited by

            Good call. Something like did help with a superficially similar problem someone was having in IRC, but I think they were dealing with a longer lived QPainter :)

            David Laing
            Location API team
            Qt Mobility

            1 Reply Last reply Reply Quote 0
            • A
              andre last edited by

              I find the code itself hard to read. Lots of unexplained abbreviations, and you use values that don't come from the method itself, so there is no way to see for us if you have initialized those properly.

              What I would start doing, is checking if the result of all your calculations is what you expect it to be. Just insert some qDebug() statements, for instance for outputting the target QRect to see if that is actually inside the widget itself.

              1 Reply Last reply Reply Quote 0
              • S
                shiroki last edited by

                I would guess the image is not loaded(found) at all.

                blog: http://www.cuteqt.com/blog
                bbs: http://www.cuteqt.com/bbs

                1 Reply Last reply Reply Quote 0
                • B
                  Beder last edited by

                  I found what was wrong. I haven't added my resources to *.pro file.

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