[Qt-interest] QT paint overload for GraphicsItem not working correctly

Cole, Derek dcole at integrity-apps.com
Sat Jun 11 23:25:39 CEST 2011


Hello,

I am trying to overload the QGraphicsItem paint() method for putting my own pixmap data in for the purposes of creating tiles. Here is the paint method.

void ImagePixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
    qDebug("This paint was called");
    if(tileItem==NULL)
    {
        qDebug("Loading Pixmap");
        int nXSize = this->nBlockXSize;
        int nYSize = this->nBlockYSize;
        qDebug("nXSize: %i ", nXSize);
        qDebug("nYSize: %i ", nYSize);
        qDebug("tilePosX: %i ", this->tilePosX);
        qDebug("tilePosY: %i ", this->tilePosY);
        float * floatData = (float *) CPLMalloc(sizeof(float)*nXSize*nYSize);
        if(floatData == NULL)
            qDebug("FloatData was not allocated");


        poBand->RasterIO( GF_Read, this->tilePosX*nXSize, this->tilePosY*nYSize, nXSize, nYSize, floatData, nXSize, nYSize,GDT_Float32, 0, 0 );
        float two_eight =  pow(2.0,8);
        float scaler = pow(2.0,A_BPP);
        for (int i = 0 ; i < nYSize ; i++)
        {
             for (int j = 0 ; j < nXSize ; j++)
             {
                    floatData[i*nXSize+j] = (two_eight* floatData[i*nXSize+j])/scaler;
             }
         }
//code to dump to file the contents of this tile
            char tmp[128];
                sprintf(tmp, "FLOATDATA%i.csv", this->tilePosX*this->nBlocksY + this->tilePosY);
            FILE *file = fopen(tmp, "wb");
            for(size_t i = 0 ; i < nYSize ; i++)
            {
                for(size_t j = 0 ; j < nXSize ; j++)
                {
                    sprintf(tmp, "%4.3f,", floatData[i*nXSize+j]);
                    fwrite(tmp, strlen(tmp), 1, file);
                }
                sprintf(tmp, "\n");
                fwrite(tmp, strlen(tmp), 1, file);
            }
            fclose(file);
///

        QImage *qi = new QImage(nXSize, nYSize, QImage::Format_RGB32);
            for (int i = 0 ; i < nYSize ; i++)
            {
                 for (int j = 0 ; j < nXSize ; j++)
                 {
                        qi->setPixel(j,i,qRgb((unsigned char)floatData[i*nXSize+j],(unsigned char)floatData[i*nXSize+j],(unsigned char)floatData[i*nXSize+j]));
                 }
             }

        QPixmap p(QPixmap::fromImage(*qi,Qt::AutoColor));


        tileItem=new QGraphicsPixmapItem;
        tileItem->setPixmap(p);
    }
    tileItem->paint(painter,option,widget);
}

This is not displaying correctly for a single tile(im only testing wiith one tile, trying to read in the upper left corner of an image). I put in that dump to csv so  I could verify that the data is being read correctly, and it is, so I think my problem is with the lower half of my code. Here is how I am creating the tiles, from my main:

    for(int i =0; i <1; i++)
    {
        for(int j = 0; j < 1; j++)
        {
            QGraphicsItem *ipi = new ImagePixmapItem(poBand, nXBlocks, nYBlocks, i, j, nXBlockSize, nYBlockSize, A_BPP);
            ipi->setPos((i*nXBlockSize), (j*nYBlockSize));
            scene.addItem(ipi);

        }
    }
    ui->graphicsView->setScene(&scene);
    ui->graphicsView->show();

and here is the constructor of my ImagePixmapItem

//create addition constructor that does not take a pixmap, but has all the other info we need for creating a tile.
ImagePixmapItem::ImagePixmapItem(GDALRasterBand *band, int nBlocksX, int nBlocksY, int xoffset, int yoffset, int nXBlockSize, int nYBlockSize, int A_BPP, QGraphicsItem *parentItem)
{
    this->poBand = band;
    this->nBlocksX = nBlocksX;
    this->nBlocksY = nBlocksY;
    this->nBlockXSize = nXBlockSize;
    this->nBlockYSize = nYBlockSize;
    this->tilePosX =xoffset;
    this->tilePosY = yoffset;
    this->A_BPP = A_BPP;
    setCacheMode(NoCache);
    setFlag(QGraphicsItem::ItemIsMovable,true);
    setActive(true);
    this->setAcceptHoverEvents(true);
    this->setScaleFactor(1);
    this->setTransformationMode(Qt::FastTransformation);
    this->setFlag(QGraphicsItem::ItemIsFocusable);
    this->tileItem = NULL;

}


Does anyone see anything wrong why it will not display my image in the QGraphicsView? I kind of suspect maybe it is a scope issue - thoughts?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110611/c459289d/attachment.html 


More information about the Qt-interest-old mailing list