用QT實現淡入淡出的效果
/* baseinfo.h
* 孤舟釣客
* 2011-07-21
*/
#ifndef BASEINFO_H
#define BASEINFO_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include <QComboBox>
class BaseInfo : public QWidget
{
Q_OBJECT
public:
BaseInfo(QWidget *parent = 0, Qt::WindowFlags fl = 0);
QLabel * label1;
QLabel * label2;
QLabel * label3;
QLabel * label4;
QLabel * label5;
QLabel * label6;
QLabel * label7;
QLabel * labelOther;
QLabel * labelIcon;
QLineEdit * lineEditUser;
QLineEdit * lineEditName;
QComboBox * comboBoxSex;
QTextEdit * textEditDepartment;
QLineEdit * lineEditAge;
QTextEdit * textEditDisc;
QPushButton * pushButtonIcon;
};
#endif // BASEINFO_H
/* baseinfo.cpp
* 孤舟釣客
* 2011-07-21
*/
#include "baseinfo.h"
#include <QLayout>
#include <QTextCodec>
#include <QPixmap>
BaseInfo::BaseInfo(QWidget *parent, Qt::WindowFlags fl)
: QWidget(parent, fl)
{
// create
label1 = new QLabel(tr("User Name:"));
label2 = new QLabel(tr("Name:"));
label3 = new QLabel(tr("Sex:"));
label4 = new QLabel(tr("Dempartment:"));
label5 = new QLabel(tr("Age:"));
labelOther = new QLabel(tr("Other:"));
labelOther->setFrameStyle(QFrame::Panel|QFrame::Sunken);
label6 = new QLabel(tr("Discription:"));
label7 = new QLabel(tr("Icon:"));
labelIcon = new QLabel();
QPixmap icon("icon.png");
labelIcon->resize(icon.width(), icon.height());
labelIcon->setPixmap(icon);
pushButtonIcon = new QPushButton();
pushButtonIcon->setText(tr("Change"));
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->setSpacing(20);
hLayout->addWidget(label7);
hLayout->addWidget(labelIcon);
hLayout->addWidget(pushButtonIcon);
lineEditUser = new QLineEdit();
lineEditName = new QLineEdit();
comboBoxSex = new QComboBox();
comboBoxSex->insertItem(0, tr("Female"));
comboBoxSex->insertItem(1, tr("Male"));
textEditDepartment = new QTextEdit();
lineEditAge = new QLineEdit();
textEditDisc = new QTextEdit();
// Lay out
// left layout -- is a grid layout
QGridLayout * leftLayout = new QGridLayout();
int labelCol = 0;
int contentCol = 1;
leftLayout->addWidget(label1, 0, labelCol); // user name row
leftLayout->addWidget(lineEditUser, 0, contentCol);
leftLayout->addWidget(label2, 1, labelCol); // name row
leftLayout->addWidget(lineEditName,1,contentCol);
leftLayout->addWidget(label3,2,labelCol); // sex row
leftLayout->addWidget(comboBoxSex, 2, contentCol);
leftLayout->addWidget(label4, 3, labelCol, Qt::AlignTop); // department row
leftLayout->addWidget(textEditDepartment, 3, contentCol);
leftLayout->addWidget(label5, 4, labelCol); // age row
leftLayout->addWidget(lineEditAge, 4, contentCol);
leftLayout->addWidget(labelOther, 5, labelCol, 1, 2); // other
leftLayout->setColumnStretch(0, 1);
leftLayout->setColumnStretch(1, 3);
leftLayout->setRowStretch(0, 1);
leftLayout->setRowStretch(3, 3);
// right layout -- is a vBoxLayout
QVBoxLayout *rightLayout = new QVBoxLayout();
rightLayout->setMargin(10);
rightLayout->addLayout(hLayout);
rightLayout->addWidget(label6);
rightLayout->addWidget(textEditDisc);
// main layout -- is a hBoxLayout
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->setMargin(15);
mainLayout->setSpacing(10);
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
}
/* contact.h
* 孤舟釣客
* 2011-07-21
*/
#ifndef CONTACT_H
#define CONTACT_H
#include <QDialog>
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QSpacerItem;
class QLabel;
class QLineEdit;
class QCheckBox;
class Contact : public QWidget
{
Q_OBJECT
public:
Contact( QWidget* parent = 0, Qt::WindowFlags fl = 0 );
~Contact();
QLabel* textLabel1;
QLabel* textLabel2;
QLabel* textLabel3;
QLabel* textLabel4;
QLabel* textLabel5;
QLineEdit* lineEditEmail;
QLineEdit* lineEditAddress;
QCheckBox* checkBoxMsg;
QLineEdit* lineEditMobile;
QLineEdit* lineEditPhone;
QLineEdit* lineEditZip;
};
#endif // CONTACT_H
/* contact.cpp
* 孤舟釣客
* 2011-07-21
*/
#include "contact.h"
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QLayout>
#include <QToolTip>
#include <QWhatsThis>
#include <QLayout>
#include <QTextCodec>
Contact::Contact(QWidget* parent, Qt::WindowFlags fl)
: QWidget(parent, fl)
{
textLabel1 = new QLabel(tr("email:"));
textLabel2 = new QLabel(tr("address:"));
textLabel3 = new QLabel(tr("zip code:"));
textLabel4 = new QLabel(tr("mobile:"));
textLabel5 = new QLabel(tr("office:"));
lineEditEmail = new QLineEdit();
lineEditAddress = new QLineEdit();
checkBoxMsg = new QCheckBox(tr("receive msg"));
lineEditMobile = new QLineEdit();
lineEditPhone = new QLineEdit();
lineEditZip = new QLineEdit();
QGridLayout *grid = new QGridLayout(this);
grid->setMargin(20);
grid->setSpacing(10);
int labelCol = 0;
int contCol = 1;
int otherCol = 2;
grid->addWidget(textLabel1, 0, labelCol);
grid->addWidget(lineEditEmail, 0, contCol);
grid->addWidget(textLabel2, 1, labelCol);
grid->addWidget(lineEditAddress, 1, contCol);
grid->addWidget(textLabel3, 2, labelCol);
grid->addWidget(lineEditZip, 2, contCol);
grid->addWidget(textLabel4, 3, labelCol);
grid->addWidget(lineEditMobile, 3, contCol);
grid->addWidget(checkBoxMsg, 3, otherCol);
grid->addWidget(textLabel5, 4, labelCol);
grid->addWidget(lineEditPhone, 4, contCol);
grid->setColumnStretch(0, 10);
grid->setColumnStretch(1, 30);
grid->setColumnStretch(2, 10);
}
/*
* Destroys the object and frees any allocated resources
*/
Contact::~Contact()
{
// no need to delete child widgets, Qt does it all for us
}
/* detail.h
* 孤舟釣客
* 2011-07-21
*/
#ifndef DETAIL_H
#define DETAIL_H
#include <QLabel>
#include <QLineEdit>
#include <QTextEdit>
#include <QComboBox>
class Detail : public QWidget
{
Q_OBJECT
public:
Detail(QWidget *parent = 0, Qt::WindowFlags f1 = 0);
QLabel *label1;
QLabel *label2;
QLabel *label3;
QLabel *label4;
QComboBox *comboBoxCountry;
QComboBox *comboBoxProvince;
QLineEdit *lineeditCity;
QTextEdit *texteditPriv;
};
#endif // DETAIL_H
/* detail.cpp
* 孤舟釣客
* 2011-07-21
*/
#include <QTextCodec>
#include <QLayout>
#include "detail.h"
Detail::Detail(QWidget *parent, Qt::WindowFlags f1)
: QWidget(parent,f1)
{
label1 = new QLabel(tr("Country/Zone:"));
label2 = new QLabel(tr("Province:"));
label3 = new QLabel(tr("City:"));
label4 = new QLabel(tr("Discription:"));
comboBoxCountry = new QComboBox();
comboBoxCountry->insertItem(0, tr("China"));
comboBoxProvince = new QComboBox();
comboBoxProvince->insertItem(0, tr("JiangSu"));
lineeditCity = new QLineEdit();
texteditPriv = new QTextEdit();
QGridLayout *mainLayout = new QGridLayout(this);
int labelCol = 0;
int contCol = 1;
mainLayout->addWidget(label1, 0, labelCol);
mainLayout->addWidget(comboBoxCountry, 0, contCol);
mainLayout->addWidget(label2, 1, labelCol);
mainLayout->addWidget(comboBoxProvince, 1, contCol);
mainLayout->addWidget(label3, 2, labelCol);
mainLayout->addWidget(lineeditCity, 2, contCol);
mainLayout->addWidget(label4, 3, labelCol, Qt::AlignTop);
mainLayout->addWidget(texteditPriv, 3, contCol);
mainLayout->setColumnStretch(0, 10);
mainLayout->setColumnStretch(1, 30);
mainLayout->setMargin(20);
mainLayout->setSpacing(25);
}
/* fader.h
* 孤舟釣客
* 2011-07-21
*/
#ifndef FADER_H
#define FADER_H
#include <QWidget>
class QColor;
class QTimer;
class FaderWidget : public QWidget
{
Q_OBJECT
public:
FaderWidget(QWidget *parent);
void start();
protected:
void paintEvent(QPaintEvent *event);
private:
QTimer *timer;
int currentAlpha;
QColor startColor;
int duration;
};
#endif // FADER_H
/* fader.cpp
* 孤舟釣客
* 2011-07-21
*/
#include "fader.h"
#include <QtGui>
FaderWidget::FaderWidget(QWidget *parent)
: QWidget(parent)
{
if (parent)
startColor = parent->palette().window().color();
else
startColor = Qt::white;
currentAlpha = 0;
duration = 1000;
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
setAttribute(Qt::WA_DeleteOnClose);
resize(parent->size());
}
void FaderWidget::start()
{
currentAlpha = 255;
timer->start(100);
show();
}
void FaderWidget::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
QColor semiTransparentColor = startColor;
semiTransparentColor.setAlpha(currentAlpha);
painter.fillRect(rect(), semiTransparentColor);
currentAlpha -= 255 * timer->interval() / duration;
if (currentAlpha <= 0)
{
timer->stop();
close();
}
}
/* configdialog.h
* 孤舟釣客
* 2011-07-21
*/
#ifndef CONFIGDIALOG_H
#define CONFIGDIALOG_H
#include <QDialog>
#include <QPointer>
#include "fader.h"
class QListWidget;
class QListWidgetItem;
class QStackedWidget;
class ConfigDialog : public QDialog
{
Q_OBJECT
public:
ConfigDialog();
public slots:
void changePage(QListWidgetItem *current, QListWidgetItem *previous);
private:
QListWidget *contentsWidget;
QStackedWidget *pagesWidget;
QPointer<FaderWidget> faderWidget;
private slots:
void fadeInWidget(int index);
};
#endif // CONFIGDIALOG_H
/* configdialog.cpp
* 孤舟釣客
* 2011-07-21
*/
#include <QtGui>
#include "configdialog.h"
#include "baseinfo.h"
#include "detail.h"
#include "contact.h"
ConfigDialog::ConfigDialog()
{
contentsWidget = new QListWidget;
contentsWidget->addItem(tr("base info"));
contentsWidget->addItem(tr("contact"));
contentsWidget->addItem(tr("detail"));
contentsWidget->setMaximumWidth(128);
connect(contentsWidget,
SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
this, SLOT(changePage(QListWidgetItem *, QListWidgetItem*)));
pagesWidget = new QStackedWidget;
pagesWidget->addWidget(new BaseInfo);
pagesWidget->addWidget(new Contact);
pagesWidget->addWidget(new Detail);
connect(pagesWidget, SIGNAL(currentChanged(int)), this, SLOT(fadeInWidget(int)));
QPushButton *closeButton = new QPushButton(tr("Close"));
contentsWidget->setCurrentRow(0);
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
QHBoxLayout *horizontalLayout = new QHBoxLayout;
horizontalLayout->addWidget(contentsWidget);
horizontalLayout->addWidget(pagesWidget, 1);
QHBoxLayout *buttonsLayout = new QHBoxLayout;
buttonsLayout->addStretch(1);
buttonsLayout->addWidget(closeButton);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(horizontalLayout);
mainLayout->addStretch(1);
mainLayout->addSpacing(12);
mainLayout->addLayout(buttonsLayout);
setLayout(mainLayout);
setWindowTitle(tr("Fade in fade out example"));
faderWidget = new FaderWidget(this);
faderWidget->start();
}
void ConfigDialog::changePage(QListWidgetItem *current, QListWidgetItem *previous)
{
if (!current)
current = previous;
pagesWidget->setCurrentIndex(contentsWidget->row(current));
}
void ConfigDialog::fadeInWidget(int index)
{
if (faderWidget)
faderWidget->close();
faderWidget = new FaderWidget(pagesWidget->widget(index));
faderWidget->start();
}
/* main.cpp
* 孤舟釣客
* 2011-07-21
*/
#include <QApplication>
#include <QTranslator>
#include "configdialog.h"
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(configdialog);
QApplication app(argc, argv);
QFont f("AR PL KaitiM GB",12);
app.setFont(f);
QTranslator translator;
translator.load("fader_zh");
app.installTranslator(&translator);
ConfigDialog dialog;
return dialog.exec();
}