Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Can't draw a tilemap on QWidget

Can't draw a tilemap on QWidget

Scheduled Pinned Locked Moved Mobile and Embedded
7 Posts 5 Posters 3.7k Views
  • 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 Offline
    B Offline
    Beder
    wrote on last edited by
    #1

    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
    0
    • D Offline
      D Offline
      dalaing
      wrote on last edited by
      #2

      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
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        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
        0
        • D Offline
          D Offline
          dalaing
          wrote on last edited by
          #4

          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
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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
            0
            • S Offline
              S Offline
              shiroki
              wrote on last edited by
              #6

              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
              0
              • B Offline
                B Offline
                Beder
                wrote on last edited by
                #7

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

                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