1、实验准备
在PC 机D:盘下创建文件夹qt-led,将光盘qt_led_exp 文件夹下的images 文件夹拷贝到E:盘下qt-led 文件夹qt-led 内
2、新建工程
新建一个Empty QT4 Project 工程,如下:
点击:File >>> New File >>> Empty Qt4 xxx
保存到D:盘下,起名叫qt-led,如下所示:
3、添加资源文件(图像)
右键点击工程文件qt_led->add new…->Qt Resource File,如下所示:
指定保存位置D:\qt-led 及文件名images,如下所示:(我的一不小心弄错了目录,成了D:\qt-led\\qt-led)
选择添加到工程,确定会出现如下界面:
点击下方的Add->Add Prefix,添加前缀,将默认值/new/prefix1 改为/,如下:
点击下方的Add->Add Prefix,添加文件,选择images 文件夹下的所有文件,如下:
添加完成后,如下显示:
然后执行”ctrl+s”进行保存。
4、创建UI界面
新建一个UI Add New…->Qt Designer Form,如下所示:
选择widget 模板,如下所示:
设置FORM 的名称为led.ui,保存位置D:\qt-led,如下:
点击窗体,设定大小为800*480,窗体名称更改为Led,如下所示:
添加标签,文本为:led 灯控制实例,objectName 为label;
添加标签,清空文本,objectName 为led1_lb,设置大小为128*128,如下所示:
为标签添加背景图片,右键点击标签,选择Change styleSheet…,如下所示:
选择添加背景图片,如下所示:
选中off.png 图像文件,如下所示:
添加完成以后,标签背景已经改变,如下:
复制另外3 个标签,起名分别为led2_lb 和led3_lb,如下所示:
添加group Box 控件,文本:开关控制,大小600*200,摆放在标签的下方,如下:
添加一个Push Button 按钮控件,文本为点亮,objectName 为led1_on,大小为90*45,
点中按钮,在右侧属性设置中找到icon 项,添加图标文件为on.png,添加后如下显
示:
同样复制另外两个led 控制点亮按钮,objectName 分别为led2_on 和led3_on,如下:
添加一个Push Button 按钮控件,文本为熄灭,objectName 为led1_off,大小为90*45,
点中按钮,在右侧属性设置中找到icon 项,添加图标文件为off.png,添加后如下显
示:
同样复制另外两个led 控制熄灭按钮,objectName 分别为led2_off 和led3_off,如下:
图标下方添加三个标签,起名分别为status_lb1、status_lb2 和status_lb3,如下:
界面设计完成,”ctrl+s”保存:
5、添加头文件
添加一个c++头文件,如下:
起名led.h,保存到D:\qt-led,如下:
编辑内容如下:
#include "led.h" #include "ui_led.h" MyLed::MyLed(QWidget *parent) : QWidget(parent),ui(new Ui::Led) { ui->setupUi(this); ui->led1_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/off.jpg);")); ui->led2_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/off.jpg);")); ui->led3_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/off.jpg);")); connect(ui->led1_on,SIGNAL(clicked()),this,SLOT(on_led1_on_clicked())); connect(ui->led1_off,SIGNAL(clicked()),this,SLOT(on_led1_off_clicked())); connect(ui->led2_on,SIGNAL(clicked()),this,SLOT(on_led2_on_clicked())); connect(ui->led2_off,SIGNAL(clicked()),this,SLOT(on_led2_off_clicked())); connect(ui->led3_on,SIGNAL(clicked()),this,SLOT(on_led3_on_clicked())); connect(ui->led3_off,SIGNAL(clicked()),this,SLOT(on_led3_off_clicked())); fd = open("/dev/leds", 0); if (fd < 0) { qDebug("open device /dev/leds"); //exit(1); } } MyLed::~MyLed() { delete ui; ::close(fd); } void MyLed::changeEvent(QEvent *e) { QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } void MyLed::on_led1_on_clicked() { ioctl(fd, LED_IOC_WR_ON, 1); ui->status_lb1->setText("LED1 ON"); ui->led1_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/led1.jpg);")); } void MyLed::on_led1_off_clicked() { ioctl(fd, LED_IOC_WR_OFF, 1); ui->status_lb1->setText("LED1 OFF"); ui->led1_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/off.jpg);")); } void MyLed::on_led2_on_clicked() { ioctl(fd, LED_IOC_WR_ON, 2); ui->status_lb2->setText("LED2_ON"); ui->led2_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/led2.jpg);")); } void MyLed::on_led2_off_clicked() { ioctl(fd, LED_IOC_WR_OFF, 2); ui->status_lb2->setText("LED2 OFF"); ui->led2_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/off.jpg);")); } void MyLed::on_led3_on_clicked() { ioctl(fd, LED_IOC_WR_ON, 3); ui->status_lb3->setText("LED3 ON"); ui->led3_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/led3.jpg);")); } void MyLed::on_led3_off_clicked() { ioctl(fd, LED_IOC_WR_OFF, 3); ui->status_lb3->setText("LED3 OFF"); ui->led3_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/off.jpg);")); }
完成,”ctrl+s”保存
6、添加源文件
添加一个c++源文件,如下:
起名led.cpp,保存到D:\qt-led,如下:
编辑文本如下:
#include "led.h" #include "ui_led.h" MyLed::MyLed(QWidget *parent) : QWidget(parent),ui(new Ui::Led) { ui->setupUi(this); ui->led1_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/off.jpg);")); ui->led2_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/off.jpg);")); ui->led3_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/off.jpg);")); connect(ui->led1_on,SIGNAL(clicked()),this,SLOT(on_led1_on_clicked())); connect(ui->led1_off,SIGNAL(clicked()),this,SLOT(on_led1_off_clicked())); connect(ui->led2_on,SIGNAL(clicked()),this,SLOT(on_led2_on_clicked())); connect(ui->led2_off,SIGNAL(clicked()),this,SLOT(on_led2_off_clicked())); connect(ui->led3_on,SIGNAL(clicked()),this,SLOT(on_led3_on_clicked())); connect(ui->led3_off,SIGNAL(clicked()),this,SLOT(on_led3_off_clicked())); fd = open("/dev/leds", 0); if (fd < 0) { qDebug("open device /dev/leds"); //exit(1); } } MyLed::~MyLed() { delete ui; ::close(fd); } void MyLed::changeEvent(QEvent *e) { QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } void MyLed::on_led1_on_clicked() { ioctl(fd, LED_IOC_WR_ON, 1); ui->status_lb1->setText("LED1 ON"); ui->led1_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/led1.jpg);")); } void MyLed::on_led1_off_clicked() { ioctl(fd, LED_IOC_WR_OFF, 1); ui->status_lb1->setText("LED1 OFF"); ui->led1_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/off.jpg);")); } void MyLed::on_led2_on_clicked() { ioctl(fd, LED_IOC_WR_ON, 2); ui->status_lb2->setText("LED2_ON"); ui->led2_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/led2.jpg);")); } void MyLed::on_led2_off_clicked() { ioctl(fd, LED_IOC_WR_OFF, 2); ui->status_lb2->setText("LED2 OFF"); ui->led2_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/off.jpg);")); } void MyLed::on_led3_on_clicked() { ioctl(fd, LED_IOC_WR_ON, 3); ui->status_lb3->setText("LED3 ON"); ui->led3_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/led3.jpg);")); } void MyLed::on_led3_off_clicked() { ioctl(fd, LED_IOC_WR_OFF, 3); ui->status_lb3->setText("LED3 OFF"); ui->led3_lb->setStyleSheet(QString::fromUtf8("background-image: url(:/images/off.jpg);")); }
完成“ctrl+s”保存
7、添加 main 函数
新建一个C++源文件,保存为main.cpp,如下:
编辑文本内容:
#include <QtGui/QApplication> #include "led.h" #include <QtGui/QApplication> #include "led.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MyLed w; w.showFullScreen(); a.setFont(QFont("unifont",12)); //w.show(); return a.exec(); }
“ctrl+s”保存
工程已经创建完毕,接下来即是交叉编译、下载测试
8、拷贝工程文件到虚拟机
[[email protected] uptech]# mkdir qt-led/
通过samba 服务将E 盘下的qt-led 文件夹拷贝到虚拟机的/home/uptech/qt-led 目录,
同时将光盘中qt_led_exp 目录下的driver 目录也拷贝到此文件如下:
[[email protected] uptech]# cd qt-led/
[[email protected] qt-led]# ls
driver qt-led
[[email protected] qt-led]#
9、编译
[[email protected] qt-led]# cd qt-led/
[[email protected] qt-led]#
/home/gxj/QT/qt4.6/for_arm/qt-everywhere-opensource-src-4.6.2/bin/qmake -project
[[email protected] qt-led]#
/home/gxj/QT/qt4.6/for_arm/qt-everywhere-opensource-src-4.6.2/bin/qmake
[[email protected] qt-led]#make
10、拷贝到共享目录
[[email protected] qt-led]# cp qt-led /mnt/nfs/Trolltech/Qt-embedded-4.6.2-install/
[[email protected] qt-led]# cp ../driver/s3c-leds.ko
/mnt/nfs/Trolltech/Qt-embedded-4.6.2-install/
[[email protected] qt-led]#
11、挂载
/mnt # mount -o nolock,rsize=4096,wsize=4096 192.168.12.95:/mnt/nfs /mnt/nfs
12、加载驱动
/mnt #cd /mnt/nfs/Trolltech/Qt-embedded-4.6.2-install/
/mnt/nfs/Trolltech/Qt-embedded-4.6.2-install # insmod s3c-leds.ko
13、执行测试程序
/mnt/nfs/Trolltech/Qt-embedded-4.6.2-install # ./export4arm.sh qt-led –qws
即可进行测试
原文地址:https://www.cnblogs.com/chuijingjing/p/9222290.html