I'm new to Qt and I'm having some problem with QWidget rotation.
I have a QPixmap inside a QLabel.
What I want is to animate it with a continuous rotation by 90 degrees.
I know QPropertyAnimation and I know how to use it, but I'm struggling with How to use it for rotating a QWidget. Is there any simple way to use achieve my goal and rotate the entire QLabel or the QPixmap inside it with an animation?
Thank you.
This is the demo for rotation of QLabel/QPixmap with animation.
it's not necessary to use QPropertyAnimation. Because there is no rotate property for QLabel or QPixmap. So used QVariantAnimation make QPixmap rotate as animation and use QPixmap::transformed to rotate it. If you want well to control the animation of the pixmap, highly recommend QGraphicsPixmapItem with QPropertyAnimation
class RotateMe : public QLabel {
Q_OBJECT
public:
explicit RotateMe(QWidget* parent = Q_NULLPTR) :
QLabel(parent),
pixmap(100, 100),
animation(new QVariantAnimation )
{
resize(200, 200);
pixmap.fill(Qt::red);
animation->setDuration(10000);
animation->setStartValue(0.0f);
animation->setEndValue(90.0f);
connect(animation, &QVariantAnimation::valueChanged, [=](const QVariant &value){
qDebug()<<value;
QTransform t;
t.rotate(value.toReal());
setPixmap(pixmap.transformed(t));
});
animation->start();
}
private:
QPixmap pixmap;
QVariantAnimation *animation;
};
You can implement the rotation in two ways:
1) Create a collection of static images each of which represents the original pixmap rotated by some angle. With a timer you can change your label's pixmap with one from your collection. This will imitate the animated rotation.
2) Use a single pixmap and override your label's QLabel::painEvent() where you should rotate the QPainter object with QPainter::rotate() function each time you redraw the label.
Related
I am creating simple tetris in wt and I inherited widget to create piece ( I put four pieces in game, four different classes ). I draw on paint event in every piece. How to rotate widget ?
Ican draw rotated image in painEvent function but I would rather rotate whole widget. Is this pissible in qt ?
You can't easily rotate any widget in Qt. But you can add your widget to QGraphicsScene, rotate it and show on QGraphicsView. Here is a short example how to do it:
QGraphicsScene *scene = new QGraphicsScene(this);
QPushButton *button = new QPushButton();
button->setText("My cool button");
QGraphicsProxyWidget *w = scene->addWidget(button);
w->setPos(50, 50);
w->setRotation(45);
ui->graphicsView->setScene(scene);
Alternatively you can rewrite all on QML. In QML you can rotate almost anything.
I'm working on a Qt application made with a main QGraphicsView.
This view can show and switch between differents QgraphicsScenes. This application needs to always have an overlay in front of each scenes, so the best way to do this overlay is by setForegroundBrush() method of QGraphicsView.
But my overlay is a tiled-image, where I could edit the opacity and the scale of the source image.
Here's the code written in my QGraphicsView class constructor :
QString imgPath("path/to/image.png");
QPixmap map(imgPath);
QPainter painter(this);
QRectF zone(0,0,map.width(),map.height());
painter.drawPixmap(zone,map,zone);
QBrush brush = painter.brush();
brush.setStyle(Qt::TexturePattern);
setForegroundBrush(brush);
But doesn't work, nothing is shown.
I tested a simple QBrush with a QPixmap and works fine, but I need to use QPainter to be able to edit the opacity of my image.
Finally I think the easiest way to have a tiled image in QGraphicsView foreground is by reimplmenting the drawForeground(QPainter *painter, const QRectF &rect).
void Frontend::drawForeground(QPainter *painter, const QRectF &rect){
float alpha = 0.15;
float scale = 2;
QString imgPath("path/to/image.png");
QPixmap img(imgPath);
painter->scale(scale,scale);
painter->setOpacity(alpha);
painter->drawTiledPixmap(rect,img);
}
You cannot paint on the widget outside of its paintEvent method. Perhaps you wanted to have the painter work on the pixmap (painter(&map)) instead of the widget (painter(this))?
You could also add an overlay by:
Painting it in the reimplemented paintEvent of your derived scene, making sure that you paint not on the scene, but on its viewport(). There are convenience methods that are called by the view's paintEvent, such as drawBackground and drawForeground.
Painting it in a generic QWidget overlay.
I have several answers that demonstrate how to get overlays over widgets in generaly, and also on scene views.
My problem is the following:
I have a QGraphicsScene, there is a QPixmap on it. I would like to rotate that pixmap around a center point, would work like a clock actually.
I've tried these:í
QPixmap pointer_pixmap("/home/peter/desktop/myimg2.png");
QTransform transform;
QGraphicsPixmapItem *pointer = new QGraphicsPixmapItem(pointer_pixmap);
pointer->setOffset(174,190);
pointer->setTransformOriginPoint(QPoint(174-pointer_pixmap.width(), 190-pointer_pixmap.height()));
transform.translate((174-pointer_pixmap.width())/2,(190-pointer_pixmap.height())/2);
transform.rotate(60);
transform.translate(-((174-pointer_pixmap.width())/2),-((190-pointer_pixmap.height())/2));
pointer_pixmap = pointer_pixmap.transformed(transform);
item->addItem(pointer);
pointer->setPixmap(pointer_pixmap);
It looks like the translation doesn't have any effect on my pixmap. Why's that?
You should rotate the QGraphicsPixmapItem, not the pixmap itself.
Use QGraphicsItem::setTransformOriginPoint to define the transform origin point and QGraphicsItem::setRotation to rotate the item.
I am using QGraphicsTextItem to paint the text on the scene. Text is painted along the path (QGraphicsPathItem), wich is parent of my QGraphicsTextItem - so the text rotation is changed to be along the path element and is sticked to it while zooming the view. But the font size of QGraphicsTextItem is also changing while zooming the view - this is what I am trying to avoid. Of I set QGraphicsItem::ItemIgnoresTransformations flag to the QGraphicsTextItem it stops rotating while it's parent (QGraphicsPathItem) does.
I do understand that I have to re-implement QGraphicsTextItem::paint function, but I am stuck with the coordination system. Here is the code (Label class inherits public QGraphicsTextItem):
void Label::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{
// Store current position and rotation
QPointF position = pos();
qreal angle = rotation();
// Store current transformation matrix
QTransform transform = painter->worldTransform();
// Reset painter transformation
painter->setTransform( QTransform() );
// Rotate painter to the stored angle
painter->rotate( angle );
// Draw the text
painter->drawText( mapToScene( position ), toPlainText() );
// Restore transformation matrix
painter->setTransform( transform );
}
The position (and rotation) of my text on the screen is unpredictable :(
What am I doing wrong? Thank you very much in advance.
I solved a problem this way - for drawing a line/circle/rectangle/path, which I want to be transformed, I use an appropriate QGraphicsLine/Ellipse/Rect/PathItem. For drawing the text (which I do NOT want to be transformed) I use QGraphicsSimpleTextItem. I set text's flag to ignore transormations and set it's parent to Line/Ellipse/Rect/Path item. The Line/Ellipse/Rect/Path item transforms, but text does not - this is what I wanted. I can also rotate text and set it's position.
Thank you very much for answers.
The following solution worked perfectly for me:
void MyDerivedQGraphicsItem::paint(QPainter *painter, const StyleOptionGraphicsItem *option, QWidget *widget)
{
double scaleValue = scale()/painter->transform().m11();
painter->save();
painter->scale(scaleValue, scaleValue);
painter->drawText(...);
painter->restore();
...
}
We can also multiply the scaleValue by other mesures we want to keep its size constant outside the save/restore environment.
QPointF ref(500, 500);
QPointF vector = scaleValue * QPointF(100, 100);
painter->drawLine(ref+vector, ref-vector);
I had this issue once. Instead of ignoring transformations, you need to scale down the items you don't want to be zoomed in in your zoom-in function.
When you zoom in, if you change the scale by ds for example, scale the items by 1.0 / ds
You might need to change their positions though.
I hope this helps.
Edit: I hope I understood the question right.
I'm trying to animate the change of a QPixmap, inside QLabel.
I have MainWindow which holds several objects that derive from QScrollArea. Each of these holds a QLabel member.
Using mousePressEvent() I am able to replace the picture of each QLabel using setPixmap(). However, that simply switches the image in each QLabel, while what I would like to achieve is an animation where a new image slides over the existing one.
First I tried using a QTimeLine to draw the QPixmap on the QLabel myself (I've created a class that derives from QLabel for that, and wrote my own setPixmap()) but that didn't work. Next I tried using QPropertyAnimation but it can't construct on a Pixmap without me implementing a sub class for that as well.
Any thoughts or ideas are appreciated.
You will need a QObject with a property that can be animated, and generates the intermediate frames for the animation. An incomplete example:
class LabelAnimator : public QObject
{
Q_OBJECT
Q_PROPERTY(float progress READ progress WRITE setProgress)
public:
LabelAnimator(QLabel* label) : mProgress(0.0f),
mLabel(label),
mAnimation(new QPropertyAnimation(this, "progress", this)
{
mAnimation->setStartValue(0.0f);
mAnimation->setEndValue(1.0f);
}
void setProgress(float progress) {
mProgress = progress;
QPixmap pix = mOriginalPixmap;
int offset = - mLabel->width() * (1.0f-progress);
QPainter painter(&pix);
painter.paint(off, 0, mNewPixmap);
painter.end();
mLabel->setPixmap(pix);
}
void setPixmap(const QPixmap& pix) {
mOriginalPixmap = mLabel->pixmap();
mNewPixmap = pix;
mAnimation->start();
}
};
QLabel was never designed for such uses. Draw your QPixmaps inside a QGraphicsView, it is far more focused towards rendering effects and animations.