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

相关文章

Spring Gateway动态路由实现方案

《SpringGateway动态路由实现方案》本文主要介绍了SpringGateway动态路由实现方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录前沿何为路由RouteDefinitionRouteLocator工作流程动态路由实现尾巴前沿S

Python绘制TSP、VRP问题求解结果图全过程

《Python绘制TSP、VRP问题求解结果图全过程》本文介绍用Python绘制TSP和VRP问题的静态与动态结果图,静态图展示路径,动态图通过matplotlib.animation模块实现动画效果... 目录一、静态图二、动态图总结【代码】python绘制TSP、VRP问题求解结果图(包含静态图与动态图

Python动态处理文件编码的完整指南

《Python动态处理文件编码的完整指南》在Python文件处理的高级应用中,我们经常会遇到需要动态处理文件编码的场景,本文将深入探讨Python中动态处理文件编码的技术,有需要的小伙伴可以了解下... 目录引言一、理解python的文件编码体系1.1 Python的IO层次结构1.2 编码问题的常见场景二

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

QT Creator配置Kit的实现示例

《QTCreator配置Kit的实现示例》本文主要介绍了使用Qt5.12.12与VS2022时,因MSVC编译器版本不匹配及WindowsSDK缺失导致配置错误的问题解决,感兴趣的可以了解一下... 目录0、背景:qt5.12.12+vs2022一、症状:二、原因:(可以跳过,直奔后面的解决方法)三、解决方

Qt中实现多线程导出数据功能的四种方式小结

《Qt中实现多线程导出数据功能的四种方式小结》在以往的项目开发中,在很多地方用到了多线程,本文将记录下在Qt开发中用到的多线程技术实现方法,以导出指定范围的数字到txt文件为例,展示多线程不同的实现方... 目录前言导出文件的示例工具类QThreadQObject的moveToThread方法实现多线程QC

go动态限制并发数量的实现示例

《go动态限制并发数量的实现示例》本文主要介绍了Go并发控制方法,通过带缓冲通道和第三方库实现并发数量限制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录带有缓冲大小的通道使用第三方库其他控制并发的方法因为go从语言层面支持并发,所以面试百分百会问到

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

Qt QCustomPlot库简介(最新推荐)

《QtQCustomPlot库简介(最新推荐)》QCustomPlot是一款基于Qt的高性能C++绘图库,专为二维数据可视化设计,它具有轻量级、实时处理百万级数据和多图层支持等特点,适用于科学计算、... 目录核心特性概览核心组件解析1.绘图核心 (QCustomPlot类)2.数据容器 (QCPDataC