Monday, September 28, 2009

Qt OpenGL in QGraphicsItem

I was working on trying to use OpenGL facilities in my graphics View ..As I am more into animations I was looking for QGraphicsItem to be on similar lines for OpenGL widget also.

The scan over the web always pushed me to use setViewPort on GraphicsView for OpenGL widget...but alas this was not tht was gonna help me...

Also Qt 4.6 follow ups made sure that I got to look at ideas of extending QGraphicsItem and QObject together to create my own needed OpenGL Graphics Item...

I will put here what I acheived from Qt 4.6 guys and am working on customising this more for OpenGL item support...


Here s the code snippet that can help having OpenGL widget as QGraphicsItem




OGMainWindow::OGMainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::OGMainWindow)
{
ui->setupUi(this);

ui->graphicsView->setScene(&scene);
GLWidget *glWidget= new GLWidget();


QImage image=QImage(":/img/auxpoint.png");

QGraphicsItem *gitem=new QGraphicsPixmapItem(QPixmap::fromImage(image));

scene.addItem(gitem);

//Custom Item can be similar to QGraphicsWidgetItem
CusotmItem *glItem = new CusotmItem(glWidget);
scene.addItem(glItem);

}

Sunday, September 27, 2009

Extending Qt Animation

Recently working with Qt Animation Framework I realised that though QVariantAnimation has property for setting easing curve to Custom type .

However this custom graph style doesnt really help the animation to follow graph of user s choice as the function pointer behind expects a definition like

qreal customFunc( qreal time) ;

where time t element of [0,1]  closed interval

also the return value is progress normalised between [0,1].

To support custom graph style that does have more parameters varying rather than just time one needs to extend QtVariantAnimation or QtPropertyAnimation.

I extended from QtPropertyAnimation for my CustomAnimation class something like..
class CustomAnimation : public QtPropertyAnimation

{

Q_OBJECT

public:

qreal a1Value() const;

void seta1Value(const qreal value);

...

QString style();

void setStyle(QString style);

CustomAnimation(QObject *parent = 0);

CustomAnimation(QObject *target, const QByteArray &propertyName, QObject *parent = 0);

~ CustomAnimation();

void updateCurrentValue(const qreal value);

void updateState(QtAbstractAnimation::State oldState, QtAbstractAnimation::State newState);

void updateCurrentTime(int msecs);

private:

qreal a1;

QString curveStyle;

Q_DISABLE_COPY(CustomAnimation);

};