Qt绘制动态罗盘

2024-03-05 18:04
文章标签 动态 qt 绘制 罗盘

本文主要是介绍Qt绘制动态罗盘,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述
在这里插入图片描述

介绍:罗盘指针以30°角旋转巡逻,扫描航海范围内的点位,并绘制点云。字段信息在表格中显示,该数据都存储在数据库中。选择不同的范围,显示该范围内的点位。

#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);initSystem();initQTableView();initQCheckBox();initDataBase();
}MainWindow::~MainWindow()
{delete ui;m_myDatabase.close();
}void MainWindow::initSystem()
{sourceDataList.clear();m_RefreshBtn    = new QPushButton("刷新",this);m_EscalationBtn = new QPushButton("上报",this);m_ResettingBtn  = new QPushButton("重置",this);m_model       = new QStandardItemModel(2,FixedColumnCount,this);m_selectModel = new QItemSelectionModel(m_model,this);ui->statusbar->addPermanentWidget(m_RefreshBtn);ui->statusbar->addPermanentWidget(m_EscalationBtn);ui->statusbar->addPermanentWidget(m_ResettingBtn);ui->statusbar->setSizeGripEnabled(false);//去掉状态栏右下角的三角ui->tableViewTarget->setModel(m_model);ui->tableViewTarget->setSelectionModel(m_selectModel);ui->tableViewTarget->setSelectionMode(QAbstractItemView::ExtendedSelection);ui->tableViewTarget->setSelectionBehavior(QAbstractItemView::SelectItems);ui->tableViewTarget->verticalHeader()->hide();ui->tableViewTarget->setEditTriggers(QAbstractItemView::NoEditTriggers);// 设置行表头自适应调整大小ui->tableViewTarget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}void MainWindow::initQTableView()
{QStringList headerList;headerList<<"批号"<<"目标方位"<<"目标距离"<<"目标属性"<<"目标种类"<<"目标航向";m_model->setHorizontalHeaderLabels(headerList);
}void MainWindow::initQCheckBox()
{m_InTheAir       = new QCheckBox(INDEXAIRSTR);m_SurfaceOfWater = new QCheckBox(INDEXSURFACEWATER);m_underwater     = new QCheckBox(INDEXUNDEXWATER);m_Shore          = new QCheckBox(INDEXSHORE);//开启三态选择m_InTheAir->setTristate(true);m_SurfaceOfWater->setTristate(true);m_underwater->setTristate(true);m_Shore->setTristate(true);//设置初始状态m_InTheAir->setCheckState(Qt::Checked);m_SurfaceOfWater->setCheckState(Qt::Checked);m_underwater->setCheckState(Qt::Checked);m_Shore->setCheckState(Qt::Checked);QString styleSheet ="QCheckBox::indicator:indeterminate {""border-image: url(:/checkbox_dark_normal.png);"  // 半选中状态的图片"}""QCheckBox::indicator:unchecked {""border-image: url(:/checkbox_dark_hover.png);"  // 未选中状态的图片"}""QCheckBox::indicator:checked {""    background-image: url(:/checkbox_dark_pressed.png);"  // 选中状态的图片"}""QCheckBox::indicator {""    width: 20px;"  // 设置复选框宽度"    height: 20px;"  // 设置复选框高度"}";// 设置样式表,使用本地图片作为背景m_InTheAir->setStyleSheet(styleSheet);m_SurfaceOfWater->setStyleSheet(styleSheet);m_underwater->setStyleSheet(styleSheet);m_Shore->setStyleSheet(styleSheet);QHBoxLayout *alayout = new QHBoxLayout;alayout->addWidget(m_InTheAir);alayout->addWidget(m_SurfaceOfWater);alayout->addWidget(m_underwater);alayout->addWidget(m_Shore);ui->widgetInsertCheckBox-> setLayout(alayout);connect(m_InTheAir,&QCheckBox:: stateChanged,this, &MainWindow::do_checkBoxAirSlot);connect(m_SurfaceOfWater,&QCheckBox:: stateChanged,this, &MainWindow::do_checkBoxSurfaceWaterSlot);connect(m_underwater,&QCheckBox:: stateChanged,this, &MainWindow::do_checkBoxUnderWaterSlot);connect(m_Shore,&QCheckBox:: stateChanged,this, &MainWindow::do_checkBoxShoreSlot);
}void MainWindow::setQtableViewIndex(QString indexName, int &state)
{if(state == Qt::Checked){//选中int rowTotal = sourceDataList.size();for (int row = 0; row < rowTotal ; row++) {QString aLineText = sourceDataList.at(row);//获取一行数据QStringList tempList = aLineText.split(" ");//把一行数据,用空格隔开if(aLineText.contains(indexName) == true){m_model->insertRow(m_model->rowCount(), QList<QStandardItem*>()<< new QStandardItem(tempList.at(0))<< new QStandardItem(tempList.at(1))<< new QStandardItem(tempList.at(2))<< new QStandardItem(tempList.at(3))<< new QStandardItem(tempList.at(4))<< new QStandardItem(tempList.at(5)));}}//创建排序和过滤代理QSortFilterProxyModel*   proxyModel = new QSortFilterProxyModel(this);// 创建过滤器proxyModel->setSourceModel(m_model);proxyModel->setFilterKeyColumn(0);proxyModel->sort(0, Qt::AscendingOrder);// 设置表格视图的模型为过滤器ui->tableViewTarget->setModel(proxyModel);}else if(state == Qt::Unchecked){//未选择// 删除满足条件的行for (int row = 0; row < m_model->rowCount(); ++row) {if (m_model->item(row, 4)->text() == indexName) {m_model->removeRow(row);// 因为删除了一行,所以需要重新检查当前行--row;}}}else if(state == Qt::PartiallyChecked){//半选中//no deal with}undatePointClouds();
}void MainWindow::do_checkBoxAirSlot(int state)
{setQtableViewIndex(INDEXAIRSTR,state);
}void MainWindow::do_checkBoxSurfaceWaterSlot(int state)
{setQtableViewIndex(INDEXSURFACEWATER,state);
}void MainWindow::do_checkBoxUnderWaterSlot(int state)
{setQtableViewIndex(INDEXUNDEXWATER,state);
}void MainWindow::do_checkBoxShoreSlot(int state)
{setQtableViewIndex(INDEXSHORE,state);
}void MainWindow::initDataBase()
{//安装驱动m_myDatabase = QSqlDatabase::addDatabase("QSQLITE");//给数据库取名字m_myDatabase.setDatabaseName("situation.db");//打开数据库if(!m_myDatabase.open()){qDebug() << "Error to open database" << m_myDatabase.lastError();return;}createDataBase();
}//创建数据库
void MainWindow::createDataBase()
{QSqlQuery query;if(!query.exec("create table if not exists situation(batchNumber int, targetBeare double ,""targetDistance double, targetProperties text, targetType text, targetCourse int);")){qDebug() << "Create Table is error" << m_myDatabase.lastError();return;}serchDataBase();
}//插入数据库数据  【如果后期需要插入数据,可以调用此接口】
void MainWindow::insertDataBase(int &batchNum,double &targetBear,double &targetDist,QString &targetPro,QString &targetType,int & targetCourse)
{QSqlQuery query = QSqlQuery(m_myDatabase);QString cmd = QString("insert into situation values(\"%1\",\"%2\",\"%3\",\"%4\",\"%5\",\"%6\");").arg(batchNum).arg(targetBear).arg(targetDist).arg(targetPro).arg(targetType).arg(targetCourse);if(!query.exec(cmd)){qDebug() << "Error to Insert Into Database " << m_myDatabase.lastError();return;}
}void MainWindow::undatePointClouds()
{m_X.clear();m_Y.clear();for (int row = 0; row < m_model->rowCount(); ++row) {QModelIndex index1 = m_model->index(row, 1, QModelIndex()); // 第2列QModelIndex index2 = m_model->index(row, 2, QModelIndex()); // 第3列QVariant data1 = m_model->data(index1, Qt::DisplayRole);QVariant data2 = m_model->data(index2, Qt::DisplayRole);m_X.push_back(data1.toDouble());m_Y.push_back(data2.toDouble());}ui->widget->initQCustomPlot(ui->widget,m_X,m_Y);
}//查询数据库成员
void MainWindow::serchDataBase()
{QSqlQuery query = QSqlQuery(m_myDatabase);QString cmd = QString("select * from situation");//查询数据库if(!query.exec(cmd))//判断数据库是否为空{qDebug() << "Error Select to Database" << m_myDatabase.lastError();return;}QSqlRecord record = query.record();//获取当前记录QStandardItem *aItem;int i = 0;//在表格中显示信息while (query.next()){QString aLine = "";aLine.clear();for (int j = 0; j < record.count(); ++j) {aItem = new QStandardItem(query.value(j).toString());m_model->setItem(i,j,aItem);aLine.append(query.value(j).toString());aLine.append(" ");}sourceDataList.append(aLine);i++;}undatePointClouds();
}

这篇关于Qt绘制动态罗盘的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/777361

相关文章

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++