设计模式(5)-装饰模式(Decorator) (二)

2014-11-24 11:48:39 · 作者: · 浏览: 6
void installIM();
};

#endif // QLINEEDITWITHIM_H

qlineeditwithim.cpp

[html]
#include "qlineeditwithim.h"

QLineEditWithIM::QLineEditWithIM(QLineEdit *lineEdit)
{
//#ifdef Q_WS_QWS
im = new InputMethod;
this->lineEdit = lineEdit;
//#endif
}

QLineEditWithIM::~QLineEditWithIM()
{
delete im;
}

void QLineEditWithIM::installIM()
{
//#ifdef Q_WS_QWS
installEventFilter(im);
connect(im->keyboard,SIGNAL(setvalue(QString)),this,SLOT(setText(QString)));
//#endif
}
#include "qlineeditwithim.h"

QLineEditWithIM::QLineEditWithIM(QLineEdit *lineEdit)
{
//#ifdef Q_WS_QWS
im = new InputMethod;
this->lineEdit = lineEdit;
//#endif
}

QLineEditWithIM::~QLineEditWithIM()
{
delete im;
}

void QLineEditWithIM::installIM()
{
//#ifdef Q_WS_QWS
installEventFilter(im);
connect(im->keyboard,SIGNAL(setvalue(QString)),this,SLOT(setText(QString)));
//#endif
}

login.h

[html]
#ifndef LOGIN_H
#define LOGIN_H

#include
#include "qlineeditwithim.h"

class QLabel;
class QLineEdit;
class QDialogButtonBox;

class QLogin : public QDialog
{
Q_OBJECT

public:
QLogin();
~QLogin();

public:

QLabel *managerLabel;
QLabel *passwdLabel;

QLineEditWithIM *managerEdit;
QLineEditWithIM *passwdEdit;

QPushButton *okButton;
QPushButton *cancelButton;
QDialogButtonBox *buttonBox;

signals:
void Authorize();

private slots:
void login();
void cancel();

};

#endif // LOGIN_H
#ifndef LOGIN_H
#define LOGIN_H

#include
#include "qlineeditwithim.h"

class QLabel;
class QLineEdit;
class QDialogButtonBox;

class QLogin : public QDialog
{
Q_OBJECT

public:
QLogin();
~QLogin();

public:

QLabel *managerLabel;
QLabel *passwdLabel;

QLineEditWithIM *managerEdit;
QLineEditWithIM *passwdEdit;

QPushButton *okButton;
QPushButton *cancelButton;
QDialogButtonBox *buttonBox;

signals:
void Authorize();

private slots:
void login();
void cancel();

};

#endif // LOGIN_H

login.cpp

[html]
#include
#include "login.h"

QLogin::QLogin()
{
managerLabel = new QLabel(tr("&Manager:"));
QLineEdit *_managerEdit = new QLineEdit;
managerEdit = new QLineEditWithIM(_managerEdit);
managerEdit->installIM();
managerLabel->setBuddy(managerEdit);

passwdLabel = new QLabel(tr("&Passwd:"));
QLineEdit *_passwdEdit = new QLineEdit;
passwdEdit = new QLineEditWithIM(_passwdEdit);
passwdEdit->installIM();
passwdEdit->setEchoMode(QLineEdit::Password);
passwdLabel->setBuddy(passwdEdit);

okButton = new QPushButton(tr("&Login"));
cancelButton = new QPushButton("&Cancel");

okButton->setDefault(true);

buttonBox = new QDialogButtonBox;
buttonBox->addButton(okButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(cancelButton, QDialogButtonBox::AcceptRole);

connect(okButton, SIGNAL(clicked()), this, SLOT(login()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));

QHBoxLayout *topLayout = new QHBoxLayout;
topLayout->addWidget(managerLabel);
topLayout->addWidget(managerEdit);

QHBoxLayout *midLayout = new QHBoxLayout;
midLayout->addWidget(passwdLabel);
midLayout->addWidget(passwdEdit);

QVBoxLayout *mai