PDA

View Full Version : QwtPlotTextLabel and animations



Maximus2
8th January 2014, 15:39
Hi,

I would like to implement a fade in/fade out animation on a QwtPlotTextLabel.
Since QwtPlotTextLabel is not a QObject, I haven't figured yet how to make this possible.

Here is a screenshot of what I have now (https://www.dropbox.com/s/ije9dugf2mbvyy4/graphMsg.png) (message in middle on the graph, still need to do the fade out animation)

Maybe i'll have to use a QLabel or a QWidget instead of QwtPlotTextLabel? If so, I'm not sure how to place a widget on top of the QwtPlot that is transparent..
Thanks in advance!!

Maximus2
8th January 2014, 19:31
So far i'm using a QLabel instead of QwtPlotTextLabel.
It's working with this code, just have to figure how to put the QLabel on top of the QwtPlot now


#include "faderqlabel.h"

#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
#include <QTimer>


FaderQLabel::FaderQLabel(QWidget *parent, int timeFadeIn, int timeFadeOut, int fadeOutAfter) : QLabel(parent) {


this->timeFadeIn = timeFadeIn;
this->timeFadeOut = timeFadeOut;

QTimer *fadeOutTimer = new QTimer(this);
connect(fadeOutTimer, SIGNAL(timeout()), this, SLOT(fadeOut()));
fadeOutTimer->start(fadeOutAfter);

}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// SLOT FADE_OUT
void FaderQLabel::fadeOut() {

QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
this->setGraphicsEffect(effect);
QPropertyAnimation *anim = new QPropertyAnimation(this);
anim->setPropertyName("opacity");
anim->setTargetObject(effect);
anim->setDuration(timeFadeOut);
anim->setStartValue(1.0);
anim->setEndValue(0.0);
anim->setEasingCurve(QEasingCurve::OutQuad);
anim->start(QAbstractAnimation::DeleteWhenStopped);
}

Maximus2
9th January 2014, 01:44
Just to plot my solution.

I made a QWidget containing a QGridLayout
the QGridLayout contain a QLabel and a QwtPlot Item.
that way, I can put the QLabel on top of the QwtPlotItem and use the animation effect

Added after 1 16 minutes:

Turn out QwtPlot canvas use a QWidget.. I put my QLabel directly there for less code



///////////////////////////////////////////////////////////////////////////////

QLabel *labelMsg = new QLabel(this);
labelMsg->setText("TEST MSG ICI");
labelMsg->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
labelMsg->setStyleSheet("background-color : rgba(1,1,1,128);");


QWidget *ptnWidget = this->canvas();

QGridLayout *gridLayout;
gridLayout = new QGridLayout(this);
gridLayout->addWidget(labelMsg, 0, 0, 1, 1);

ptnWidget->setLayout(gridLayout);


QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(labelMsg);
labelMsg->setGraphicsEffect(effect);
QPropertyAnimation *anim = new QPropertyAnimation(labelMsg);
anim->setPropertyName("opacity");
anim->setTargetObject(effect);
anim->setDuration(3000);
anim->setStartValue(1.0);
anim->setEndValue(0.0);
anim->setEasingCurve(QEasingCurve::OutQuad);
anim->start(QAbstractAnimation::DeleteWhenStopped);

/////////////////////////////////////////////////////////////////////////////