Qt QGraphicsView translate BackgroundBrush - qt

I have created a view class derived from QGraphicsView and set the backgroundBrush as an image. I want to translate the backgroundBrush. I have tried the following
// graphicsView derived from QGraphicsView
graphicsView->backgroundBrush().transform().translate(moveX, moveY);
But it is not transforming the background brush.

backgroundBrush() and transform() are defined as const member functions, meaning they don't modify the object on which they are called.
You need to call setBackgroundBrush() and setTransform() to modify these properties:
QBrush brush = graphicsView->backgroundBrush();
brush.setTransform(QTransform::fromTranslate(moveX, moveY));
graphicsView->setBackgroundBrush(brush);

Related

How to save the QPixmap of a label which consist of one more label above it which has a Qpixmap

I have inherited the QLabel to create my own class of QLabel, The code is below:
class myLabel : public QLabel
{
Q_OBJECT
public:
explicit myLabel(QWidget *parent=nullptr): QLabel(parent){ setFrameShape(QFrame::Box);}
QLabel insideLabel;
};
As you can see, My class has one QLabel member inside.
Now, What I have done is that I created the object of my class and assign a image to it using setPixmap() and also assign a image to my QLabel member inside my class. The code is below:
QFile file("file.png");
ui->setupUi(this);
//Creatig instance of my QLabel class and setting one image to it.
myLabel *label=new myLabel(this);
label->setPixmap(QPixmap("someImagePath"));
//Assining image to my class QLabel member.
label->insideLabel.setPixmap(QPixmap("someImagePath"));
//saving file.
label->pixmap()->save("imageName","png",-1);
So when I save the image it only save the image of my QLabel class object QPixmap instead of saving both of my label image as the insideLabel is present above the my label class object as it is it's member. How can i save both images of my labels as one image.
Your code is not trying to save a QLabel, you are trying to save the pixmap property of a QLabel, those are two very different things.
A QLabel is a widget that can be rendered on a paint device. You should read the documentation about the QWidget subsystem:
Qt Widgets module
I see some problems with your code, the inside widget has no parent, and in your case I think that is a problem, but explaining that is beyond your question.
If what you want is to render the widget into an image file, you could do something like this:
QPixmap pixmap(Widget->size());
Widget->render(&pixmap);
pixmap.save("widget_render_file.png");
If you want to compose an image you can directly draw on a painter attached to a pixmap like this:
QPixmap pixmap(100,100);
QPainter painter(pixmap);
painter.setPen (...);
painter.drawPixmap (...);
painter.end();
pixmap.save(...);
You should also check documentation on QPainter and QPixmap, there's a lot of useful drawing/painting functions.
I just saved the needed portion of my Widget window as image, below is my code
label=new myLabel(this);
label->setGeometry(50,50,300,300);
label->setPixmap(QPixmap("/home/vinay/Pictures/exp.png").scaled(label->width(),label->height()));
label->insideLabel=new QLabel(this);
label->insideLabel->setGeometry(50,50,50,50);
label->insideLabel->setPixmap(QPixmap("/home/vinay/Pictures/exp2.png"));
label->insideLabel->setFrameShape(QFrame::Box);
label->insideLabel->setLineWidth(3);
//Assining image to my class QLabel member.
//saving file.
label->pixmap()->save("imageName","png",-1);
//ui->myWidget->grab().save("image.png");
QRect r(50,50,300,300);
Widget::grab(r).save("image.png");

Tiled image in QGraphicsView foreground

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.

Animating a QRect with QPropertyAnimation

I have a bunch of QRects and some text in a QGraphicsScene that I am trying to animate with QPropertyAnimation. Animating the text works fine but the QRects don't work because they fail to convert to a QGraphicsObject
This works perfectly
QPropertyAnimation *a = new QPropertyAnimation(this);
a->setTargetObject(items[size.x()*size.y()-1-aa]->toGraphicsObject()); //text
a->setPropertyName("pos");
a->setDuration(animationLength);
a->setStartValue(items[size.x()*size.y()-1-aa]->pos());
a->setEndValue(newTextPos);
a->setEasingCurve(easingCurve);
a->start(QAbstractAnimation::DeleteWhenStopped);
But this doesn't, because items[2*size.x()*size.y()-2-aa]->toGraphicsObject() returns a null pointer.
QPropertyAnimation *a = new QPropertyAnimation(this);
a->setTargetObject(items[2*size.x()*size.y()-2-aa]->toGraphicsObject()); //rect
a->setPropertyName("pos");
a->setDuration(animationLength);
a->setStartValue(items[2*size.x()*size.y()-2-aa]->pos());
a->setEndValue(newRectPos);
a->setEasingCurve(easingCurve);
a->start(QAbstractAnimation::DeleteWhenStopped);
Is there a way to fix this?
toGraphicsObject returns null pointer because QGraphicsRectItem is not a QGraphicsObject. You cannot use QGraphicsRectItem to perform an animation. I can suggest two workarounds:
Create your own class derived from QObject and QGraphicsRectItem, create "pos" property and implement its getter and setter.
Create your own class derived from QGraphicsObject. Implement its boundingRect and paint pure virtual methods to make it paint a rectangle.

Animating image replacement in Qt

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.

Creating shader program for QGraphicsItem

I have a QGraphicsScene in which I add a QGraphicsItem. Inside the QGraphicsItem, I render the triangle used in the hello triangle example of OpenGL ES 2.0. The problem is that if I create and compile the shaders anyplace other than the QGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) function, they do not get compiled. I know that you should have a GL rendering context active but doesn't that happen when I set my viewport to a GLWidget?? I tried various things like compiling them in the QGraphicsItem constructor or in the QGraphicsScene and setting the QGraphicsItem as a parent (which I learned they are very stupid things to do), but (obviously) nothing works. What seemed most logical to me was to create a initShaders() function inside the QGraphicsItem class and call it after the item is created in my scene, but that didn't work also.
Create a derived class of QGraphicsView. Override the setupViewport(QWidget *viewport) to initialize the shaders. This will allow you to ensure the context is current when compiling the shaders. However, it requires that the items be added to the scene before setViewport() is called on the graphics view.
void MyGraphicsView::setupViewport(QWidget *viewport)
{
QGLWidget *glWidget = qobject_cast<QGLWidget*>(viewport);
if (glWidget) {
glWidget->makeCurrent();
foreach (QGraphicsItem *item, scene()->items())
{
MyGraphicsShaderItem *glItem = qgraphicsitem_cast<MyGraphicsShaderItem*>(item);
if (glItem)
glItem->initShader();
}
glWidget->doneCurrent();
}
}

Resources