#ifndef GRAPHICSSTACKEDWIDGET_H
#define GRAPHICSSTACKEDWIDGET_H
#include <QStackedWidget>
class GraphicsStackedWidgetPrivate;
{
Q_OBJECT
Q_PROPERTY (int currentIndex READ currentIndex WRITE setCurrentIndex)
Q_PROPERTY (double windowOpacity READ windowOpacity WRITE setWindowOpacity DESIGNABLE isWindow)
private:
GraphicsStackedWidgetPrivate *d;
Q_DECLARE_PRIVATE(GraphicsStackedWidget)
public:
GraphicsStackedWidget
(QWidget *parent
= 0);
~GraphicsStackedWidget();
int currentIndex() const;
Q_INVOKABLE
int addWidget
(QWidget *w
);
public slots:
private slots:
void setCurrentIndex ( int index );
bool hidePage();
void showPage();
};
#endif // GRAPHICSSTACKEDWIDGET_H
-----------------------------------------------------------
#include "graphicsstackedwidget.h"
#include <QtGui>
#include <QGraphicsLinearLayout>
#define TRANSITION_TIME_MS 500
class GraphicsStackedWidgetPrivate
{
public:
int m_index;
GraphicsStackedWidgetPrivate() {
}
};
GraphicsStackedWidget
::GraphicsStackedWidget(QWidget *parent
): d(new GraphicsStackedWidgetPrivate)
{
d->m_index = 0;
}
GraphicsStackedWidget::~GraphicsStackedWidget()
{
delete d;
}
int GraphicsStackedWidget::currentIndex() const
{
}
void GraphicsStackedWidget::setCurrentIndex ( int index )
{
d->m_index = index;
if ( hidePage() ) {
QTimer::singleShot(TRANSITION_TIME_MS
/2,
this,
SLOT(showPage
()));
} else {
showPage();
}
}
int GraphicsStackedWidget
::addWidget(QWidget *w
) {
}
bool GraphicsStackedWidget::hidePage()
{
bool result = false;
if ( oldWidget != NULL ) {
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(oldWidget);
oldWidget->setGraphicsEffect(effect);
QPropertyAnimation *animation = new QPropertyAnimation(effect, "opacity", this);
animation->setDuration(TRANSITION_TIME_MS/2);
animation->setStartValue(1);
animation->setEndValue(0);
animation->start();
QTimer::singleShot(TRANSITION_TIME_MS
/2, oldWidget,
SLOT(repaint
()));
result = true;
}
return result;
}
void GraphicsStackedWidget::showPage()
{
if ( widget != NULL ) {
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(widget);
widget->setGraphicsEffect(effect);
QPropertyAnimation *animation = new QPropertyAnimation(effect, "opacity", this);
animation->setDuration(TRANSITION_TIME_MS/2);
animation->setStartValue(0);
animation->setEndValue(1);
animation->start();
QTimer::singleShot(TRANSITION_TIME_MS
/2, widget,
SLOT(repaint
()));
}
if ( widget != NULL ) {
widget->update();
}
}
#ifndef GRAPHICSSTACKEDWIDGET_H
#define GRAPHICSSTACKEDWIDGET_H
#include <QStackedWidget>
class GraphicsStackedWidgetPrivate;
class GraphicsStackedWidget : public QStackedWidget
{
Q_OBJECT
Q_PROPERTY (int currentIndex READ currentIndex WRITE setCurrentIndex)
Q_PROPERTY (double windowOpacity READ windowOpacity WRITE setWindowOpacity DESIGNABLE isWindow)
private:
GraphicsStackedWidgetPrivate *d;
Q_DECLARE_PRIVATE(GraphicsStackedWidget)
public:
GraphicsStackedWidget(QWidget *parent = 0);
~GraphicsStackedWidget();
int currentIndex() const;
Q_INVOKABLE int addWidget(QWidget *w);
public slots:
private slots:
void setCurrentIndex ( int index );
bool hidePage();
void showPage();
};
#endif // GRAPHICSSTACKEDWIDGET_H
-----------------------------------------------------------
#include "graphicsstackedwidget.h"
#include <QtGui>
#include <QGraphicsLinearLayout>
#define TRANSITION_TIME_MS 500
class GraphicsStackedWidgetPrivate
{
public:
int m_index;
GraphicsStackedWidgetPrivate() {
}
};
GraphicsStackedWidget::GraphicsStackedWidget(QWidget *parent):
QStackedWidget(parent),
d(new GraphicsStackedWidgetPrivate)
{
d->m_index = 0;
}
GraphicsStackedWidget::~GraphicsStackedWidget()
{
delete d;
}
int GraphicsStackedWidget::currentIndex() const
{
return QStackedWidget::currentIndex();
}
void GraphicsStackedWidget::setCurrentIndex ( int index )
{
d->m_index = index;
if ( hidePage() ) {
QTimer::singleShot(TRANSITION_TIME_MS/2, this, SLOT(showPage()));
} else {
showPage();
}
}
int GraphicsStackedWidget::addWidget(QWidget *w)
{
return QStackedWidget::addWidget(w);
}
bool GraphicsStackedWidget::hidePage()
{
bool result = false;
QWidget *oldWidget = QStackedWidget::widget(QStackedWidget::currentIndex());
if ( oldWidget != NULL ) {
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(oldWidget);
oldWidget->setGraphicsEffect(effect);
QPropertyAnimation *animation = new QPropertyAnimation(effect, "opacity", this);
animation->setDuration(TRANSITION_TIME_MS/2);
animation->setStartValue(1);
animation->setEndValue(0);
animation->start();
QTimer::singleShot(TRANSITION_TIME_MS/2, oldWidget, SLOT(repaint()));
result = true;
}
return result;
}
void GraphicsStackedWidget::showPage()
{
QWidget *widget = QStackedWidget::widget(d->m_index);
if ( widget != NULL ) {
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(widget);
widget->setGraphicsEffect(effect);
QPropertyAnimation *animation = new QPropertyAnimation(effect, "opacity", this);
animation->setDuration(TRANSITION_TIME_MS/2);
animation->setStartValue(0);
animation->setEndValue(1);
animation->start();
QTimer::singleShot(TRANSITION_TIME_MS/2, widget, SLOT(repaint()));
}
QStackedWidget::setCurrentIndex(d->m_index);
if ( widget != NULL ) {
widget->update();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks