一、演示效果
二、关键程序
#include
#include
#include
#include
#include
#include
#include
#include
#include "qFlightInstruments.h"
QADI::QADI(QWidget *parent)
: QWidget(parent)
{
connect(this, SIGNAL(canvasReplot(void)), this, SLOT(canvasReplot_slot(void)));
m_sizeMin = 200;
m_sizeMax = 600;
m_offset = 2;
m_size = m_sizeMin - 2*m_offset;
setMinimumSize(m_sizeMin, m_sizeMin);
setMaximumSize(m_sizeMax, m_sizeMax);
resize(m_sizeMin, m_sizeMin);
setFocusPolicy(Qt::NoFocus);
m_roll = 0.0;
m_pitch = 0.0;
}
QADI::~QADI()
{
}
void QADI::canvasReplot_slot(void)
{
update();
}
void QADI::resizeEvent(QResizeEvent *event)
{
m_size = qMin(width(),height()) - 2*m_offset;
}
void QADI::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QBrush bgSky(QColor(48,172,220));
QBrush bgGround(QColor(247,168,21));
QPen whitePen(Qt::white);
QPen blackPen(Qt::black);
QPen pitchPen(Qt::white);
QPen pitchZero(Qt::green);
whitePen.setWidth(2);
blackPen.setWidth(2);
pitchZero.setWidth(3);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
painter.rotate(m_roll);
double pitch_tem = -m_pitch;
{
int y_min, y_max;
y_min = m_size/2*-40.0/45.0;
y_max = m_size/2* 40.0/45.0;
int y = m_size/2*pitch_tem/45.;
if( y < y_min ) y = y_min;
if( y > y_max ) y = y_max;
int x = sqrt(m_size*m_size/4 - y*y);
qreal gr = atan((double)(y)/x);
gr = gr * 180./3.1415926;
painter.setPen(blackPen);
painter.setBrush(bgSky);
painter.drawChord(-m_size/2, -m_size/2, m_size, m_size,
gr*16, (180-2*gr)*16);
painter.setBrush(bgGround);
painter.drawChord(-m_size/2, -m_size/2, m_size, m_size,
gr*16, -(180+2*gr)*16);
}
QRegion maskRegion(-m_size/2, -m_size/2, m_size, m_size, QRegion::Ellipse);
painter.setClipRegion(maskRegion);
{
int x, y, x1, y1;
int textWidth;
double p, r;
int ll = m_size/8, l;
int fontSize = 8;
QString s;
pitchPen.setWidth(2);
painter.setFont(QFont("", fontSize));
for(int i=-9; i<=9; i++) {
p = i*10;
s = QString("%1").arg(-p);
if( i % 3 == 0 )
l = ll;
else
l = ll/2;
if( i == 0 ) {
painter.setPen(pitchZero);
l = l * 1.8;
} else {
painter.setPen(pitchPen);
}
y = m_size/2*p/45.0 - m_size/2*pitch_tem/45.;
x = l;
r = sqrt(x*x + y*y);
if( r > m_size/2 ) continue;
painter.drawLine(QPointF(-l, 1.0*y), QPointF(l, 1.0*y));
textWidth = 100;
if( i % 3 == 0 && i != 0 ) {
painter.setPen(QPen(Qt::white));
x1 = -x-2-textWidth;
y1 = y - fontSize/2 - 1;
painter.drawText(QRectF(x1, y1, textWidth, fontSize+2),
Qt::AlignRight|Qt::AlignVCenter, s);
}
}
int markerSize = m_size/20;
float fx1, fy1, fx2, fy2, fx3, fy3;
painter.setBrush(QBrush(Qt::red));
painter.setPen(Qt::NoPen);
fx1 = markerSize;
fy1 = 0;
fx2 = fx1 + markerSize;
fy2 = -markerSize/2;
fx3 = fx1 + markerSize;
fy3 = markerSize/2;
QPointF points[3] = {
QPointF(fx1, fy1),
QPointF(fx2, fy2),
QPointF(fx3, fy3)
};
painter.drawPolygon(points, 3);
QPointF points2[3] = {
QPointF(-fx1, fy1),
QPointF(-fx2, fy2),
QPointF(-fx3, fy3)
};
painter.drawPolygon(points2, 3);
}
{
int nRollLines = 36;
float rotAng = 360.0 / nRollLines;
int rollLineLeng = m_size/25;
double fx1, fy1, fx2, fy2;
int fontSize = 8;
QString s;
blackPen.setWidth(1);
painter.setPen(blackPen);
painter.setFont(QFont("", fontSize));
for(int i=0; i<nRollLines; i++) {
if( i < nRollLines/2 )
s = QString("%1").arg(-i*rotAng);
else
s = QString("%1").arg(360-i*rotAng);
fx1 = 0;
fy1 = -m_size/2 + m_offset;
fx2 = 0;
if( i % 3 == 0 ) {
fy2 = fy1 + rollLineLeng;
painter.drawLine(QPointF(fx1, fy1), QPointF(fx2, fy2));
fy2 = fy1 + rollLineLeng+2;
painter.drawText(QRectF(-50, fy2, 100, fontSize+2),
Qt::AlignCenter, s);
} else {
fy2 = fy1 + rollLineLeng/2;
painter.drawLine(QPointF(fx1, fy1), QPointF(fx2, fy2));
}
painter.rotate(rotAng);
}
}
{
int rollMarkerSize = m_size/25;
double fx1, fy1, fx2, fy2, fx3, fy3;
painter.rotate(-m_roll);
painter.setBrush(QBrush(Qt::black));
fx1 = 0;
fy1 = -m_size/2 + m_offset;
fx2 = fx1 - rollMarkerSize/2;
fy2 = fy1 + rollMarkerSize;
fx3 = fx1 + rollMarkerSize/2;
fy3 = fy1 + rollMarkerSize;
QPointF points[3] = {
QPointF(fx1, fy1),
QPointF(fx2, fy2),
QPointF(fx3, fy3)
};
painter.drawPolygon(points, 3);
}
}
void QADI::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Left:
m_roll -= 1.0;
break;
case Qt::Key_Right:
m_roll += 1.0;
break;
case Qt::Key_Down:
if(m_pitch>-90.)
m_pitch -=1.0;
break;
case Qt::Key_Up:
if(m_pitch<90.)
m_pitch +=1.0;
break;
default:
QWidget::keyPressEvent(event);
break;
}
update();
}
QCompass::QCompass(QWidget *parent)
: QWidget(parent)
{
connect(this, SIGNAL(canvasReplot(void)), this, SLOT(canvasReplot_slot(void)));
m_sizeMin = 200;
m_sizeMax = 600;
m_offset = 2;
m_size = m_sizeMin - 2*m_offset;
setMinimumSize(m_sizeMin, m_sizeMin);
setMaximumSize(m_sizeMax, m_sizeMax);
resize(m_sizeMin, m_sizeMin);
setFocusPolicy(Qt::NoFocus);
m_yaw = 0.0;
m_alt = 0.0;
m_h = 0.0;
}
QCompass::~QCompass()
{
}
void QCompass::canvasReplot_slot(void)
{
update();
}
void QCompass::resizeEvent(QResizeEvent *event)
{
m_size = qMin(width(),height()) - 2*m_offset;
}
void QCompass::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QBrush bgGround(QColor(48,172,220));
QPen whitePen(Qt::white);
QPen blackPen(Qt::black);
QPen redPen(Qt::red);
QPen bluePen(Qt::blue);
QPen greenPen(Qt::green);
whitePen.setWidth(1);
blackPen.setWidth(2);
redPen.setWidth(2);
bluePen.setWidth(2);
greenPen.setWidth(2);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
{
painter.setPen(blackPen);
painter.setBrush(bgGround);
painter.drawEllipse(-m_size/2, -m_size/2, m_size, m_size);
}
{
int nyawLines = 36;
float rotAng = 360.0 / nyawLines;
int yawLineLeng = m_size/25;
double fx1, fy1, fx2, fy2;
int fontSize = 8;
QString s;
blackPen.setWidth(1);
painter.setPen(blackPen);
for(int i=0; i<nyawLines; i++) {
if( i == 0 ) {
s = "N";
painter.setPen(bluePen);
painter.setFont(QFont("", fontSize*1.3));
} else if ( i == 9 ) {
s = "W";
painter.setPen(blackPen);
painter.setFont(QFont("", fontSize*1.3));
} else if ( i == 18 ) {
s = "S";
painter.setPen(redPen);
painter.setFont(QFont("", fontSize*1.3));
} else if ( i == 27 ) {
s = "E";
painter.setPen(blackPen);
painter.setFont(QFont("", fontSize*1.3));
} else {
s = QString("%1").arg(i*rotAng);
painter.setPen(blackPen);
painter.setFont(QFont("", fontSize));
}
fx1 = 0;
fy1 = -m_size/2 + m_offset;
fx2 = 0;
if( i % 3 == 0 ) {
fy2 = fy1 + yawLineLeng;
painter.drawLine(QPointF(fx1, fy1), QPointF(fx2, fy2));
fy2 = fy1 + yawLineLeng+4;
painter.drawText(QRectF(-50, fy2, 100, fontSize+2),
Qt::AlignCenter, s);
} else {
fy2 = fy1 + yawLineLeng/2;
painter.drawLine(QPointF(fx1, fy1), QPointF(fx2, fy2));
}
painter.rotate(-rotAng);
}
}
{
int arrowWidth = m_size/5;
double fx1, fy1, fx2, fy2, fx3, fy3;
fx1 = 0;
fy1 = -m_size/2 + m_offset + m_size/25 + 15;
fx2 = -arrowWidth/2;
fy2 = 0;
fx3 = arrowWidth/2;
fy3 = 0;
painter.setPen(Qt::NoPen);
painter.setBrush(QBrush(Qt::blue));
QPointF pointsN[3] = {
QPointF(fx1, fy1),
QPointF(fx2, fy2),
QPointF(fx3, fy3)
};
painter.drawPolygon(pointsN, 3);
fx1 = 0;
fy1 = m_size/2 - m_offset - m_size/25 - 15;
fx2 = -arrowWidth/2;
fy2 = 0;
fx3 = arrowWidth/2;
fy3 = 0;
painter.setBrush(QBrush(Qt::red));
QPointF pointsS[3] = {
QPointF(fx1, fy1),
QPointF(fx2, fy2),
QPointF(fx3, fy3)
};
painter.drawPolygon(pointsS, 3);
}
{
int yawMarkerSize = m_size/12;
double fx1, fy1, fx2, fy2, fx3, fy3;
painter.rotate(-m_yaw);
painter.setBrush(QBrush(QColor(0xFF, 0x00, 0x00, 0xE0)));
fx1 = 0;
fy1 = -m_size/2 + m_offset;
fx2 = fx1 - yawMarkerSize/2;
fy2 = fy1 + yawMarkerSize;
fx3 = fx1 + yawMarkerSize/2;
fy3 = fy1 + yawMarkerSize;
QPointF points[3] = {
QPointF(fx1, fy1),
QPointF(fx2, fy2),
QPointF(fx3, fy3)
};
painter.drawPolygon(points, 3);
painter.rotate(m_yaw);
}
{
int altFontSize = 13;
int fx, fy, w, h;
QString s;
char buf[200];
w = 130;
h = 2*(altFontSize + 8);
fx = -w/2;
fy = -h/2;
blackPen.setWidth(2);
painter.setPen(blackPen);
painter.setBrush(QBrush(Qt::white));
painter.setFont(QFont("", altFontSize));
painter.drawRoundedRect(fx, fy, w, h, 6, 6);
painter.setPen(bluePen);
sprintf(buf, "ALT: %6.1f m", m_alt);
s = buf;
painter.drawText(QRectF(fx, fy+2, w, h/2), Qt::AlignCenter, s);
sprintf(buf, "H: %6.1f m", m_h);
s = buf;
painter.drawText(QRectF(fx, fy+h/2, w, h/2), Qt::AlignCenter, s);
}
}
void QCompass::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Left:
m_yaw -= 1.0;
break;
case Qt::Key_Right:
m_yaw += 1.0;
break;
case Qt::Key_Down:
m_alt -= 1.0;
break;
case Qt::Key_Up:
m_alt += 1.0;
break;
case Qt::Key_W:
m_h += 1.0;
break;
case Qt::Key_S:
m_h -= 1.0;
break;
default:
QWidget::keyPressEvent(event);
break;
}
update();
}
QKeyValueListView::QKeyValueListView(QWidget *parent) : QTableWidget(parent)
{
connect(this, SIGNAL(listUpdate(void)), this, SLOT(listUpdate_slot(void)));
m_mutex = new QMutex();
setRowCount(0);
setColumnCount(2);
QStringList htb = {"Name", "Value"};
this->setHorizontalHeaderLabels(htb);
QHeaderView *HorzHdr = horizontalHeader();
HorzHdr->setStretchLastSection(true);
HorzHdr->resizeSection(0, 80);
setEditTriggers(QTableWidget::NoEditTriggers);
setFocusPolicy(Qt::NoFocus);
}
QKeyValueListView::~QKeyValueListView()
{
delete m_mutex;
}
void QKeyValueListView::listUpdate_slot(void)
{
int i, n;
ListMap::iterator it;
QColor clCL1, clCL2;
QColor clB1, clB2;
int fontSize = 8;
int rowHeight = 20;
clCL1 = QColor(0x00, 0x00, 0xFF);
clCL2 = QColor(0x00, 0x00, 0x00);
clB1 = QColor(0xFF, 0xFF, 0xFF);
clB2 = QColor(0xE0, 0xE0, 0xE0);
m_mutex->lock();
n = m_data.size();
setRowCount(n);
setColumnCount(2);
for(i=0, it=m_data.begin(); it!=m_data.end(); i++, it++) {
if( this->item(i, 0) != NULL ) {
this->item(i, 0)->setText(it.key());
} else {
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(it.key());
item->setTextColor(clCL1);
if( i % 2 == 0 ) item->setBackgroundColor(clB1);
else item->setBackgroundColor(clB2);
item->setFont(QFont("", fontSize));
this->setItem(i, 0, item);
}
if( this->item(i, 1) != NULL ) {
this->item(i, 1)->setText(it.value());
} else {
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(it.value());
item->setTextColor(clCL2);
if( i % 2 == 0 ) item->setBackgroundColor(clB1);
else item->setBackgroundColor(clB2);
item->setFont(QFont("", fontSize));
this->setItem(i, 1, item);
}
setRowHeight(i, rowHeight);
}
m_mutex->unlock();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
- 516
- 517
- 518
- 519
- 520
- 521
- 522
- 523
- 524
- 525
- 526
- 527
- 528
- 529
- 530
- 531
- 532
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541
- 542
- 543
- 544
- 545
- 546
- 547
- 548
- 549
- 550
- 551
- 552
- 553
- 554
- 555
- 556
- 557
- 558
- 559
- 560
- 561
- 562
- 563
- 564
- 565
- 566
- 567
- 568
- 569
- 570
- 571
- 572
- 573
- 574
- 575
- 576
- 577
- 578
- 579
- 580
- 581
- 582
- 583
- 584
- 585
- 586
- 587
- 588
- 589
- 590
- 591
- 592
- 593
- 594
- 595
- 596
- 597
- 598
- 599
- 600
- 601
- 602
- 603
- 604
- 605
- 606
- 607
- 608
- 609
- 610
- 611
- 612
- 613
- 614
- 615
- 616
- 617
- 618
- 619
- 620
- 621
- 622
- 623
- 624
- 625
- 626
- 627
- 628
- 629
- 630
- 631
- 632
- 633
- 634
三、下载链接
https://download.csdn.net/download/u013083044/89067703