第四个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