基于串口收发数据的温度检测Qt界面程序

基于串口收发的温度检测qt界面

温度变化曲线

温度数据显示

实时温度,由于这里并没有用串口连接单片机,所以没有数据显示

室温数据分析则是找出温度最大值最小值显示出来

温差变化曲线和更新进度功能没有具体地功能的实现

RW.pro文件

QT       += serialport
QT       += sql
QT       += charts

connect.h文件(连接数据库)

#ifndef CONNECT_H
#define CONNECT_H
#include<QtSql/QSqlDatabase>
#include<QtSql/QSqlQuery>
#include<QtSql/QSqlRecord>
#include<QtDebug>

static bool connectSQL(){
    QSqlDatabase tempData=QSqlDatabase::addDatabase("QMYSQL");

    tempData.setHostName("localhost");
    tempData.setDatabaseName("receivetemp");
    tempData.setUserName("root");
    tempData.setPort(3306);
    tempData.setPassword("123456");

    if(!tempData.open()){
        return false;
    }
    return true;
}


#endif // CONNECT_H

view.h和view.cpp文件(温度变化曲线表格的绘制)

view.h

#ifndef VIEW_H
#define VIEW_H
#include<QtWidgets/QGraphicsView>
#include<QtCharts/QChartGlobal>
#include<QtCharts/QChartView>

QT_BEGIN_NAMESPACE
class QMouseEvent;
class QResizeEvent;
class QGraphicsScene;
QT_END_NAMESPACE

QT_CHARTS_BEGIN_NAMESPACE
class QChart;
QT_CHARTS_END_NAMESPACE

QT_CHARTS_USE_NAMESPACE

class View : public QGraphicsView
{
public:
    View(QWidget *parent=nullptr);
    void getData(QString str);

protected:
    void resizeEvent(QResizeEvent *event);
    void mouseMoveEvent(QMouseEvent *event);

private:
    QGraphicsSimpleTextItem *timeX;
    QGraphicsSimpleTextItem *tempY;

    QChart *chart;

    QChartView *chartView;
};

#endif // VIEW_H

view.cpp

#include "view.h"
#include"connect.h"
#include<QtGui/QResizeEvent>
#include<QtGui/QMouseEvent>
#include<QtWidgets/QGraphicsScene>
#include<QtWidgets/QGraphicsSimpleTextItem>
#include<QtCharts/QSplineSeries>
#include<QtCharts/QDateTimeAxis>
#include<QtCharts/QValueAxis>
#include<QtCharts/QChart>
#include<QtCharts/QChartView>
#include<QDateTime>

View::View(QWidget *parent)
    :QGraphicsView (ne
  • 12
    点赞
  • 144
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
要将串口数据输出到Qt界面,你可以按照以下步骤进行操作: 1. 在你的界面类的头文件中添加一个槽函数,用于处理串口接收到数据后的逻辑。例如,可以在名为MainWindow的类中添加一个名为onSerialDataReceived的槽函数: ```cpp class MainWindow : public QMainWindow { Q_OBJECT public slots: void onSerialDataReceived(); }; ``` 2. 在界面类的实现文件中实现onSerialDataReceived槽函数,并在其中读取串口接收到的数据,并将其输出到界面上。假设你已经创建并打开了QSerialPort对象,并且连接了readyRead信号,可以这样实现: ```cpp void MainWindow::onSerialDataReceived() { QByteArray data = serialPort->readAll(); // 读取串口接收到的数据 // 在界面上显示接收到的数据 ui->textEdit->append(QString(data)); } ``` 3. 在界面类的构造函数中连接串口的readyRead信号与槽函数: ```cpp MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { // 创建并打开串口对象 serialPort = new QSerialPort(this); serialPort->setPortName("COM1"); // 设置串口端口号 serialPort->open(QIODevice::ReadOnly); // 连接串口的readyRead信号与槽函数 connect(serialPort, &QSerialPort::readyRead, this, &MainWindow::onSerialDataReceived); } ``` 这样,每当串口接收到数据时,onSerialDataReceived槽函数会被调用,将接收到的数据输出到界面上的TextEdit控件(假设控件名为textEdit)。 希望这个例子对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值