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

第四个Qt开发实例(为Label组件添加显示的文字)

引言和代码实现说明

本文在上篇博文 https://blog.csdn.net/wenhao_ir/article/details/145499068的基础上为Label标签添加具体的显示内容。实现的方法其实非常简单,直接调用类Ui::MainWindow的对象ui中的label成员中的方法setText就行了,即只需要在上篇博文的基础上添加下面两行代码就行了:

    // 设置 label 和 label_2 的文本
    ui->label->setText("SuWenhao");
    ui->label_2->setText("WangHong");

在这里插入图片描述

完整源代码

文件mainwindow.ui 中的代码

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>130</y>
      <width>89</width>
      <height>25</height>
     </rect>
    </property>
    <property name="text">
     <string>LED</string>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>260</x>
      <y>80</y>
      <width>71</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>260</x>
      <y>160</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

文件led.h中的代码

#ifndef LED_H
#define LED_H

void led_init(void);
void led_control(int on);

#endif // LED_H

文件mainwindow.h中的代码

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

文件led.cpp中的代码

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <QDebug>

static int fd;

void led_init(void)
{
    fd = open("/dev/imx6ull_led0", O_RDWR);
    if (fd < 0)
    {
        qDebug()<<"open /dev/imx6ull_led0 failed";
    }
}

void led_control(int on)
{
    char status;

    // 因为int类型是占4个字节,而我只向驱动空间写入1个字节,正好char类型只占一个字节,所以这里要强制转换一下
    // 当status为1时,驱动程序会在对应的GPIO口输出低电平,此时灯亮,反之灯灭
    status = (char)on;
    write(fd, &status, 1);

}

文件main.cpp中的代码

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "led.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    static int status = 1;

    if (status)
        qDebug()<<"LED clicked on";
    else
        qDebug()<<"LED clicked off";

    /* 2. control LED */
    led_control(status);

    status = !status;
}

文件mainwindow.cpp中的代码

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "led.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 设置 label 和 label_2 的文本
    ui->label->setText("SuWenhao");
    ui->label_2->setText("WangHong");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    static int status = 1;

    if (status)
        qDebug()<<"LED clicked on";
    else
        qDebug()<<"LED clicked off";

    /* 2. control LED */
    led_control(status);

    status = !status;
}

编译工程生成可执行程序

为了确保是新生成的可执行程序,所以把之前在别的博文中生成的可执行程序删掉~
在这里插入图片描述
然后编译:
在这里插入图片描述
生成了新的ELF可执行文件:
在这里插入图片描述
把生成的可执行文件和LED的驱动程序都放到NFS网络文件目录中,备用:
备注:其实如果只是实现标签显示的话是不需要LED的驱动程序,但因为本文是在上一篇博文的基础上实现的,所以就需要。
在这里插入图片描述

上板测试

打开串口→启动开发板

打开串口终端→打开开发板

关掉开发板上自带的QT的GUI

经实测,如果不关闭开发板自带的QT的GUI,虽然你自己写的Qt界面能加载出来,但是当你用手划动屏幕时,开发板上自带的QT的GUI有可能会弹出来。

参考博文 https://blog.csdn.net/wenhao_ir/article/details/144591685 用一次性有效的方法(即不是永久有效的方法)关掉自带的QT的GUI界面。

这里用一次性的方法,即不是永久有效的方法:
执行下面这条命令:

/etc/init.d/S99myirhmi2 stop

执行完成后再用手去操作屏幕上的UI界面,UI界面就没有任何反应了,说明QT的GUI界面被关掉了

加载驱动程序

挂载网络文件系统:

mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs /mnt

加载驱动程序

insmod /mnt/qt_label/led_driver.ko

在这里插入图片描述

设置Qt环境变量

export QT_QPA_GENERIC_PLUGINS=tslib:/dev/input/event1
export QT_QPA_PLATFORM=linuxfb:fb=/dev/fb0
export QT_QPA_FONTDIR=/usr/lib/fonts/

这三条命令的详细解释见 https://blog.csdn.net/wenhao_ir/article/details/145433648

运行编译生成的Qt程序

注意:运行前请确保Qt运行的环境变量设置好了。
注意:运行前请确保Qt运行的环境变量设置好了。

/mnt/qt_label/test_01

在这里插入图片描述
可见,Label上显示了我们想要的字符串,只是由于Label组件的大小不够,所以没有显示完整。

附编译完成后完整的工程目录

https://pan.baidu.com/s/1Av4kxm6TYJmqC2VwLR25gQ?pwd=yy7b
注意:QtCreator的工程换位置后一定要更换一下Build directory的位置,因为在QtCreator中Build directory是一个绝对路径,详情见 https://blog.csdn.net/wenhao_ir/article/details/145458743


http://www.kler.cn/a/540236.html

相关文章:

  • GitHub分支与标签完全指南:从入门到高效管理
  • 电脑重启后vscode快捷方式失效,找不到code.exe
  • 人工智能浪潮下脑力劳动的变革与重塑:挑战、机遇与应对策略
  • 排序合集(一)
  • 蓝耘智算平台与DeepSeek R1模型:推动深度学习发展
  • 使用 Apache Spark 进行大数据分析
  • 【机器学习与数据挖掘实战】案例13:基于BP神经网络模型的家用热水器用户行为分析与事件识别
  • 哪些情况会导致JVM内存泄露
  • qt制作一个png格式转ico格式的工具
  • YOLOv11-ultralytics-8.3.67部分代码阅读笔记-metrics.py
  • AGI的基石:什么是机器学习
  • 【DeepSeek × Postman】请求回复
  • UP-VLA:具身智体的统一理解与预测模型
  • USB子系统学习(四)用户态下使用libusb读取鼠标数据
  • 深度学习-与OCR结合
  • react脚手架搭建react项目使用scss
  • windows 边框函数 画笔
  • MIT6.824 Lecture 2-RPC and Threads Lecture 3-GFS
  • Post-trained猜想
  • 长安汽车发布“北斗天枢2.0”计划,深蓝汽车普及全民智驾
  • 安装 Ollama 需要哪些步骤?(windows+mac+linux+二进制+Docker)
  • 云原生(五十四) | RDS数据导入与导出
  • Jenkins设置防火墙规则允许访问本机IP端口
  • 面试高频题拆解
  • 如果一个服务器突然间变的很卡,该如何排查?
  • 局域网内别的电脑怎么连接到对方的mysql数据库