资源简介
两个小程序,socket实现数据本地收发,定时器40ms一飞行,80ms一发送,显控端显示飞行轨迹。老师布置的小任务,本人自学2天QT做的小程序,都是最简单的应用,一点小经验,注释齐全,适合初学者参考学习。
代码片段和文件信息
#include
#include “Console.h“
#include “ui_Console.h“
Console::Console(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
S_UdpSocket.bind(5099);
connect(&S_UdpSocket SIGNAL(readyRead()) SLOT(onProcessPendingDatagrams()));
qScene = new QGraphicsScene(this);
qScene->setSceneRect(0 0 ui.graphicsView->width()-5 ui.graphicsView->height()-5 );
pixMapItem = new QGraphicsPixmapItem();
image = new QPixmap(“plane.png“);
pixMapItem->setPixmap(image->scaled(20 20));
x0 = qScene->height() / 2;
y0 = qScene->width() / 2;
scale = qScene->height() / 2 / 10000;
pixMapItem->setPos(x0-10 y0-10);
//初始化画板
qScene->addItem(pixMapItem);
qScene->addLine(x0 0 x0 qScene->width());
qScene->addLine(0 y0 qScene->height() y0);
ui.graphicsView->setScene(qScene);
QTimer * timer_clock = new QTimer(this);
connect(timer_clock SIGNAL(timeout()) this SLOT(showTime()));
timer_clock->start(1000); //每1000ms刷新一次,即1秒
showTime();
}
Console::~Console()
{
}
double Console::angle()
{
return orientation * 180 / Pi;
}
void Console::onProcessPendingDatagrams()
{
last_x = now_x;
last_y = now_y;
QByteArray datagram;
while (S_UdpSocket.hasPendingDatagrams()) //是否有数据包等待读取
{
datagram.resize(S_UdpSocket.pendingDatagramSize());
S_UdpSocket.readDatagram(datagram.data() datagram.size());
}
QDataStream in(&datagram QIODevice::ReadOnly); //从datagram中读取数据
in.setVersion(QDataStream::Qt_5_9);
in >> now_x >> now_y >> speed >> orientation;
ui.label_4->setText(QString::number(now_x 10 2));
ui.label_9->setText(QString::number(now_y 10 2));
ui.label_5->setText(QString::number(speed 10 2));
ui.label_6->setText(QString::number(angle() 10 2));
if (now_x == 0 && now_y == 0) //回正时初始化清空画板,防止点回正时出现一条从最后位置连向原点的线
{
qScene->clear();
pixMapItem = new QGraphicsPixmapItem();
pixMapItem->setPixmap(image->scaled(20 20));
pixMapItem->setPos(x0 - 10 y0 - 10);
QPointF centerPos = pixMapItem->boundingRect().center();
pixMapItem->setTransformOriginPoint(centerPos.x() centerPos.y());
pixMapItem->setRotation(-angle());
qScene->addItem(pixMapItem);
qScene->addLine(x0 0 x0 qScene->width());
qScene->addLine(0 y0 qScene->height() y0);
}
else //绘制轨迹
{
QGraphicsLineItem *pLine = new QGraphicsLineItem();
QPen pen = pLine->pen();
pen.setColor(QColor(0 160 230));
pen.setWidth(2);
pLine->setPen(pen);
pLine->setLine(QLineF(last_x * scale + x0 -last_y * scale + y0 now_x * scale + x0 -now_y * scale + y0));
qScene->addItem(pLine);
pixMapItem->setPos(now_x * scale + x0 - 10 -now_y * scale + y0 - 10);
}
QPointF centerPos = pixMapItem->boundingRect().center();
pixMapItem->setTransformOriginPoint(centerPos.x() centerPos.y());
pixMapItem->setRotation(-angle());
}
void Console::Btn_TakeOff_Click() //起飞按钮事件
{
QByteArray datagram;
QDataStream o
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
I.A.... 1073 2018-05-14 16:34 Console.sln
I.A.... 1067 2018-05-14 16:34 plane.sln
I.A.... 6029 2018-05-14 16:34 Console\Console.cpp
I.A.... 1334 2018-05-14 16:34 Console\Console.h
I.A.... 67 2018-05-14 16:34 Console\Console.qrc
I.A.... 12728 2018-05-14 16:34 Console\Console.ui
I.A.... 8511 2018-05-14 16:34 Console\Console.vcxproj
I.A.... 2036 2018-05-14 16:34 Console\Console.vcxproj.filters
I.A.... 617 2018-05-14 16:34 Console\Console.vcxproj.user
I.A.... 4921 2018-05-14 16:34 Console\GeneratedFiles\Debug\moc_Console.cpp
I.A.... 1396 2018-05-14 16:34 Console\GeneratedFiles\qrc_Console.cpp
I.A.... 11437 2018-05-14 16:34 Console\GeneratedFiles\ui_Console.h
I.A.... 183 2018-05-14 16:34 Console\main.cpp
I.A.... 7163 2018-05-14 16:34 Console\plane.png
文件 141 2018-05-14 16:34 Console\x64\Debug\Console.log
文件 456271 2018-05-14 16:34 Console\x64\Debug\Console.obj
文件 5550 2018-05-14 16:34 Console\x64\Debug\Console.tlog\CL.command.1.tlog
文件 95654 2018-05-14 16:34 Console\x64\Debug\Console.tlog\CL.read.1.tlog
文件 1796 2018-05-14 16:34 Console\x64\Debug\Console.tlog\CL.write.1.tlog
文件 202 2018-05-14 16:34 Console\x64\Debug\Console.tlog\Console.lastbuildstate
文件 1774 2018-05-14 16:34 Console\x64\Debug\Console.tlog\li
文件 4282 2018-05-14 16:34 Console\x64\Debug\Console.tlog\li
文件 744 2018-05-14 16:34 Console\x64\Debug\Console.tlog\li
文件 109754 2018-05-14 16:34 Console\x64\Debug\main.obj
文件 118517 2018-05-14 16:34 Console\x64\Debug\moc_Console.obj
文件 6480 2018-05-14 16:34 Console\x64\Debug\qrc_Console.obj
文件 2150400 2018-05-14 16:34 Console\x64\Debug\vc141.pdb
I.A.... 208 2018-05-14 16:34 plane\function.cpp
I.A.... 134 2018-05-14 16:34 plane\function.h
文件 2711 2018-05-14 16:34 plane\GeneratedFiles\Debug\moc_ModelPlane.cpp
............此处省略47个文件信息
- 上一篇:sim900a 中文短信发送与接收
- 下一篇:Creo2.0三维布线教程
评论
共有 条评论