当前位置: 首页 > news >正文

QT | 编写一个简单的上位机

QT | 编写一个简单的上位机

时间:2023-03-19


参考:

1.易懂 | 手把手教你编写你的第一个上位机

2.QT中修改窗口的标题和图标

3.图标下载

1.打开QT Creator

在这里插入图片描述

2.新建工程

Qt Creator 可以创建多种项目,在最左侧的列表框中单击“Application”,中间的列表框中列出了可以创建的应用程序的模板,各类应用程序如下:

  • Qt Widgets Application,支持桌面平台的有图形用户界面Graphic User Interface,GUI) 界面的应用程序。GUI 的设计完全基于 C++ 语言,采用 Qt 提供的一套 C++ 类库。
  • Qt Console Application控制台应用程序,无 GUI 界面,一般用于学习 C/C++ 语言,只需要简单的输入输出操作时可创建此类项目。
  • Qt Quick Application,创建可部署的 Qt Quick 2 应用程序。Qt QuickQt 支持的一套 GUI 开发架构,其界面设计采用 QML 语言,程序架构采用 C++ 语言。利用 Qt Quick 可以设计非常炫的用户界面,一般用于移动设备或嵌入式设备上无边框的应用程序的设计。
  • Qt Quick Controls 2 Application,创建基于 Qt Quick Controls 2 组件的可部署的 Qt Quick 2 应用程序。Qt Quick Controls 2 组件只有 Qt 5.7 及以后版本才有。
  • Qt Canvas 3D Application,创建 Qt Canvas 3D QML 项目,也是基于 QML 语言的界面设计,支持 3D 画布。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

如下图:在此界面中选择需要创建界面的基类(base class)。有 3 种基类可以选择:

  1. QMainWindow 是主窗口类,主窗口具有主菜单栏、工具栏和状态栏,类似于一般的应用程序的主窗口;
  2. QWidget 是所有具有可视界面类的基类,选择 QWidget 创建的界面对各种界面组件都可以 支持;
  3. QDialog 是对话框类,可建立一个基于对话框的界面;

在此选择 QMainWindow 作为基类,自动更改的各个文件名不用手动去修改。勾选“创建界面”复选框。这个选项如果勾选,就会由 Qt Creator 创建用户界面文件,否则,需要自己编程手工创建界面。初始学习,为了了解 Qt Creator 的设计功能,勾选此选项。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

3.上位机界面设计

3-1.界面布局

双击“widget.ui”文件。进入可视化设计界面。

在这里插入图片描述

使用拖曳的形式从左侧拖出控件摆放在画布上。

在这里插入图片描述

在这里插入图片描述

3-2.修改窗口标题

在这里插入图片描述

3-3.修改窗口图标

鼠标选中工程,右键,然后选择“添加新文件”;

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

打开“myres.qrc”文件,选择:添加-》添加前缀;前缀改为“/”;

在这里插入图片描述

在这里插入图片描述

3-4.重新布局

3-4-1.打破布局

在这里插入图片描述

3-4-2.选择栅格布局

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

4.上位机逻辑编写

QT += core gui serialport

#-------------------------------------------------
#
# Project created by QtCreator 2023-03-19T22:52:53
#
#-------------------------------------------------QT       += core gui serialportgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = serial_led
TEMPLATE = app# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \widget.cppHEADERS += \widget.hFORMS += \widget.uiRESOURCES += \res/myres.qrc

widget.hWidget类中添加一个QSerialPort成员:

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QSerialPort>
#include <QSerialPortInfo>namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private slots:void on_onButton_clicked();void on_offButton_clicked();void on_openButton_clicked();void on_closeButton_clicked();private:Ui::Widget *ui;QSerialPort *serialPort;
};#endif // WIDGET_H

#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{QStringList serialNamePort;ui->setupUi(this);this->setWindowTitle("serial_led");/* 创建一个串口对象 */serialPort = new QSerialPort(this);/* 搜索所有可用串口 */foreach (const QSerialPortInfo &inf0, QSerialPortInfo::availablePorts()) {serialNamePort<<inf0.portName();}ui->serialBox->addItems(serialNamePort);
}Widget::~Widget()
{delete ui;
}void Widget::on_onButton_clicked()
{/* 串口设置 */serialPort->setPortName(ui->serialBox->currentText());serialPort->setBaudRate(ui->baudrateBox->currentText().toInt());serialPort->setDataBits(QSerialPort::Data8);serialPort->setStopBits(QSerialPort::OneStop);serialPort->setParity(QSerialPort::NoParity);/* 打开串口提示框 */if (true == serialPort->open(QIODevice::ReadWrite)){QMessageBox::information(this, "提示", "串口打开成功");}else{QMessageBox::critical(this, "提示", "串口打开失败");}
}void Widget::on_offButton_clicked()
{serialPort->close();
}void Widget::on_openButton_clicked()
{serialPort->write("ON\n");qDebug("ON\n");
}void Widget::on_closeButton_clicked()
{serialPort->write("OFF\n");qDebug("OFF\n");
}
http://www.lryc.cn/news/41390.html

相关文章:

  • DirectX12(D3D12)基础教程(二十一)—— PBR:IBL 的数学原理(2/5)
  • 嵌入式学习笔记——SysTick(系统滴答)
  • Linux实操之服务管理
  • 基于Java+SpringBoot+vue的毕业生信息招聘平台设计和实现【源码+论文+演示视频+包运行成功】
  • 智能生活垃圾检测与分类系统(UI界面+YOLOv5+训练数据集)
  • 建立农村污水处理设施已经成为了当务之急!
  • 【Matlab算法】粒子群算法求解一维线性函数问题(附MATLAB代码)
  • 【JavaEE】Thread 类及常用方法
  • C语言数据结构初阶(7)----队列
  • 代码随想录二刷 day01 | 704. 二分查找 27. 移除元素 977. 有序数组的平方
  • Linux 终端、进程组、会话、守护进程
  • 你是否有潜质成为谷歌开发者专家?加入 GDE 成长计划,释放潜力!
  • 安全防御之防火墙篇(二)
  • 设计必备,5个png免抠素材网站,建议收藏
  • shell 脚本expect
  • 第十九天 Maven总结
  • ESP8266-NodeMCU开发板-------开发板介绍(1)
  • 【测试开发篇3】软件测试的常用概念
  • javaEE初阶 — JavaScript WebAPI
  • UE实现地面动态交互效果
  • 如何用自己的数据训练YOLOv5
  • 【基础算法】数组相关题目
  • MatBox—基于PyQt快速入门matplotlib的教程库
  • go channel使用
  • 5. QtDesignStudio中的3D场景
  • 人工智能的几个研究方向
  • 软件测试 - 常见的开发模型和测试模型
  • 从零开始的机械臂yolov5抓取gazebo仿真(四)
  • C++修炼之筑基期第一层——认识类与对象
  • IT 运营监控工具