QT与很多GUI库不同(如MFC),它不能随时随地地在界面上画图,只能在界面类的painterEvent中画图,如此一来,想在绘制QT界面时使用状态模式(GOF的23种设计模式之一)就有点困难了,作为解决方案,我先把要界面上的图片绘制在一张图片上(QPixmap),然后再在painterEvent中将Pixmap“画”到界面上。以下是这种方法的一个小例子。
截图:



源代码:
- #include <QtGui>
-
- class BasePen
- {
- protected:
-
-
- QPixmap m_Pixmap;
- QPoint m_StartPoint;
- QPoint m_EndPoint;
- virtual void SetPixmap() = 0;
- public:
- BasePen()
- {
- m_StartPoint = m_EndPoint = QPoint(0, 0);
- m_Pixmap = QPixmap(500, 500);
- }
- void SetStartPoint(QPoint point) { m_StartPoint = point; }
- void SetEndPoint(QPoint point)
- {
- m_EndPoint = point;
- SetPixmap();
- }
- QPixmap GetPixmap() { return m_Pixmap; }
- };
-
- class RectPen : public BasePen
- {
- protected:
- void SetPixmap()
- {
- m_Pixmap.fill(Qt::white);
- QPainter painter(&m_Pixmap);
- QRect rect(m_StartPoint, m_EndPoint);
- painter.setPen(Qt::red);
- painter.drawRect(rect);
- }
- };
-
- class LinePen : public BasePen
- {
- protected:
- void SetPixmap()
- {
- m_Pixmap.fill(Qt::white);
- QPainter painter(&m_Pixmap);
- painter.setPen(Qt::blue);
- painter.drawLine(m_StartPoint, m_EndPoint);
- }
- };
-
- class CirclePen : public BasePen
- {
- protected:
- void SetPixmap()
- {
- m_Pixmap.fill(Qt::white);
- QPainter painter(&m_Pixmap);
- QRect rect(m_StartPoint, m_EndPoint);
- painter.setPen(Qt::green);
- painter.drawEllipse(rect);
- }
- };
-
- class Widget : public QWidget
- {
- Q_OBJECT
- private:
- bool m_MouseDown;
- BasePen *m_BasePen;
- RectPen *m_RectPen;
- LinePen *m_LinePen;
- CirclePen *m_CirclePen;
-
- QRadioButton *m_LineButton;
- QRadioButton *m_RectButton;
- QRadioButton *m_CircleButton;
- protected:
- void mousePressEvent(QMouseEvent *event);
- void mouseMoveEvent(QMouseEvent *event);
- void mouseReleaseEvent(QMouseEvent *event);
- void paintEvent(QPaintEvent *event);
- public:
- Widget(QWidget *parent = 0);
- ~Widget();
- private slots:
- void ClickedLineButton() { m_BasePen = m_LinePen; }
- void ClickedRectButton() { m_BasePen = m_RectPen; }
- void ClickedCircleButton() { m_BasePen = m_CirclePen; }
- };
-
- Widget::Widget(QWidget *parent
- : QWidget(parent)
- {
- m_MouseDown = false;
- m_RectPen = new RectPen;
- m_LinePen = new LinePen;
- m_CirclePen = new CirclePen;
- m_LineButton = new QRadioButton("Line", this);
- m_RectButton = new QRadioButton("Rect", this);
- m_CircleButton = new QRadioButton("Circle", this);
- m_LineButton->move(10, 10);
- m_RectButton->move(100, 10);
- m_CircleButton->move(200, 10);
- connect(m_LineButton, SIGNAL(clicked()), this, SLOT(ClickedLineButton()));
- connect(m_RectButton, SIGNAL(clicked()), this, SLOT(ClickedRectButton()));
- connect(m_CircleButton, SIGNAL(clicked()), this, SLOT(ClickedCircleButton()));
- m_BasePen = m_LinePen;
- m_LineButton->setChecked(true);
- setFixedSize(500, 500);
- }
-
- Widget::~Widget()
- {
- delete m_LinePen;
- delete m_RectPen;
- delete m_CirclePen;
- }
-
- void Widget::mousePressEvent(QMouseEvent *event)
- {
- if( event->button() == Qt::LeftButton )
- {
- m_MouseDown = true;
- m_BasePen->SetStartPoint(event->pos());
- }
- }
-
- void Widget::mouseMoveEvent(QMouseEvent *event)
- {
- if( m_MouseDown )
- {
- m_BasePen->SetEndPoint(event->pos());
- update();
- }
- }
-
- void Widget::mouseReleaseEvent(QMouseEvent *event)
- {
- if( event->button() == Qt::LeftButton )
- {
- m_MouseDown = false;
- }
- }
-
- void Widget::paintEvent(QPaintEvent *event)
- {
- QPixmap temp = m_BasePen->GetPixmap();
- QPainter painter(this);
- painter.drawPixmap(0, 0, temp);
- }
-
- #include "main.moc"
-
- int main(int argc, char **argv)
- {
- QApplication app(argc, argv);
- Widget *ww = new Widget;
- ww->show();
- return app.exec();
- }
http://blog.csdn.net/small_qch/article/details/7632226
QT:使用“状态模式”绘制界面
原文:http://www.cnblogs.com/findumars/p/4993538.html