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

相关文章

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

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

Qt QCustomPlot库简介(最新推荐)

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

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

springboot如何通过http动态操作xxl-job任务

《springboot如何通过http动态操作xxl-job任务》:本文主要介绍springboot如何通过http动态操作xxl-job任务的问题,具有很好的参考价值,希望对大家有所帮助,如有错... 目录springboot通过http动态操作xxl-job任务一、maven依赖二、配置文件三、xxl-

Qt如何实现文本编辑器光标高亮技术

《Qt如何实现文本编辑器光标高亮技术》这篇文章主要为大家详细介绍了Qt如何实现文本编辑器光标高亮技术,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录实现代码函数作用概述代码详解 + 注释使用 QTextEdit 的高亮技术(重点)总结用到的关键技术点应用场景举例示例优化建议

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

Java调用C#动态库的三种方法详解

《Java调用C#动态库的三种方法详解》在这个多语言编程的时代,Java和C#就像两位才华横溢的舞者,各自在不同的舞台上展现着独特的魅力,然而,当它们携手合作时,又会碰撞出怎样绚丽的火花呢?今天,我们... 目录方法1:C++/CLI搭建桥梁——Java ↔ C# 的“翻译官”步骤1:创建C#类库(.NET

MyBatis编写嵌套子查询的动态SQL实践详解

《MyBatis编写嵌套子查询的动态SQL实践详解》在Java生态中,MyBatis作为一款优秀的ORM框架,广泛应用于数据库操作,本文将深入探讨如何在MyBatis中编写嵌套子查询的动态SQL,并结... 目录一、Myhttp://www.chinasem.cnBATis动态SQL的核心优势1. 灵活性与可

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

Mybatis嵌套子查询动态SQL编写实践

《Mybatis嵌套子查询动态SQL编写实践》:本文主要介绍Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、实体类1、主类2、子类二、Mapper三、XML四、详解总结前言MyBATis的xml文件编写动态SQL