Qt-widget画坐标系以及一些功能
2018-06-17 23:41:50来源:未知 阅读 ()
窗口:
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,暂时未找到原因。
因为是第一次写,思路可能不是很好,有更好的方法的话,可以探讨。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- C++ 类 2020-06-02
- C++临时变量的回顾思考以及librdkafka设置回调函数注意点 2019-09-17
- 剑指offer25:复杂链表(每个节点中有节点值,以及两个指针 2019-08-27
- Z算法 2019-08-16
- 对快速排序的理解以及相关c++代码 2019-08-16
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash