PDA

View Full Version : rotate an image



saman_artorious
18th March 2013, 11:27
I have two images, one inside another. first I used two QFrame s and set background image via stylesheets.
but this was not a good idea as QFrame has no pixmap() member.
I changed QFrame to QLabel n set the background image of the inner image via stylesheets. (only the inner image shall rotate).

Now, I used the following code to try rotating QLabel background image, though I get segmentation fault, when assignig QLabel pixmap to the Qpixmap variable.



QPixmap pixmap(*ui->label_11->pixmap());
QMatrix rm;
rm.rotate(90);
pixmap = pixmap.transformed(rm);
ui->label_11->setPixmap(pixmap);

wysota
18th March 2013, 19:02
QWidget::paintEvent()
QPainter::rotate()
QPainter::drawPixmap()
QPropertyAnimation

Esther
21st March 2013, 09:56
Im not a professional programmer, but there is something wrong with your code, check this image ratating sample code (http://www.rasteredge.com/how-to/csharp-imaging/rotate-image/) and have a comparison to check,hope this can help you.

saman_artorious
25th March 2013, 09:45
as I said I have one QLabel inside a QFrame. the label background image is a small movable part of the general frame background image. what I want is to rotate the smaller image according to a particular angle.



void MainWindow::rotate()
{
QRectF target(0.0, 0.0, ui->label->width(), ui->label->height());
QRectF source(0.0, 0.0, ui->label->width(), ui->label->height());
QPixmap pixmap("path-to-image");

QPainter painter(this);
painter.rotate(90.0);

painter.drawPixmap(target, pixmap, source);

}



I do not know how to link the pixmap to the label background after rotation!

d_stranz
25th March 2013, 19:58
What's wrong with QLabel::setPixmap()? Seems like the obvious thing to use after reading the QLabel documentation.

saman_artorious
26th March 2013, 05:02
Although I could rotate the inner image with the code below, the sad point is that, the background image always fit inside the inner QLabel. But, what I need is to rotate both the QLabel and the image simultaneously. or it will be better not to use QLabel at all this way. can I only load an extra image inside the outer frame background image and try to rotate it? I need help please.



QPixmap *orig_pixmap = new QPixmap();
orig_pixmap->load("path-to-png");
degrees++;
if(degrees == 90) degrees = 0;
QTransform rotate_disc;
rotate_disc.translate((orig_pixmap->width()-ui->label->width())/2 , (orig_pixmap->width()-ui->label->height())/2);
rotate_disc.rotate(degrees,Qt::ZAxis);
QPixmap pixmap;
//pixmap = orig_disc->scaled(89,89,Qt::IgnoreAspectRatio,Qt::SmoothTrans formation);
pixmap = orig_pixmap->transformed(rotate_disc,Qt::SmoothTransformation);
ui->label->setPixmap(pixmap);

wysota
26th March 2013, 06:36
#include <QtGui>

class Widget : public QWidget {
Q_OBJECT
Q_PROPERTY(int rot1 READ rot1 WRITE setRot1);
Q_PROPERTY(int rot2 READ rot2 WRITE setRot2);
public:
Widget(QWidget *parent = 0) : QWidget(parent) { m_rot1 = 0; m_rot2 = 0; }
void setPixmap1(QPixmap px) { m_px1 = px; update(); }
void setPixmap2(QPixmap px) { m_px2 = px; update(); }
int rot1() const { return m_rot1; }
int rot2() const { return m_rot2; }
public slots:
void setRot1(int r1) { m_rot1 = r1; update(); }
void setRot2(int r2) { m_rot2 = r2; update(); }
protected:
void paintEvent(QPaintEvent *) {
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
p.translate(width()/2, height()/2);
if(!m_px1.isNull()) {
p.save();
p.rotate(m_rot1);
QRect r = m_px1.rect();
r.moveCenter(QPoint(0,0));
p.drawPixmap(r, m_px1);
p.restore();
}
if(!m_px2.isNull()) {
p.save();
p.rotate(m_rot2);
QRect r = m_px2.rect();
r.moveCenter(QPoint(0,0));
p.drawPixmap(r, m_px2);
p.restore();
}
}
private:
int m_rot1;
int m_rot2;
QPixmap m_px1, m_px2;
};

#include "main.moc"

int main(int argc, char **argv) {
QApplication app(argc, argv);
Widget w;
w.setPixmap1(QPixmap("/usr/share/icons/default.kde4/64x64/actions/system-lock-screen.png"));
w.setPixmap2(QPixmap("/usr/share/icons/default.kde4/64x64/actions/application-exit.png"));
w.resize(100,100);
QPropertyAnimation anim1(&w, "rot1");
QPropertyAnimation anim2(&w, "rot2");
anim1.setStartValue(0);
anim2.setStartValue(0);
anim1.setEndValue(359);
anim2.setEndValue(-359);
anim1.setLoopCount(-1);
anim2.setLoopCount(-1);
anim1.setDuration(5000);
anim2.setDuration(3000);
anim1.start();
anim2.start();
w.show();
return app.exec();
}

saman_artorious
26th March 2013, 20:49
There is something I need to know. As in my case I have a MainWindow of so many tools n widgets. would it be all right if I inherit from QWidget publicly in MainWindow class?
that way I can load as much anim n pixmaps as I need in the project main window. The main window paintEvent would be customized then. is that ok?

saman_artorious
29th May 2013, 14:24
Wysota
The the rotation axis of the second image is its center. However, I may want to set one side of the rectangle fixed and or even rotate it from a certain point. Is that possible?

wysota
29th May 2013, 18:05
Yes, of course. You can manipulate the painter in any way you want using rotate(), scale() and translate().