admin管理员组文章数量:1566222
目录
- win10中vs+qt+qgis环境搭建
- 源代码
- 界面搭建
- 完整工程下载
- 问题解决
- 效果
win10中vs+qt+qgis环境搭建
环境搭建见我另一篇博客:(很详细,免编译)https://blog.csdn/xx970829/article/details/109890931
环境搭建常见问题及解决方案:
https://blog.csdn/xx970829/article/details/109891607
源代码
main.cpp
#include "QtWidgetsApplicationxx.h"
#include <QtWidgets/QApplication>
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//
QgsApplication b(argc, argv, true);
QgsApplication::setPrefixPath("./qgis", true);//QGIS路径 F://OSGeo4W64/apps/
QgsApplication::initQgis();//初始化QGIS应用
///
QtWidgetsApplicationxx w;
w.show();
return a.exec();
}
/
QtWidgetsApplicationxx.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtWidgetsApplicationxx.h"
#include<string>
#include<iostream>
#include <iomanip>
#include <windows.h>
#include<qapplication.h>
#include <io.h>
#include <fstream>
#include<stdio.h>
//qt
#include <qmessagebox.h>
#include <QString>
#include <QFileDialog>
#include <QWidget>
#include <QSerialPort>
#include<QtConcurrent>
#include<QHBoxLayout>
#include<QDebug>
#include <QByteArray>
#include <QApplication>
#include <QSerialPortInfo>
#include <QComboBox>
#include <QPushButton>
#include <QMainWindow>
#include <QtSvg/QGraphicsSvgItem>//QtSvg/QGraphicsSvgItem QGraphicsSvgItem
#include<QKeyEvent>
#include<QtWidgets/QApplication>
#include <QStringList>
#include<QMessageBox>
#include<QObject>
#include <QList>
#include <QFileInfoList>
#include <QDir>
#include <QLibrary>
#include<QTabWidget>
#include<QMutex>
//gis
#include <qgssinglesymbolrenderer.h>
#include <qgsvectordataprovider.h>
#include <qgsrasterlayer.h>
#include<qgsCoordinatetransform.h>
#include<qgsmaplayerref.h>
#include<qgsmaplayerrenderer.h>
#include<qgsauthimportcertdialog.h>
#include<qgsapplication.h>
#include<qgsvectorlayer.h>
#include<qgsmapcanvas.h>
#include<qgsmaplayerstore.h>//qgsmaplayerstore.h qgsmaplayerregistry.h
#include <qgsproviderregistry.h>
#include <qgssymbollayer.h>
#include <qgssymbol.h>
#include <qgsmarkersymbollayer.h>
#include <qgsvectorlayerrenderer.h>
#include <qgsrendercontext.h>
class QtWidgetsApplicationxx : public QMainWindow
{
Q_OBJECT
public:
QtWidgetsApplicationxx(QWidget *parent = Q_NULLPTR);
~QtWidgetsApplicationxx();
//QtWidgetsClass map;//创建一个窗体,类似于Qt
QStringList getPortNameList();//获取可用的串口列表
void openPort();//打开串口
//void listenthread();//读取数据
void countxy(double r[8], double *xx, double *yy);
int power(int x, int n);
//----------------------------------------------------------------------------------------------
void openMap(QgsMapCanvas *mapCanvas,QgsVectorLayer* newLayer, QgsVectorLayer* newLayer1, QgsVectorLayer* newLayer2);
QgsVectorLayer* drawfire(QgsVectorLayer* newLayer, double x, double y);
QgsVectorLayer* drawplane(QgsVectorLayer* newLayer, double x, double y);
QgsVectorLayer* createLayer(QgsVectorLayer* newLayer);
//QgsVectorLayer* createLayer();
//----------------------------------------------------------------------------------------------
public slots:
void receiveInfo();
//void print_s();//combox选框
protected:
//键盘事件
void keyPressEvent(QKeyEvent *event);
private slots:
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
void on_pushButton_6_clicked();
void on_pushButton_7_clicked();
void on_pushButton_8_clicked();
void on_pushButton_9_clicked();
void on_pushButton_10_clicked();
void on_pushButton_11_clicked();
void on_pushButton_12_clicked();
void on_pushButton_13_clicked();
void on_pushButton_14_clicked();
void on_pushButton_15_clicked();
private:
Ui::QtWidgetsApplicationxxClass ui;
QSerialPort* m_serialPort; //串口类
QStringList m_portNameList;
QgsVectorLayer *newLayer;
QgsVectorLayer *newLayer1;
QgsVectorLayer *newLayer2;
QgsMapCanvas *mapCanvas;
};
/
QtWidgetsApplicationxx.cpp
#include "QtWidgetsApplicationxx.h"
#include<QWidget>
//-----------------------------------------------------------------------
using namespace std;
int k = 0, kk = 0, k_ = 0, kk_ = 0;//控制检测火qDebug
int lei = 0, guan = 0, xiang = 0, ros = 0, bao = 0, m = 0, por = 0;//控制设备
int lei_ = 2, guan_ = 2, xiang_ = 2, bao_ = 2, Reset = 2;
int comp = 0, reboot = 0;//控制工控机
/
QtWidgetsApplicationxx::QtWidgetsApplicationxx(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
this->setWindowTitle("serialport");
//ui.tabWidget_4->setStyleSheet("QTabWidget{border-image:url(F:/OSGeo4W64/QGIS/11111.jpg)}");
m_serialPort = new QSerialPort();
m_portNameList = getPortNameList();
uiboBox->addItems(m_portNameList);
newLayer = new QgsVectorLayer();
newLayer1 = new QgsVectorLayer();
newLayer2 = new QgsVectorLayer();
mapCanvas = new QgsMapCanvas();
newLayer=createLayer(newLayer);
newLayer1= createLayer(newLayer1);
newLayer2= createLayer(newLayer2);
ui.widget_4->setStyleSheet("QWidget{border: 2px outset green }");
connect(ui.pushButton_2, &QPushButton::clicked, this, &QtWidgetsApplicationxx::openPort);
connect(ui.pushButton_3, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_3_clicked);//101
connect(ui.pushButton_4, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_4_clicked);//102
connect(ui.pushButton_5, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_5_clicked);//103
connect(ui.pushButton_6, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_6_clicked);//104
connect(ui.pushButton_7, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_7_clicked);//105
connect(ui.pushButton_8, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_8_clicked);
connect(ui.pushButton_9, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_9_clicked);//107
connect(ui.pushButton_10, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_10_clicked);//106
connect(ui.pushButton_11, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_11_clicked);
connect(ui.pushButton_12, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_12_clicked);//222
connect(ui.pushButton_13, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_13_clicked);
connect(ui.pushButton_14, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_14_clicked);//225
connect(ui.pushButton_15, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_15_clicked);//223
//---------------------------------------------------------------------------------------------------------
connect(uiboBox_2, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
[=]() {
if (uiboBox_2->currentIndex() == 0)
{
ui.lineEdit->setText("101");
}if (uiboBox_2->currentIndex() == 1)
{
ui.lineEdit->setText("102");
}if (uiboBox_2->currentIndex() == 2)
{
ui.lineEdit->setText("103");
}if (uiboBox_2->currentIndex() == 3)
{
ui.lineEdit->setText("104");
}if (uiboBox_2->currentIndex() == 4)
{
ui.lineEdit->setText("105");
}if (uiboBox_2->currentIndex() == 5)
{
ui.lineEdit->setText("106");
}if (uiboBox_2->currentIndex() == 6)
{
ui.lineEdit->setText("107");
}if (uiboBox_2->currentIndex() == 7)
{
ui.lineEdit->setText("201");
}if (uiboBox_2->currentIndex() == 8)
{
ui.lineEdit->setText("202");
}if (uiboBox_2->currentIndex() == 9)
{
ui.lineEdit->setText("203");
}if (uiboBox_2->currentIndex() == 10)
{
ui.lineEdit->setText("222");
}if (uiboBox_2->currentIndex() == 11)
{
ui.lineEdit->setText("223");
}if (uiboBox_2->currentIndex() == 12)
{
ui.lineEdit->setText("224");
}if (uiboBox_2->currentIndex() == 13)
{
ui.lineEdit->setText("225");
}
});
}
//----------------------------------------------------------------------------------------------------------
void QtWidgetsApplicationxx::openPort()
{
if (m_serialPort->isOpen())//如果串口已经打开了 先给他关闭了
{
m_serialPort->clear();
m_serialPort->close();
}
m_serialPort->setPortName(uiboBox->currentText());//当前选择的串口名字
if (!m_serialPort->open(QIODevice::ReadWrite))//用ReadWrite 的模式尝试打开串口
{
ui.lineEdit_3->setText("Failed to open serial port!");
return;
}
ui.lineEdit_3->setText("Serial Port Open successfully!");
//QSerialPort::Baud9600
m_serialPort->setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);//设置波特率和读写方向
m_serialPort->setDataBits(QSerialPort::Data8); //数据位为8位
m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//无流控制
m_serialPort->setParity(QSerialPort::NoParity); //无奇偶校验位
m_serialPort->setStopBits(QSerialPort::OneStop); //一位停止位
m_serialPort->open(QIODevice::ReadWrite);
connect(m_serialPort, &QSerialPort::readyRead, this, &QtWidgetsApplicationxx::receiveInfo);
por = 1;//控制串口
comp = 0;//控制工控机
//检测是否开机
QByteArray temp;
temp.resize(1);
temp[0] = 224;
m_serialPort->write(temp);
}
///
QtWidgetsApplicationxx::~QtWidgetsApplicationxx()
{
if (m_serialPort->isOpen())
{
m_serialPort->close();
}
delete m_serialPort;
}
QStringList QtWidgetsApplicationxx::getPortNameList()
{
QStringList m_serialPortName;
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
m_serialPortName << info.portName();
//qDebug() << "serialPortName:" << info.portName();
}
return m_serialPortName;
}
//-------------------------------------------------------------------------------------------------
//快捷键设置
void QtWidgetsApplicationxx::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Return)
{
on_pushButton_11_clicked();//发送数据
}if (event->key() == Qt::Key_R)
{
on_pushButton_12_clicked();//复位。。。。
}if (event->key() == Qt::Key_L)
{
on_pushButton_3_clicked();//雷达
}if (event->key() == Qt::Key_G)
{
on_pushButton_4_clicked();//惯导
}if (event->key() == Qt::Key_I)
{
on_pushButton_5_clicked();//红外相机
}if (event->key() == Qt::Key_M)
{
on_pushButton_8_clicked();//地图
}if (event->key() == Qt::Key_T)
{
on_pushButton_9_clicked();//检测火
}if (event->key() == Qt::Key_Delete)
{
on_pushButton_13_clicked();//清空特征
}if (event->key() == Qt::Key_Escape)
{
on_pushButton_14_clicked();
ui.lineEdit->setText("Shutdown ..");//关机
}if (event->key() == Qt::Key_F1)
{
on_pushButton_15_clicked();
ui.lineEdit->setText("Reboot ..");//重启Control
}if (event->key() == Qt::Key_Control)
{
comp = 1;
ui.lineEdit_3->setText("Booting successful!!");//默认开机成功
ui.lineEdit->setText("Booting successful!!");
}
}
//combx设置---------------------------------------------------------------------------------------------------
/*listenthread getPortNameList
void QtWidgetsApplicationxx::print_s()
{
if (uiboBox->currentIndex() == 0)
{
ui.lineEdit->setText("101");
}if (uiboBox->currentIndex() == 1)
{
ui.lineEdit->setText("102");
}
}*/
//------------------------------------------------------------------------------------------------------
void QtWidgetsApplicationxx::receiveInfo()//解析接收到数据
{
QByteArray info = m_serialPort->readAll();
double *temp = new double[16];
int j = 0;
QString strDisplay;
QString str = info.toHex().data();
for (int i = 0; i<str.length(); i += 2)
{
QString st = str.mid(i, 2);//返回一个字符串 从指定位置返回n个字符
strDisplay += st;
strDisplay += " ";//每个字节中间的空格
QString str_hex = st; //获取十六进制数
double dec = str_hex.toInt(0, 16);
//qDebug() << "dec==:" << dec;
temp[j] = dec;
j++;
}
//
if (temp[0] == 101 && (k != 2)&& lei_ ==1)//激光雷达启动成功
{
lei = 1;
ui.pushButton_3->setText("Lidar ON");
ui.lineEdit_3->setText("Lidar activated!!");
ui.lineEdit->setText("Lidar activated!!");
}if (temp[0] == 201 && (k != 2)&& lei_ == 0)//激光雷达已关闭
{
lei = 0;
ui.pushButton_3->setText("Lidar OFF");
ui.lineEdit_3->setText("Lidar is down!!");
ui.lineEdit->setText("Lidar is down!!");
}
if (temp[0] == 102 && (k != 2)&& guan_ == 1)//惯导启动成功GNSS/INS
{
guan = 1;
ui.pushButton_4->setText("GNSS/INS ON");
ui.lineEdit_3->setText("GNSS/INS activated!!");
ui.lineEdit->setText("GNSS/INS activated!!");
}if (temp[0] == 202 && (k != 2)&& guan_ == 0)//惯导已关闭
{
guan = 0;
ui.pushButton_4->setText("GNSS/INS OFF");
ui.lineEdit_3->setText("GNSS/INS is down!!");
ui.lineEdit->setText("GNSS/INS is down!!");
}
if (temp[0] == 103 && (k != 2)&& xiang_ == 1)//红外相机启动成功
{
xiang = 1;
ui.pushButton_5->setText("I-Camera ON");
ui.lineEdit_3->setText("I-Camera activated!!");
ui.lineEdit->setText("I-Camera activated!!");
}if (temp[0] == 203 && (k != 2)&& xiang_ == 0)//红外相机已关闭
{
xiang = 0;
ui.pushButton_5->setText("I-Camera OFF");
ui.lineEdit_3->setText("I-Camera is down!!");
ui.lineEdit->setText("I-Camera is down!!");
}
if (temp[0] == 104&& ros == 0)//正在记录ROS包
{
ros = 1;
ui.lineEdit_3->setText("Recording packets...");
ui.lineEdit->setText("Recording packets...");
}
if (temp[0] == 105&& ros == 1)//ROS包记录结束
{
ros = 0;
ui.lineEdit_3->setText("Package record closed!!");
ui.lineEdit->setText("Package record closed!!");
}
if (temp[0] == 106 && bao_ == 1)//开始播放ROS包
{
bao = 1;
k = 2;
ui.lineEdit_3->setText("Playing the Ros package..");
ui.lineEdit->setText("Playing Ros package..");
}if (temp[0] == 206 && bao_ == 0)//ROS包播放结束
{
bao = 0;
ui.lineEdit_3->setText("End of Ros package!!");
ui.lineEdit->setText("End Ros package!!");
}
if (temp[0] == 222&& Reset == 1)//复位
{
Reset = 0;
ui.lineEdit_3->setText("Reset successful!!");
ui.lineEdit->setText("Reset successful!!");
}
if (temp[0] == 207 && k_ == 1)
{
k_ = 0;
k = 1;
ui.lineEdit_3->setText("Stop the fire detection!!");
ui.lineEdit->setText("Stop fire detection!!");
}if (temp[0] == 223)
{
comp = 0;
reboot = 1;
ui.lineEdit_3->setText("Reboot successful!!");//重启成功
ui.lineEdit->setText("Reboot successful!!");
}if (temp[0] == 224)
{
comp = 1;//comp == 1
reboot = 0;
ui.lineEdit_3->setText("Booting successful!!");//开机成功
ui.lineEdit->setText("Booting successful!!");
}if (temp[0] == 225)
{
comp = 0;
ui.lineEdit_3->setText("Shutdown successful!!");//关机成功
ui.lineEdit->setText("Shutdown successful!!");
}
///Booting successful
if (kk == 1&& k == 2 && (k_ != 0) && (temp[0] < 100))
{
//ui.lineEdit_3->setText(strDisplay);
double xx, yy;
countxy(temp, &xx, &yy);
if (yy<100)
{
//QString s = QString::number(xx);
//ui.lineEdit_3->setText(s);
newLayer1->startEditing();
newLayer1->deleteFeatures(newLayer1->allFeatureIds());//删除特征(清空图层)
newLayer1->commitChanges();
// 更新范围
newLayer1->updateExtents();
drawplane(newLayer1, xx, yy);//qgis 画点--plane
}if (yy>100)
{
yy = yy - 100;
drawfire(newLayer2, xx, yy);//qgis 画点--fire
}
}/
if (temp[0] == 107 && k_ == 0)
{
k_ = 1;
k = 2;
ui.lineEdit_3->setText("Start testing the fire!!");
ui.lineEdit->setText("Start testing fire!!");
}
}
//------------------------------------------------------------------------------------------------
//int lei = 0, guan = 0, xiang = 0, ros = 0, rosclose = 0;
void QtWidgetsApplicationxx::on_pushButton_3_clicked()
{
QByteArray temp;
temp.resize(1);
if (lei == 0 && (k != 2)&& comp == 1)
{
lei_ = 1;
temp[0] = 101;//启动激光雷达
ui.lineEdit_3->setText("Turn on the Lidar");
m_serialPort->write(temp);
}
if (lei == 1 && (k != 2)&& comp == 1)
{
lei_ = 0;
temp[0] = 201;//关闭激光雷达
ui.lineEdit_3->setText("Turn off the Lidar");
m_serialPort->write(temp);
}
if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
}
if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
}
}
void QtWidgetsApplicationxx::on_pushButton_4_clicked()
{
QByteArray temp;
temp.resize(1);
if (guan == 0 && (k != 2)&& comp == 1)
{
guan_ = 1;
temp[0] = 102;//启动惯导
ui.lineEdit_3->setText("Turn on the GNSS/INS");
m_serialPort->write(temp);
}if (guan == 1 && (k != 2) && comp == 1)
{
guan_ = 0;
temp[0] = 202;//关闭惯导
ui.lineEdit_3->setText("Turn off the GNSS/INS");
m_serialPort->write(temp);
}if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
}if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
}
}
void QtWidgetsApplicationxx::on_pushButton_5_clicked()
{
QByteArray temp;
temp.resize(1);
if (xiang == 0 && (k != 2) && comp == 1)
{
xiang_ = 1;
temp[0] = 103;//启动红外相机
ui.lineEdit_3->setText("Turn on the I-camera");
m_serialPort->write(temp);
}if (xiang == 1 && (k != 2) && comp == 1)
{
xiang_ = 0;
temp[0] = 203;//关闭红外相机
ui.lineEdit_3->setText("Turn off the I-camera");
m_serialPort->write(temp);
}if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
}if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
}
}
void QtWidgetsApplicationxx::on_pushButton_6_clicked()
{
QByteArray temp;
temp.resize(1);
temp[0] = 104;//开始记录ROS包
if (ros == 0 && comp == 1)
{
m_serialPort->write(temp);
ui.lineEdit_3->setText("Start recording the Ros package");
}if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
}if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
}
}
void QtWidgetsApplicationxx::on_pushButton_7_clicked()
{
QByteArray temp;
temp.resize(1);
temp[0] = 105;//记录结束
if (ros == 1 && comp == 1)
{
m_serialPort->write(temp);
ui.lineEdit_3->setText("End of record");
}if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
}if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
}
}
//--------------------------------------------------------------------------------------------------------------
void QtWidgetsApplicationxx::on_pushButton_8_clicked()
{
if (m % 2 == 0)//打开地图
{
openMap(mapCanvas, newLayer, newLayer1, newLayer2);
ui.pushButton_8->setText("Close Map");
kk = 1;
}if (m % 2 == 1)
{
mapCanvas->close();
ui.pushButton_8->setText("Open Map");
kk = 0;
}
m++;
}
void QtWidgetsApplicationxx::on_pushButton_9_clicked()
{
QByteArray temp;
temp.resize(1);
if (k_== 0 && comp == 1)
{
kk_ = 1;
temp[0] = 107;//开始检测火
ui.lineEdit_3->setText("Start testing the fire");
m_serialPort->write(temp);
}if (k_ == 1 && comp == 1)
{
temp[0] = 207;//停止检测
kk_ = 0;
ui.lineEdit_3->setText("Stop the fire detection");
m_serialPort->write(temp);
}if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
}if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
}
}
void QtWidgetsApplicationxx::on_pushButton_10_clicked()
{
if (kk_ == 1)
{
k = 0;
}
QByteArray temp;
temp.resize(1);//lei_ = 2, guan_ = 2, xiang_ = 2
if(lei_ ==1)
{
ui.lineEdit_3->setText("Please turn off the LIDAR");
}if (guan_ == 1)
{
ui.lineEdit_3->setText("Please turn off the GNSS/INS");
}if (xiang_ == 1)
{
ui.lineEdit_3->setText("Please turn off the I-camera");
}if ((lei_ != 1) && (guan_ != 1) && (xiang_ != 1) && comp == 1)
{
if (bao == 0)
{
bao_ = 1;
temp[0] = 106;//开始播放ROS包
ui.lineEdit_3->setText("Start playing the Ros package");
m_serialPort->write(temp);
}if (bao == 1)
{
bao_ = 0;
temp[0] = 206;//停止播放ROS包
ui.lineEdit_3->setText("Stop playing the Ros package");
m_serialPort->write(temp);
}
}/
if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
}
if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
}
}
void QtWidgetsApplicationxx::on_pushButton_11_clicked()
{
QString str = ui.lineEdit->text();//自定义发送数据
int dec = str.toInt(0, 10);
QByteArray temp;
temp.resize(1);
temp[0] = dec;
if (comp == 1)
{
m_serialPort->write(temp);
ui.lineEdit_3->setText("Sending data now..");
}if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
}
if (por == 0)
{
ui.lineEdit->setText("Please open the Serial Port");
}
}
void QtWidgetsApplicationxx::on_pushButton_12_clicked()
{
/*
int k = 0, kk = 0, k_ = 0;//控制检测火
int lei = 0, guan = 0, xiang = 0, ros = 0, bao =0 ;//控制设备
int lei_ = 2, guan_ = 2, xiang_ = 2, bao_ = 2, Reset = 2;
*/
k = 0; //复位
//kk = 0;//打开地图
kk_ = 0;
k_ = 0;
lei = 0;
guan = 0;
xiang = 0;
ros = 0;
bao = 0;
lei_ = 2;
guan_ = 2;
xiang_ = 2;
bao_ = 2;
Reset = 2;
QByteArray temp;
temp.resize(1);
temp[0] = 222;
if (comp == 1)
{
m_serialPort->write(temp);
}
//mapCanvas->close();
ui.pushButton_3->setText("Lidar");//
ui.pushButton_4->setText("GNSS/INS");
ui.pushButton_5->setText("I-Camera");
ui.lineEdit_3->setText("Reset..");
if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
}if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
}
}
void QtWidgetsApplicationxx::on_pushButton_13_clicked()
{
newLayer1->startEditing();
newLayer1->deleteFeatures(newLayer1->allFeatureIds());//删除特征(清空图层)
newLayer1->commitChanges();
newLayer1->updateExtents();// 更新范围
newLayer2->startEditing();
newLayer2->deleteFeatures(newLayer2->allFeatureIds());//删除特征(清空图层)
newLayer2->commitChanges();
newLayer2->updateExtents();// 更新范围
}
void QtWidgetsApplicationxx::on_pushButton_14_clicked()
{
QByteArray temp;
temp.resize(1);
temp[0] = 225;//关机
if (comp == 1)
{
m_serialPort->write(temp);
ui.lineEdit_3->setText("Computer shutdown");
}
}
void QtWidgetsApplicationxx::on_pushButton_15_clicked()
{
QByteArray temp;
temp.resize(1);
temp[0] = 223;//重启
if (comp == 1&& reboot == 0)
{
m_serialPort->write(temp);
ui.lineEdit_3->setText("Reboot the computer");
}
}
//--------------------------------------------------------------------------------------------------------------
void QtWidgetsApplicationxx::countxy(double r[8], double *xx, double *yy)
{
double x = r[0] * power(255, 3) + r[1] * power(255, 2) + r[2] * 255 + r[3];
double y = r[4] * power(255, 3) + r[5] * power(255, 2) + r[6] * 255 + r[7];
x = x / 1000000;
y = y / 1000000;
cout << setprecision(6) << fixed << x << endl;
cout << setprecision(6) << fixed << y << endl;
*xx = x;
*yy = y;
}
int QtWidgetsApplicationxx::power(int x, int n)
{
int val = 1;
while (n--)
val *= x;
return val;
}
//--------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//************ 创建图层 **********************************************************************************************
QgsVectorLayer* QtWidgetsApplicationxx::createLayer(QgsVectorLayer*newLayer)
{
QString layerProperties = "Point?"; // 定义了我们创建图层的几何类型,可以是"Point"、"LineString"、"Polygon"、"MultiPoint"、"MultiLineString"、"MultiPolygon"其中之一
layerProperties.append(QString("crs=EPSG:4236&")); //定义图层的参照坐标,如果需要一定灵活性可以参照QGis的方式通过对话框选取,或是根据自己的需求来实现,需要改变的仅仅是"EPSG:4326" 而已
layerProperties.append(QString("field=id:integer&field=name:string(50)&")); //定义的图层字段,多个字段用"&" 进行连接,完整形式为 field=name:type(length,precision) ,从参数看不仅可以定义长度还可以定义其精度
layerProperties.append(QString("index=yes&")); // 定义空间索引,于数据量较大的图层很有用
layerProperties.append(QString( // 通过QUuid创建了一个全局唯一标识符(UUID),Qt中解释主要是用于分布式计算环境中的实体标识,而此处是用于当我们多次创建临时图层时的唯一标识符
"memoryid=%1").arg(QUuid::createUuid().toString()));
newLayer = new QgsVectorLayer(layerProperties, QString("临时点层"), QString("memory"));//开始创建临时图层
//QgsMapLayerRegistry::instance()->addMapLayer(newLayer);
return newLayer;
}
//*************** 获取地图 *******************************************************************************************
/*QgsVectorLayer* */
void QtWidgetsApplicationxx::openMap(QgsMapCanvas *mapCanvas,QgsVectorLayer*newLayer, QgsVectorLayer*newLayer1, QgsVectorLayer*newLayer2)
{
QString myPluginsDir = "..\\plugins"; //注册插件库
QgsProviderRegistry::instance(myPluginsDir);
//QList<QgsMapLayer*>layer;
QList<QgsMapLayer*>layerlist;
//创建地图画布
//QgsMapCanvas*mapCanvas = new QgsMapCanvas();
//mapCanvas = new QgsMapCanvas();
//*************获取网络地图************************
//QgsRasterLayer*rastermap = new QgsRasterLayer("type=xyz&url=https://webst01.is.autonavi/appmaptile?style%3D6%26x%3D%7Bx%7D%26y%3D%7By%7D%26z%3D%7Bz%7D&zmax=18&zmin=0","gaode", "wms");
QgsRasterLayer*rastermap = new QgsRasterLayer("type=xyz&url=https://tile.openstreetmap/%7Bz%7D/%7Bx%7D/%7By%7D.png&zmax=19&zmin=0", " ", "wms");
//*************添加shp地图*************************
//QString fileName = QFileDialog::getOpenFileName(this, tr("Open shape file"), "", "*.shp");
//QStringList temp = fileName.split('/');
//QString basename = temp.at(temp.size() - 1);
//QgsVectorLayer*rastermap = new QgsVectorLayer(fileName, basename, "ogr");
if (!rastermap->isValid())
{
QMessageBox::critical(this, "error", QString("This layer is invalid"));
}
else
{
layerlist.append(newLayer);
}
//把图层添加到地图画布并显示
mapCanvas->setExtent(rastermap->extent());设置显示范围。Extent:范围、尺度
layerlist.append(newLayer1);//将图层1加载到地图中
QgsProject::instance()->addMapLayers(layerlist);
layerlist.append(newLayer2);//将图层1加载到地图中
QgsProject::instance()->addMapLayers(layerlist);
layerlist.append(rastermap);
mapCanvas->setLayers(layerlist);//设置图层集合
mapCanvas->setVisible(false);//设置是否可见
mapCanvas->freeze(false);//解冻对图层的操作
mapCanvas->resize(800, 500);
mapCanvas->refresh();
mapCanvas->show();
//return newLayer1;
}
//*************** 创建点 ******************************************************************************************
QgsVectorLayer* QtWidgetsApplicationxx::drawplane(QgsVectorLayer*newLayer1, double x, double y)
{
//定义坐标转换对象trans,从src向dest转换(CRS转换)
//EPSG:4326指的是WGS84系统..EPSG是最常用的授权机构,它涵盖了全世界范围广泛的坐标系统。
QgsCoordinateReferenceSystem src(4326, QgsCoordinateReferenceSystem::EpsgCrsId);
QgsCoordinateReferenceSystem dest(3857, QgsCoordinateReferenceSystem::EpsgCrsId);
QgsCoordinateTransformContext context;
QgsCoordinateTransform trans(src, dest, context);
//转换坐标
QgsPointXY point1 = trans.transform(x, y);
//QgsVectorDataProvider* dateProvider = newLayer1->dataProvider();//创建一个容器
/plane
QgsSvgMarkerSymbolLayer* svgMarker1 = new QgsSvgMarkerSymbolLayer("./plane13.svg");// F:/OSGeo4W64/QtQgisVs
QgsSymbolLayerList symList1;
symList1.append(svgMarker1);
QgsMarkerSymbol* markSym1 = new QgsMarkerSymbol(symList1);
QgsSingleSymbolRenderer* symRenderer1 = new QgsSingleSymbolRenderer(markSym1);
newLayer1->setRenderer(symRenderer1);
svgMarker1->setSize(10);
QgsFeature MyFeature1;
MyFeature1.setGeometry(QgsGeometry::fromPointXY(point1));
MyFeature1.setAttributes(QgsAttributes() << QVariant(2) << QVariant("test"));
// 开始编辑
newLayer1->startEditing();
// 添加要素
newLayer1->addFeatures(QgsFeatureList() << MyFeature1);
plane
// 保存
newLayer1->commitChanges();
// 更新范围
newLayer1->updateExtents();
//newLayer1->s
//mapCanvas->refresh();
//mapCanvas->show();
return newLayer1;
}
/// fire ///
QgsVectorLayer* QtWidgetsApplicationxx::drawfire(QgsVectorLayer*newLayer, double x, double y)
{
//定义坐标转换对象trans,从src向dest转换(CRS转换)
//EPSG:4326指的是WGS84系统..EPSG是最常用的授权机构,它涵盖了全世界范围广泛的坐标系统。
QgsCoordinateReferenceSystem src(4326, QgsCoordinateReferenceSystem::EpsgCrsId);
QgsCoordinateReferenceSystem dest(3857, QgsCoordinateReferenceSystem::EpsgCrsId);
QgsCoordinateTransformContext context;
QgsCoordinateTransform trans(src, dest, context);
//转换坐标
QgsPointXY point = trans.transform(x, y);
//QgsVectorDataProvider* dateProvider = newLayer->dataProvider();//创建一个容器
QgsFeature MyFeature;
fire
QgsSvgMarkerSymbolLayer* svgMarker = new QgsSvgMarkerSymbolLayer("./fire1.svg");// F:/OSGeo4W64/QtQgisVs
QgsSymbolLayerList symList;
symList.append(svgMarker);
QgsMarkerSymbol* markSym = new QgsMarkerSymbol(symList);
QgsSingleSymbolRenderer* symRenderer = new QgsSingleSymbolRenderer(markSym);
newLayer->setRenderer(symRenderer);
svgMarker->setSize(3);
fire
MyFeature.setGeometry(QgsGeometry::fromPointXY(point));
MyFeature.setAttributes(QgsAttributes() << QVariant(1) << QVariant("test"));
// 开始编辑
newLayer->startEditing();
// 添加要素
newLayer->addFeatures(QgsFeatureList() << MyFeature);
// 保存
newLayer->commitChanges();
// 更新范围
newLayer->updateExtents();
//mapCanvas->refresh();
return newLayer;
}
界面搭建
桌面图标:
第一页:mygis
远程控制设备的启动、关闭、运行、数据处理与发送。。。
第二页:manual
手动控制。。。
第三页:使用说明
快捷键:
R 复位
L 雷达
G 惯导
I 红外相机
M 地图
T 检测火
F1 重启
Esc 关机
Enter 发送
Delete 清空特征
Control 默认工控机已开机
控制参数表:
101: 打开激光雷达
102: 打开惯导
103: 打开红外相机
104: 开始记录ROS包
105: ROS包记录结束
106: 播放ROS包
107: 开始检测火
201: 关闭激光雷达
202: 关闭惯导
203: 关闭红外相机
222: 复位
223: 工控机重启
224: 工控机开机验证
225: 工控机关机
说明:
1.只有收到返回的数据才能启动/关闭成
功;
2.除了“记录ROS包、结束记录”外,其他
按钮启动和关闭均由一个键控制;
3.开始检测火点后雷达、惯导、红外相机将
无法点击;
4.复位不会关闭地图或清空特征;
5.223:重启工控机,此时其他发送指令的
按钮将失效,其快捷键为F1,224:验证工
控机是否处于开机状态,225:工控机关
机,其快捷键为Esc。
[注意:]每次打开串口都会检测工控机是否开
机,只有接收到224时才能进行其他操
作,所以脱离工控机调试时,每次点击打
开串口按钮都需要另一设备发送数据
224,或者点击键盘ctrl来默认工控机已
打开。
完整工程下载
链接:https://download.csdn/download/xx970829/13199234
(有源代码,及已打包程序–解压后可直接运行)
问题解决
未找到头文件:
#include<qgsmaplayerregistry.h>
#include
解决:
将两个头文件改为:
#include<qgsmaplayerstore.h>
#include <QtSvg/QGraphicsSvgItem>
效果
打开地图->快捷键m
(初来乍到,望大佬勿喷)
ico图标下载链接:https://download.csdn/download/xx970829/13986687
svg图标下载链接:https://download.csdn/download/xx970829/13105443
版权声明:本文标题:[902项目]远程控制界面程序:win10+vs+qt+qgis 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1726779023a1084163.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论