Question

What the easiest way to draw an image as a background for a QGraphicsRectItem? I can set the background as follows but I can't scale the image:

 QGraphicsRectItem *enemyItem;

 QImage *image = new QImage(":/PaperMario.png");

 QBrush *brush = new QBrush(*image);


 enemyItem = new QGraphicsRectItem();
 enemyItem->setBrush(*brush);

 enemyItem->setRect(enemy->getXPos()*30,enemy->getYPos()*30,30,30);
 scene->addItem(enemyItem);

inside QGraphicsRectItem

void MySquare::setBrush(QColor _color){
    color = _color;
    color_pressed = _color;
    update(); //repaint
}

I have tried it using:

QBrush *brush = new QBrush(*image->scaled(10));

But I'm stuck: doesn't want to compile. Is this the way to do it?

Edit:

    QImage *image = new QImage(":/PaperMario.png");
    QImage *scaled_image = new QImage(image->scaled(35,35,Qt::KeepAspectRatio));
    QBrush *brush = new QBrush(*scaled_image);

Does does draw it but it still shows the image multiple times in the drawing enter image description here

Was it helpful?

Solution

when you display your image in a scene (what i would guess) why dont you use QGraphicsScene::addPixmap(QPixmap) and use the QPixmap::fromImage ( const QImage & image, Qt::ImageConversionFlags flags = Qt::AutoColor ) to get your image into the right format.

OTHER TIPS

*image->scaled(10) does not work on a QImage*. Either you write *image.scaled or (better) image->scaled.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top