Search code examples
c++qtqimageqlistwidget

Rotate QImage in QListWidget


Got a small app where in a QListWidget many QImage and text are displayed with this function:

void TileDisplay::DisplayTiles()
{
    for(int i = 0; i < m_tiles.size(); i++) {
        QListWidgetItem *item = new QListWidgetItem("Tile " + QString::number(i+1), ui->listWidget);
        item->setData(Qt::DecorationRole, m_tiles[i].scaled(64, 64, Qt::IgnoreAspectRatio, Qt::FastTransformation));
    }
}

I'm looking for a solution where the QImage is rotated around it's centre with this function:

void TileDisplay::RotateImage(int degree)
{
    if(GetTiles().size() > 0) {
        QImage *tileToRotate = GetCurrentTile();
        if(tileToRotate != nullptr) {
            QTransform rotate;
            rotate.rotate(degree);
            tileToRotate->transformed(rotate);
            DisplayTiles();
        }
    }
}

It is running without any issue, but the image is not rotated. Any help appreciated.


Solution

  • From the documentation the signature of the QImage::transformed member function is...

    QImage QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode) const;
    

    So you need to make use of the returned QImage. Try...

    *tileToRotate = tileToRotate->transformed(rotate);