设计模式(1)-模板模式(Template) (二)

2014-11-24 11:48:37 · 作者: · 浏览: 1
ebug()<<"construct Draw2";
}

void Draw2::draw()
{
qDebug()<<"Draw2::draw()";
setMethod(getMethod());
}

QString Draw2::getMethod()
{
QString test = "Method::Draw2";
qDebug()<<"Draw2::getMethod()";
return test;
}

void Draw2::setMethod(QString method) const
{
qDebug()< }

main.cpp

[html] view plaincopyprint #include "drawtemplate.h"
#include "draw1.h"
#include "draw2.h"

int main(void)
{
Draw1 draw1;
draw1.draw();

Draw2 draw2;
draw2.draw();
}
#include "drawtemplate.h"
#include "draw1.h"
#include "draw2.h"

int main(void)
{
Draw1 draw1;
draw1.draw();

Draw2 draw2;
draw2.draw();
}

【运行结果】

[html] view plaincopyprint construct DrawTemplate
construct Draw1
Draw1::draw()
DrawTemplate::getMethod()
"Draw1::setMethod(Method::DrawTemplate)"
construct DrawTemplate
construct Draw2
Draw2::draw()
Draw2::getMethod()
"Draw2::setMethod(Method::Draw2)"
construct DrawTemplate
construct Draw1
Draw1::draw()
DrawTemplate::getMethod()
"Draw1::setMethod(Method::DrawTemplate)"
construct DrawTemplate
construct Draw2
Draw2::draw()
Draw2::getMethod()
"Draw2::setMethod(Method::Draw2)"

【实例剖析】

Qt输入法设计(嵌入式)一文中介绍了一种输入法的设计,在使用时,需要对控件对象的名称进行指定,并对每个QLineEdit对象绑定输入法对象。能否有一种方法,使得编辑框自带输入法对象?

下面利用模板模式,将QLineEdit作为模板,派生一个QLineEditWithIM,使得QLineEditWithIM自带输入法,使用就像QLineEdit一样简单。UML图如图2所示:

图2

【代码清单】

仅贴出改动的部分,省略了keyboard.h、keyboard.cpp代码。详请参考Qt输入法设计(嵌入式)

inputmethod.h

[html] view plaincopyprint #ifndef INPUTMETHOD_H
#define INPUTMETHOD_H

#include "keyboard.h"

class InputMethod : public KeyBoard
{
Q_OBJECT
public:
InputMethod();
~InputMethod();

bool eventFilter(QObject *obj, QEvent *event);

public:
KeyBoard *keyboard;

public:
void showKeyBoard();
};

#endif // INPUTMETHOD_H
#ifndef INPUTMETHOD_H
#define INPUTMETHOD_H

#include "keyboard.h"

class InputMethod : public KeyBoard
{
Q_OBJECT
public:
InputMethod();
~InputMethod();

bool eventFilter(QObject *obj, QEvent *event);

public:
KeyBoard *keyboard;

public:
void showKeyBoard();
};

#endif // INPUTMETHOD_H

inputmethod.cpp

[html] view plaincopyprint #include
#include "inputmethod.h"

InputMethod::InputMethod()
{
keyboard = new KeyBoard;
setWindowFlags(Qt::Tool|Qt::WindowStaysOnTopHint|Qt::FramelessWindowHint);
}

InputMethod::~InputMethod()
{
delete keyboard;
}

/*
* Name : void eventFilter(QObject *obj, QEvent *event);
* Type : QEvent
* Func : judge input method event
* In : QObject,QEvent
* Out : bool
*/
bool InputMethod::eventFilter(QObject *obj, QEvent *event)
{
if(event->type()==QEvent::MouseButtonPress)
{
showKeyBoard();
return true;
}

return QObject::eventFilter(obj,event);
}


/*
* Name : void showKeyBoard();
* Type : function
* Func : show keyBoard
* In : Null
* Out : Null
*/
void InputMethod::showKeyBoard()
{
keyboard->setWindowFlags(Qt::Tool|Qt::WindowStaysOnTopHint|Qt::FramelessWindowHint);
keyboard->move(50,120);
keyboard->exec();
}
#include
#include "inputmethod.h"

InputMethod::InputMethod()
{
keyboard = new KeyBoard;
setWindowFlags(Qt::Tool|Qt::WindowStaysOnTopHint|Qt::FramelessWindowHint);
}

InputMethod::~InputMethod()
{
delete keyboard;
}

/*
* Name : void eventFilter(QObject *obj, QEvent *event);
* Type : QEvent
* Func :