search-demo 二.鼠标事件

  鼠标点击,拖拽事件的追加,现在可以手动对方格染色。

  图片:

 

main.cpp:

C++语言: Codee#26073
01 #include <QtGui>
02 #include <QtCore>
03 #include "pathfinding.h"
04
05 int main(int argc, char *argv[])
06 {
07     QApplication a(argc, argv);
08     Widget* window=new Widget;
09
10     QRect frect = window->frameGeometry();
11     frect.moveCenter(QDesktopWidget().availableGeometry().center());
12     window->move(frect.topLeft());
13     window->show();
14
15     return a.exec();
16 }

pathfinding.h:

 

C++语言: Codee#26074
01 #ifndef PATHFINDING_H
02 #define PATHFINDING_H
03
04 #include <QtGui>
05 #include <QtCore>
06 #include <QTime>
07
08 class Widget : public QWidget
09 {
10     Q_OBJECT
11
12 public:
13     Widget(QWidget* parent = 0);
14     void paintEvent(QPaintEvent* event);
15     void mousePressEvent(QMouseEvent* event);
16     void mouseMoveEvent(QMouseEvent* event);
17     void setWindowPixel(const QPoint& pos, bool opaque);
18
19 private:
20     enum {LENGTH = 910};
21     enum {WIDTH = 700};
22     enum {BLOCKSIZE = 35};
23     bool blocks[LENGTH/BLOCKSIZE][WIDTH/BLOCKSIZE];
24     //QTime time;
25     QPoint cursorpos;
26 };
27
28 #endif // PATHFINDING_H

 

pathfinding.cpp:

 

C++语言: Codee#26075
01 #include "pathfinding.h"
02
03 Widget::Widget(QWidget* parent)
04     : QWidget(parent)
05 {
06     for (int i = 0; i < LENGTH / BLOCKSIZE; ++i)
07         for (int j = 0; j < WIDTH / BLOCKSIZE; ++j)
08             blocks[i][j] = true;
09     setAttribute(Qt::WA_StaticContents);
10     setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
11     this->setMouseTracking(true);
12     this->setFixedSize(LENGTH, WIDTH);
13 }
14
15 void Widget::paintEvent(QPaintEvent* event)
16 {
17     QPainter painter(this);
18
19
20     QColor color;
21     for (int i = 0; i < LENGTH / BLOCKSIZE; ++i)
22         for (int j = 0; j < WIDTH / BLOCKSIZE; ++j)
23         {
24             if (!blocks[i][j])
25                 color = Qt::gray;
26             else
27                 color = Qt::white;
28             painter.fillRect(QRect(BLOCKSIZE * i, BLOCKSIZE * j,
29                                    BLOCKSIZE, BLOCKSIZE), color);
30         }
31     painter.setPen(QPen(Qt::gray));
32
33     for (int i = 0; i < LENGTH; i += BLOCKSIZE)
34         painter.drawLine(i, 0,
35                          i, WIDTH);
36     for (int j = 0; j <= WIDTH; j += BLOCKSIZE)
37         painter.drawLine(0,  j,
38                          LENGTH, j);
39
40     painter.setFont(QFont("Arial", 50));
41     painter.drawText(rect(), Qt::AlignCenter, "Hello Qt!");
42 }
43
44 void Widget::mousePressEvent(QMouseEvent* event)
45 {
46     cursorpos = QPoint(event->pos().x() / BLOCKSIZE,
47                        event->pos().y() / BLOCKSIZE);
48     /*
49     if (event->buttons() & Qt::LeftButton)
50     */
51     setWindowPixel(event->pos(), true);
52 }
53
54 void Widget::mouseMoveEvent(QMouseEvent* event)
55 {
56     QTime curTime = QTime::currentTime();
57     QPoint tmppos = QPoint(event->pos().x() / BLOCKSIZE,
58                            event->pos().y() / BLOCKSIZE);
59     if (cursorpos != tmppos &&
60         event->buttons() & Qt::RightButton)
61     {
62         //qDebug("Time elapsed: %d ms", time.msecsTo(curTime));
63         setWindowPixel(event->pos(), true);
64     }
65     //cursorpos=tmppos;
66 }
67
68
69 void Widget::setWindowPixel(const QPoint& pos, bool opaque)
70 {
71     int x = pos.x() / BLOCKSIZE;
72     int y = pos.y() / BLOCKSIZE;
73     cursorpos = QPoint(x, y);
74     blocks[x][y] = !blocks[x][y];
75     update();
76 }
posted @ 2012-04-15 11:48  strorehouse  阅读(271)  评论(0编辑  收藏  举报