Can't draw a tilemap on QWidget



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



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



  • 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) {
    ...
    }
    }
    @



  • 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 :)



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.