摘要:
通常,我们运行shell程序都是在终端里进行的,而在产品中,不可能叫用户到终端里去敲命令吧;还有,当系统中的某一个计算功能,我在另一程序里已经做好了,想通过GUI程序直接调用它运行。那么,在这些情况下,如何通过GUI上的button来运行shell脚本和linux程序呢。下面将在mini6410上,通过一个范例来介绍如何在QT4 GUI程序里调用shell程序和运行应用程序。
通过本范例,可以测试用linux C库的函数来调用shell脚本和应用程序,也可以通过qt的函数来调用shell脚本和应用程序。还介绍了阻塞运行和非阻塞运行。
介绍:
范例简介:基于mini6410开发平台,用QT4设计一GUI界面HelloQt4,通过HelloQt4界面上button->clicked来启动smplayer播放器。
1、设计gui图形界面,创建一button,并在clicked下添加代码
第一种方法(50行)是调用linux C函数库中的system(const char *string);
第二种方法(51行)和第三种方法(53 54行)是调QT里的函数;
需要说明的时,上面三种方法都是可以的,但前两种方法会阻塞进程,直到smplayer程序结束,而第三种方法则不会阻塞进程,可以多任务运行。
还有,我们知道qt在运行的时候,要启动qws(
qtwindows server
),如果用前面两种方法,则smplayer运行的时候,要新开启一个qws,否则不能运行;而用第三种方法,则不需要再开启qws,它和HelloQt4共用一个qws,这样,在一个窗口里可以看到这两个程序。
2、新建运行脚本,用来启动smplayer播放器
在上一步中,qt程序执行了 /opt/run_smplayer脚本,但这个脚本是不存在的,我们要新建它,打开开发板的/opt目录,新建一个run_smplayer文件,添加如下内容
# !/ bin / sh
#测试用
#cd / opt /
#rm qt_create /
- rf
#sleep 3
#mkdir qt_create
#cd qt_create
#mkdir qt_create1
#cd qt_create1
#mkdir qt_create2
#cd qt_create2
#mkdir ok
#直接调用mplayer播放sdcard里的gq.avi文件
#cd / usr / local / smplayer / bin
#. / mplayer / sdcard / gq.avi
#启动友善的smplayer
cd / usr / local / smplayer / bin
. / smplayer
3、修改smplayer文件
在友善的根文件系统中,/user/lical/smplayer/bin目录中的smplayer脚本是有问题的,如果直接运行它,会出现鼠标和触摸屏不能使用的情况,我们要将/bin目录下的smplayer脚本复制过来,并将最后两行中的qws去掉。(前面提到过的,我们要多任务运行,不需要再开一个qws)
export HOME =/ root
cd / usr / local / smplayer /
exe__c . / smplayer # - qws 1 >/ dev / null
2 >/ dev / null // 此处有屏蔽字,真实没有下划线
#hotplug
4、修改rcS文件,使之开机就运行HelloQt4i程序
rcS文件如下:
# !
/ bin / sh
PATH =/ sbin: / bin: / usr / sbin: / usr / bin: / usr / local / bin:
runlevel = S
prevlevel = N
umask 022
export PATH runlevel prevlevel
#
# Trap CTRL
-
C
&
c only
in
this shell so we can interrupt subprocesses.
#
trap " : " INT QUIT TSTP
/ bin / hostname Crt
[ - e / proc / 1 ]
||
/ bin / mount - n - t proc none
/
proc
[ - e / sys / class ] ||
/ bin / mount - n - t sysfs none / sys
[ - e / dev / tty ]
||
/ bin / mount
-
t ramfs none
/
dev
/ bin / mount - n - t usbfs none / proc / bus / usb
echo / sbin / mdev >
/ proc / sys / kernel / hotplug
/ sbin / mdev - s
/ bin / hotplug
# mounting file system specified in
/ etc / fstab
mkdir - p / dev / pts
mkdir - p / dev / shm
/ bin / mount - n - t devpts none / dev / pts - o mode = 0622
/ bin / mount - n - t tmpfs tmpfs / dev / shm
/ bin / mount - n - t ramfs none / tmp
/ bin / mount - n - t ramfs none / var
mkdir - p / var / empty
mkdir - p / var / log
mkdir - p / var / lock
mkdir - p / var / run
mkdir - p / var / tmp
/ sbin / hwclock - s
syslogd
/ etc / rc.d / init.d / netd start
echo "
"
>
/ dev / tty1
echo " Starting networking... "
>
/ dev / tty1
sleep 1
/ etc / rc.d / init.d / httpd start
echo "
"
>
/ dev / tty1
echo " Starting web server... "
>
/ dev / tty1
sleep 1
/ etc / rc.d / init.d / leds start
echo "
"
>
/ dev / tty1
echo " Starting leds service... "
>
/ dev / tty1
echo "
"
sleep 1
echo "
"
>
/ dev / tty1
/ etc / rc.d / init.d / alsaconf start
echo " Loading sound card config... "
>
/ dev / tty1
echo "
"
/ sbin / ifconfig lo 127.0 . 0.1
/ etc / init.d / ifconfig - eth0
# / bin / qt4 &
#echo "
"
>
/ dev / tty1
#echo " Starting Qt4 Applications, please waiting... "
>
/ dev / tty1
# / bin / smplayer &
#echo "
"
>
/ dev / tty1
#echo " Starting smplayer Applications, please waiting... "
>
/ dev / tty1
/ bin / hello_qt4app &
echo "
"
>
/ dev / tty1
echo " Starting hello_qt4 Applications, please waiting...
"
>
/ dev / tty1
hello_qt4app脚本如下:
# !/ bin / sh
rm / root / .config /
- rf;
rm / root / .mplayer /
- rf;
if [ - e / etc / friendlyarm - ts - input.conf ] ; then
.
/
etc
/
friendlyarm
-
ts
-
input.conf
fi
true ${TSLIB_TSDEVICE:
=/
dev
/
touchscreen}
TSLIB_CONFFILE =/ etc / ts.conf
export TSLIB_TSDEVICE
export TSLIB_CONFFILE
export TSLIB_PLUGINDIR =/ usr / lib / ts
export TSLIB_CALIBFILE =/ etc / pointercal
export QWS_DISPLAY = : 1
export LD_LIBRARY_PATH =/ usr / local / lib:$LD_LIBRARY_PATH
export PATH =/ bin: / sbin: / usr / bin / : / usr / sbin: / usr / local / bin
if [ - c / dev / touchscreen ]; then
export QWS_MOUSE_PROTO
=
"
Tslib:${TSLIB_TSDEVICE} MouseMan:/dev/input/mice
"
if [ - e / etc / pointercal - a !
- s / etc / pointercal ] ; then
rm
/
etc
/
pointercal
fi
else
export QWS_MOUSE_PROTO
=
"
MouseMan:/dev/input/mice
"
>/ etc / pointercal
fi
export QWS_KEYBOARD = TTY: / dev / tty1
export HOME =/ root
cd / opt
. / hello_qt4 - qws 1 >/ dev / null
2 >/ dev / null
hotplug
然后,重启开发板,hello_qt4程序运行了,再点button,播放器也弹出来了。
结语:
本例程简单实用,介绍了函数的用途,没有对枯燥的代码进行深入的分析;对于入门者来说,这些内容应该会让它很容易就接受并应用起来,如果您是高手,更深入的还请自行分析。
参考资料:
QT下执行Shell或启动外部程序
http://wenku.baidu.com/view/33467002bed5b9f3f90f1c5b .html
QT下实现对Linux Shell调用的几种方法 QProcess AND QThread http://www.linuxidc.com/Linux/2010-08/28172.htm
介绍:
![如何在QT下执行shell或外部程序](https://i-blog.csdnimg.cn/blog_migrate/34b024d589686ea8b09071415c265a07.jpeg)
# !/ bin / sh
#测试用
#cd / opt /
#rm qt_create /
- rf
#sleep 3
#mkdir qt_create
#cd qt_create
#mkdir qt_create1
#cd qt_create1
#mkdir qt_create2
#cd qt_create2
#mkdir ok
#直接调用mplayer播放sdcard里的gq.avi文件
#cd / usr / local / smplayer / bin
#. / mplayer / sdcard / gq.avi
#启动友善的smplayer
cd / usr / local / smplayer / bin
. / smplayer
export HOME =/ root
cd / usr / local / smplayer /
exe__c . / smplayer # - qws 1 >/ dev / null
2 >/ dev / null
#hotplug
# !
/ bin / sh
PATH =/ sbin: / bin: / usr / sbin: / usr / bin: / usr / local / bin:
runlevel = S
prevlevel = N
umask 022
export PATH runlevel prevlevel
#
#
this shell so we can interrupt subprocesses.
#
trap " : " INT QUIT TSTP
/ bin / hostname Crt
[ - e / proc / 1 ]
/ bin / mount - n - t proc
[ - e / sys / class ] ||
/ bin / mount - n - t sysfs none / sys
[ - e / dev / tty ]
/ bin / mount
/ bin / mount - n - t usbfs none / proc / bus / usb
echo / sbin / mdev >
/ proc / sys / kernel / hotplug
/ sbin / mdev - s
/ bin / hotplug
# mounting file system specified in
/ etc / fstab
mkdir - p / dev / pts
mkdir - p / dev / shm
/ bin / mount - n - t devpts none / dev / pts - o mode = 0622
/ bin / mount - n - t tmpfs tmpfs / dev / shm
/ bin / mount - n - t ramfs none / tmp
/ bin / mount - n - t ramfs none / var
mkdir - p / var / empty
mkdir - p / var / log
mkdir - p / var / lock
mkdir - p / var / run
mkdir - p / var / tmp
/ sbin / hwclock - s
syslogd
/ etc / rc.d / init.d / netd start
echo "
"
>
/ dev / tty1
echo " Starting networking... "
>
/ dev / tty1
sleep 1
/ etc / rc.d / init.d / httpd start
echo "
"
>
/ dev / tty1
echo " Starting web server... "
>
/ dev / tty1
sleep 1
/ etc / rc.d / init.d / leds start
echo "
"
>
/ dev / tty1
echo " Starting leds service... "
>
/ dev / tty1
echo "
"
sleep 1
echo "
"
>
/ dev / tty1
/ etc / rc.d / init.d / alsaconf start
echo " Loading sound card config... "
>
/ dev / tty1
echo "
"
/ sbin / ifconfig lo 127.0 . 0.1
/ etc / init.d / ifconfig - eth0
# / bin / qt4 &
#echo "
"
>
/ dev / tty1
#echo " Starting Qt4 Applications, please waiting... "
>
/ dev / tty1
# / bin / smplayer &
#echo "
"
>
/ dev / tty1
#echo " Starting smplayer Applications, please waiting... "
>
/ dev / tty1
/ bin / hello_qt4app &
echo "
"
>
/ dev / tty1
echo " Starting hello_qt4
>
/ dev / tty1
# !/ bin / sh
rm / root / .config /
- rf;
rm / root / .mplayer /
- rf;
if [ - e / etc / friendlyarm - ts - input.conf ] ; then
fi
true
TSLIB_CONFFILE =/ etc / ts.conf
export TSLIB_TSDEVICE
export TSLIB_CONFFILE
export TSLIB_PLUGINDIR =/ usr / lib / ts
export TSLIB_CALIBFILE =/ etc / pointercal
export QWS_DISPLAY = : 1
export LD_LIBRARY_PATH =/ usr / local / lib:$LD_LIBRARY_PATH
export PATH =/ bin: / sbin: / usr / bin / : / usr / sbin: / usr / local / bin
if [ - c / dev / touchscreen ]; then
if [ - e / etc / pointercal - a !
- s / etc / pointercal ] ; then
else
>/ etc / pointercal
fi
export QWS_KEYBOARD = TTY: / dev / tty1
export HOME =/ root
cd / opt
. / hello_qt4 - qws 1 >/ dev / null
2 >/ dev / null
hotplug
结语:
参考资料:
QT下执行Shell或启动外部程序
QT下实现对Linux Shell调用的几种方法 QProcess AND QThread http://www.linuxidc.com/Linux/2010-08/28172.htm