Search code examples
qtpaintqpixmap

how to set QPixmap as brush for rectangle properly?


i want to set a QPixmap as brush for a rectangle in paint method .I do this work but it doesn't set accurately .I test many value for width and height but yet it wasn't good .Overall, what sizes should I set for the rectangle surrounding the photo that put in it properly?

void set_coin::paint(QPainter *painter, const QStyleOptionGraphicsItem  *option,)
{
QRectF rec(x_size,y_size,10,30);
QPixmap coin(":pictures/image/coin.jpg");
coin=coin.scaled(10,30);
painter->setBrush(QBrush(coin));
painter->setPen(Qt::NoPen);
//painter->drawRect(rec);
painter->drawRoundedRect(rec,10,10);
}
QRectF set_coin::boundingRect() const
{
  return QRectF(x_size-10,y_size-10,20,40);
}

i want it What is shown in program(i put 4 coin in my scene)


Solution

  • The problem is here:

    QRectF rec(x_size,y_size,10,30);

    The doc says:

    QRectF::QRectF ( qreal x, qreal y, qreal width, qreal height )

    The last two arguments are x_size and y_size, not the first two one ;)

    EDIT: (Because I didn't really understand the problem first)

    I tried your code and in fact, your image is not "accuracy" because you see 4 pieces of mage on your small 10 to 30 area.

    The solution is to set x_size (respectively y_size) a value which is a multiple of 10 (respectively 30).

    If you try x_size = 100 and y_size = 300, it will works ;)