程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Qt-widget畫坐標系以及一些功能,qt-widget畫坐標系

Qt-widget畫坐標系以及一些功能,qt-widget畫坐標系

編輯:C++入門知識

Qt-widget畫坐標系以及一些功能,qt-widget畫坐標系


窗口:

 

 

mywidget.h:

 1 #ifndef MYWIDGET_H
 2 #define MYWIDGET_H
 3 
 4 #include <QWidget>
 5 #include "ui_mywidget.h"
 6 #include <QPaintEvent>
 7 #include <QtGui>
 8 
 9 class MyWidget : public QWidget
10 {
11     Q_OBJECT
12 
13 public:
14     MyWidget(QWidget *parent = 0);
15     ~MyWidget();
16     QPointF ObjectPtoDisplayP(QPointF objPoint);
17     QPointF DisplayPtoObjectP(QPointF disPoint);
18     QPointF WidgetPtoObjectP(QPointF wigPoint);
19     QPointF ObjectPtoWidgetP(QPointF objPoint);
20     QPointF ValuePtoObjectP(QPointF valPoint);
21     QPointF ObjectPtoValueP(QPointF objPoint);
22     QPointF scaleIn(QPointF pos_before, QPointF scale_center, double scale_value);
23     QPointF scaleOut(QPointF pos_before, QPointF scale_center, double scale_value);
24 
25 protected:
26     void paintEvent(QPaintEvent *event);
27     void wheelEvent(QWheelEvent *event);
28     void mousePressEvent(QMouseEvent *event);
29     void mouseMoveEvent(QMouseEvent *event);
30 
31 private:
32     Ui::MyWidget ui;
33 
34     int margin_size;
35 
36     QPointF paint_org;              //widget坐標系
37     QSize paint_size_first;
38     QSize paint_size_old;
39     QSize paint_size_new;
40     QPointF paint_center_old;       //display坐標系
41     QPointF paint_center_new;       //display坐標系
42 
43     double offset_x;
44     double offset_y;
45 
46     QPointF rect_center;           //display坐標系
47     QPointF rect_topl;             //display坐標系
48     QPointF rect_bottomr;          //display坐標系
49 
50     double scale_value;
51 
52     QPointF mousepress_org;        //display坐標系
53 
54     QPointF axis_x_old;            //display坐標系
55     QPointF axis_y_old;            //display坐標系
56     double axis_scale;
57 
58     double offsetv_x;
59     double offsetv_y;
60     double pixel_per_mm;
61 
62     QPointF mouse_current_pos;      //object坐標系
63 };
64 
65 #endif // MYWIDGET_H

 

 

mywidget.cpp:

  1 #include "mywidget.h"
  2 
  3 MyWidget::MyWidget(QWidget *parent)
  4     : QWidget(parent)
  5 {
  6     ui.setupUi(this);
  7 
  8     margin_size = 35;
  9 
 10     paint_org = QPointF(margin_size + 20, margin_size - 20);      
 11     paint_size_first = QSize(this->width() - margin_size * 2, this->height() - margin_size * 2);
 12     paint_size_old = paint_size_first;
 13     paint_size_new = paint_size_first;
 14     paint_center_old = QPointF(paint_size_first.width() / 2, paint_size_first.height() / 2);   
 15     paint_center_new = paint_center_old;
 16 
 17     offset_x = 0;
 18     offset_y = paint_size_new.height();
 19 
 20     rect_center = QPointF(paint_size_new.width() / 2, paint_size_new.height() / 2);   
 21     rect_topl = QPointF(rect_center.rx() - (paint_size_new.height() / 2 - 15), rect_center.ry() + (paint_size_new.height() / 2 - 15));
 22     rect_bottomr = QPointF(rect_center.rx() + (paint_size_new.height() / 2 - 15), rect_center.ry() - (paint_size_new.height() / 2 - 15));
 23 
 24     scale_value = 1.05;
 25 
 26     mousepress_org = QPointF(0, 0);
 27 
 28     axis_x_old = QPointF(rect_center.rx(), 0);
 29     axis_y_old = QPointF(0, rect_center.ry());
 30 
 31     axis_scale = 20.0;
 32 
 33     offsetv_x = rect_center.rx();
 34     offsetv_y = rect_bottomr.ry();
 35     pixel_per_mm = (double)(rect_bottomr.rx() - rect_topl.rx()) / 600;
 36 
 37     mouse_current_pos = QPointF(rect_center.rx(), rect_center.ry());
 38     this->setMouseTracking(true);
 39 
 40     //this->resize(880, 640);
 41 }
 42 
 43 MyWidget::~MyWidget()
 44 {
 45 
 46 }
 47 
 48 void MyWidget::paintEvent(QPaintEvent *event)
 49 {
 50     paint_size_new = QSize(this->width() - margin_size * 2, this->height() - margin_size * 2);
 51 
 52     QImage image = QImage(paint_size_new, QImage::Format_RGB32);
 53     QColor backColor = qRgb(255, 255, 255);
 54     image.fill(backColor);
 55 
 56     if (paint_size_new != paint_size_old)
 57     {
 58         offset_x = 0;
 59         offset_y = paint_size_new.height();
 60 
 61         paint_center_new = QPointF(paint_size_new.width() / 2, paint_size_new.height() / 2);
 62         rect_center = paint_center_new - paint_center_old + rect_center;
 63         rect_topl = paint_center_new - paint_center_old + rect_topl;
 64         rect_bottomr = paint_center_new - paint_center_old + rect_bottomr;
 65 
 66         paint_size_old = paint_size_new;
 67         paint_center_old = paint_center_new;
 68     }
 69 
 70     offsetv_x = rect_center.rx();
 71     offsetv_y = rect_bottomr.ry();
 72     pixel_per_mm = (double)(rect_bottomr.rx() - rect_topl.rx()) / 600;
 73 
 74     QPainter painter2(&image);
 75     QRectF rec(DisplayPtoObjectP(rect_topl), DisplayPtoObjectP(rect_bottomr));
 76     painter2.drawRect(rec);
 77      painter2.drawPoint(DisplayPtoObjectP(rect_center));
 78     painter2.drawText(QPointF(mouse_current_pos.rx(), mouse_current_pos.ry()), "(" + QString::number(ObjectPtoValueP(mouse_current_pos).rx(), 'f', 1) + "mm, " + QString::number(ObjectPtoValueP(mouse_current_pos).ry(), 'f', 1) + "mm)");
 79     //painter2.drawPoint(ValuePtoObjectP(QPointF(-150.0, 150.0)));            //(-150mm, 150mm)
 80 
 81     QPainter painter(this);
 82      //painter.setRenderHint(QPainter::Antialiasing, true);
 83     painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(0, 0))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(image.width(), 0))));
 84     painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-0.01, 0))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-0.01, image.height()))));
 85 
 86     painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(0, 0))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(0, -10))));
 87     painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(0, 0))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-10, 0))));
 88     painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(image.width(), 0))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(image.width(), -10))));
 89     painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(0, image.height()))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-10, image.height()))));
 90     painter.drawText(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(0, -20))), QString::number((double)(0 - offsetv_x) / pixel_per_mm, 'f', 1));
 91     painter.drawText(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-30, 0))), QString::number((double)(0 - offsetv_y) / pixel_per_mm, 'f', 1));
 92     painter.drawText(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(image.width(), -20))), QString::number((double)(image.width() - offsetv_x) / pixel_per_mm, 'f', 1));
 93     painter.drawText(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-30, image.height()))), QString::number((double)(image.height() - offsetv_y) / pixel_per_mm, 'f', 1));
 94     painter.drawText(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(image.width() / 2, -35))), QString(tr("X [mm]")));
 95     painter.rotate(-90);
 96     painter.drawText(QPointF(- (image.height() / 2 + 20), 15), QString(tr("Z [mm]")));
 97     painter.rotate(90);
 98 
 99     if (rect_center.rx() > 0 && rect_center.rx() < image.width())
100     {
101         painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(rect_center.rx(), 0))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(rect_center.rx(), -10))));
102         painter.drawText(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(rect_center.rx(), -20))), QString::number((double)(rect_center.rx() - offsetv_x) / pixel_per_mm, 'f', 1));
103     }
104     
105     if (rect_center.ry() > 0 && rect_center.ry() < image.height())
106     {
107         painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(0, rect_center.ry()))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-10, rect_center.ry()))));
108         painter.drawText(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-30, rect_center.ry()))), QString::number((double)(rect_center.ry() - offsetv_y) / pixel_per_mm, 'f', 1));
109     }
110 
111     if (axis_scale >= 35.0)
112     {
113         axis_scale = axis_scale / 2;
114     }
115     else if (axis_scale <= 15.0)
116     {
117         axis_scale = axis_scale * 2;
118     }
119 
120     axis_x_old = QPointF(rect_center.rx(),0);
121     int i = 0;
122     while (axis_x_old.rx() > axis_scale)
123     {
124         axis_x_old.rx() = axis_x_old.rx() - axis_scale;
125         i++;
126         if (axis_x_old.rx() > 0 && axis_x_old.rx() < image.width())
127         {
128             if (i % 5 == 0)
129             {
130                 painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(axis_x_old.rx(), 0))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(axis_x_old.rx(), -10))));
131                 painter.drawText(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(axis_x_old.rx(), -20))), QString::number((double)(axis_x_old.rx() - offsetv_x) / pixel_per_mm, 'f', 1));
132             }
133             else
134             {
135                 painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(axis_x_old.rx(), 0))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(axis_x_old.rx(), -5))));
136             }
137         }
138     }
139     axis_x_old = QPointF(rect_center.rx(), 0);
140     i = 0;
141     while ((image.width() - axis_x_old.rx()) > axis_scale)
142     {
143         axis_x_old.rx() = axis_x_old.rx() + axis_scale;
144         i++;
145         if (axis_x_old.rx() > 0 && axis_x_old.rx() < image.width())
146         {
147             if (i % 5 == 0)
148             {
149                 painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(axis_x_old.rx(), 0))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(axis_x_old.rx(), -10))));
150                 painter.drawText(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(axis_x_old.rx(), -20))), QString::number((double)(axis_x_old.rx() - offsetv_x) / pixel_per_mm, 'f', 1));
151             }
152             else
153             {
154                 painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(axis_x_old.rx(), 0))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(axis_x_old.rx(), -5))));
155             }
156         }
157     }
158 
159     axis_y_old = QPointF(0, rect_center.ry());
160     i = 0;
161     while (axis_y_old.ry() > axis_scale)
162     {
163         axis_y_old.ry() = axis_y_old.ry() - axis_scale;
164         i++;
165         if (axis_y_old.ry() > 0 && axis_y_old.ry() < image.height())
166         {
167             if (i % 5 == 0)
168             {
169                 painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(0, axis_y_old.ry()))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-10, axis_y_old.ry()))));
170                 painter.drawText(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-30, axis_y_old.ry()))), QString::number((double)(axis_y_old.ry() - offsetv_y) / pixel_per_mm, 'f', 1));
171             }
172             else
173             {
174                 painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(0, axis_y_old.ry()))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-5, axis_y_old.ry()))));
175             }
176         }
177     }
178     axis_y_old = QPointF(0, rect_center.ry());
179     i = 0;
180     while ((image.height() - axis_y_old.ry()) > axis_scale)
181     {
182         axis_y_old.ry() = axis_y_old.ry() + axis_scale;
183         i++;
184         if (axis_y_old.ry() > 0 && axis_y_old.ry() < image.height())
185         {
186             if (i % 5 == 0)
187             {
188                 painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(0, axis_y_old.ry()))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-10, axis_y_old.ry()))));
189                 painter.drawText(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-30, axis_y_old.ry()))), QString::number((double)(axis_y_old.ry() - offsetv_y) / pixel_per_mm, 'f', 1));
190             }
191             else
192             {
193                 painter.drawLine(ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(0, axis_y_old.ry()))), ObjectPtoWidgetP(DisplayPtoObjectP(QPointF(-5, axis_y_old.ry()))));
194             }
195         }
196     }
197 
198     painter.drawImage(paint_org, image);
199 }
200 
201 void MyWidget::wheelEvent(QWheelEvent *event)
202 {
203      QPointF mousepos = WidgetPtoObjectP(event->pos());
204      if (event->delta() > 0)    
205      {
206         rect_center = scaleIn(rect_center, ObjectPtoDisplayP(mousepos), scale_value);
207         rect_topl = scaleIn(rect_topl, ObjectPtoDisplayP(mousepos), scale_value);
208         rect_bottomr = scaleIn(rect_bottomr, ObjectPtoDisplayP(mousepos), scale_value);
209         axis_scale = axis_scale * scale_value;
210      }
211      else
212      {
213         rect_center = scaleOut(rect_center, ObjectPtoDisplayP(mousepos), scale_value);
214         rect_topl = scaleOut(rect_topl, ObjectPtoDisplayP(mousepos), scale_value);
215         rect_bottomr = scaleOut(rect_bottomr, ObjectPtoDisplayP(mousepos), scale_value);
216         axis_scale = axis_scale / scale_value;
217       }
218       
219        repaint();
220 }
221 
222 void MyWidget::mouseMoveEvent(QMouseEvent *event)
223 {
224     mouse_current_pos = WidgetPtoObjectP(event->pos());
225 
226     if (event->buttons() & Qt::LeftButton)
227     {
228         QPointF mousepos_move = ObjectPtoDisplayP(WidgetPtoObjectP(event->pos()));
229         
230         rect_center = mousepos_move - mousepress_org + rect_center;
231         rect_topl = mousepos_move - mousepress_org + rect_topl;
232         rect_bottomr = mousepos_move - mousepress_org + rect_bottomr;
233         
234         mousepress_org = mousepos_move;
235     }
236 
237     repaint();
238 }
239 
240 void MyWidget::mousePressEvent(QMouseEvent *event)
241 {
242     if (event->button() == Qt::LeftButton)
243     {
244         mousepress_org = ObjectPtoDisplayP(WidgetPtoObjectP(event->pos()));
245     }
246 }
247 
248 QPointF MyWidget::ObjectPtoDisplayP(QPointF objPoint)
249 {
250     return QPointF(objPoint.rx() - offset_x, -objPoint.ry() + offset_y);
251 }
252 
253 QPointF MyWidget::DisplayPtoObjectP(QPointF disPoint)
254 {
255     return QPointF(disPoint.rx() + offset_x, -disPoint.ry() + offset_y);
256 }
257 
258 QPointF MyWidget::scaleIn(QPointF pos_before, QPointF scale_center, double scale_value)
259 {
260     QPointF temp;
261     temp.rx() = (double)(pos_before.rx() - scale_center.rx()) * scale_value + scale_center.rx();
262     temp.ry() = (double)(pos_before.ry() - scale_center.ry()) * scale_value + scale_center.ry();
263     return temp;
264 }
265 
266 QPointF MyWidget::scaleOut(QPointF pos_before, QPointF scale_center, double scale_value)
267 {
268     QPointF temp;
269     temp.rx() = (double)(pos_before.rx() - scale_center.rx()) / scale_value + scale_center.rx();
270     temp.ry() = (double)(pos_before.ry() - scale_center.ry()) / scale_value + scale_center.ry();
271     return temp;
272 }
273 
274 QPointF MyWidget::WidgetPtoObjectP(QPointF wigPoint)
275 {
276     return QPointF(wigPoint - paint_org);
277 }
278 
279 QPointF MyWidget::ObjectPtoWidgetP(QPointF objPoint)
280 {
281     return QPointF(objPoint + paint_org);
282 }
283 
284 QPointF MyWidget::ValuePtoObjectP(QPointF valPoint)
285 {
286     return DisplayPtoObjectP(QPointF(valPoint.rx() * pixel_per_mm + offsetv_x, valPoint.ry() * pixel_per_mm + offsetv_y));
287 }
288 
289 QPointF MyWidget::ObjectPtoValueP(QPointF objPoint)
290 {
291     return QPointF((double)(ObjectPtoDisplayP(objPoint).rx() - offsetv_x) / pixel_per_mm, (double)(ObjectPtoDisplayP(objPoint).ry() - offsetv_y) / pixel_per_mm);
292 }

 

 

代碼搞得有點復雜,用於顯示三維輪廓測量的深度輪廓。

特點:
1、顯示鼠標點坐標值

2、矩形框坐標值固定

  ①以鼠標點為中心滾輪等比例縮放,矩形框隨之縮放,坐標軸跟著縮放(減少和插入中間值)

  ②鼠標左擊拖動白板視窗,矩形框隨之移動,坐標值跟著移動

 3、窗口縮放,坐標系整體不縮放

 

有一個問題,Z軸的0.0值在縮放之後會變成-0.0,暫時未找到原因。

因為是第一次寫,思路可能不是很好,有更好的方法的話,可以探討。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved