Pregunta

I’m just beggining to use Qt’s QTransform. I’m trying to translate a simple image but I can’t do it! In the form, I have a label named “rulo” and a button wich triggers the following code when clicked.

void MainWindow::on_pushButton_clicked()
{
    QImage canvas = QImage("/home/andresdido/Descargas/ruleta.png","PNM");

    // QImage canvas;
    QTransform translating;
    QImage trasladada;

    qreal ancho = (qreal) canvas.width()/2;
    qreal alto = (qreal) canvas.height()/2;

    translating.translate(ancho,alto);

    trasladada=canvas.transformed(translating);
    ui->rulo->setPixmap(QPixmap::fromImage(trasladada));

    return;

}

Both QImage’s, canvas and trasladada are just the same! Just as if the translation had no effect at all. (Obviously, both Qimage’s also look the same, since their “data” is perfectly equal).

I've seen there is an unanswered topic here which is, in a way, similar: Qt image move/rotation Any ideas about why the translation has no effect? Thanks!

¿Fue útil?

Solución

You can do the same using the following sample code:

QImage canvas = QImage("source.png");
QImage trasladada(canvas.width(), canvas.height(), QImage::Format_ARGB32);
trasladada.fill(Qt::white);

QPainter painter(&trasladada);
qreal ancho = canvas.width() / 2.0;
qreal alto = canvas.height() / 2.0;    
painter.drawImage(QPointF(ancho, alto), canvas);   

ui->rulo->setPixmap(QPixmap::fromImage(trasladada));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top