QT中怎么调Linux 命令

QT中怎么调Linux 命令,第1张

有个这类 QProcess 

    QProcess p(0)

    QString command = ""

    QStringList args

    QString strTemp = ""

    QStringList tmpList

    

    /**

     * 调用 hostname 命令 获取机器名

     ================================================================ */

    QString hostname = ""

    command = "hostname"

    args.clear()

    p.start( command,args )

    p.waitForFinished()

    strTemp = QString::fromLocal8Bit( p.readAllStandardOutput() )

    hostname = strTemp.replace( "\n","").trimmed()

    qDebug()<<"hostname:"<< hostname

在php教程中调用linux命令的函数是string exec(string command, string [array], int [return_var])如exec( "vpopmail ")echo exec('whoami')再看一实例function exec_enabled() { $disabled = explode(', ', ini_get('disable_functions')) return !in_array('exec', $disabled)}<?php $tmp = exec("c:\Image\gm.exe convert c:\Image\file1.tiff c:\Image\file1.jpg", $results)?>还有一种命令是php通过函数system()调用系统命令。string system ( string $command [, int &$return_var ] )实例system('asterisk -vvvvvvvvvvvc')system()是一样的函数C的,它执行给定的命令和输出结果的版本。该system()的调用也尝试自动刷新网页服务器的输出缓冲器在每个输出行如果PHP运行作为服务器模块。如果你需要执行一个命令,并已全部通过直接从背面没有任何干扰的命令数据,使用passthru()函数。$last_line = system('ls', $retval)function my_exec($cmd, $input='') {$proc=proc_open($cmd, array(0=>array('pipe', 'r'), 1=>array('pipe', 'w'), 2=>array('pipe', 'w')), $pipes) fwrite($pipes[0], $input)fclose($pipes[0]) $stdout=stream_get_contents($pipes[1])fclose($pipes[1]) $stderr=stream_get_contents($pipes[2])fclose($pipes[2]) $rtn=proc_close($proc) return array('stdout'=>$stdout, 'stderr'=>$stderr, 'return'=>$rtn ) } var_export(my_exec('echo -e $(</dev/stdin) wc -l', 'h\nel\nlo'))实例三$cmd = "date" $output = system($cmd) printf("System Output: $output ") exec($cmd, $results) printf("Exec Output: {$results[0]} ")php调用linux命令的权限问题你可以使用定时任务执行你要调用的php,这时的权限就是root,php通过函数system()调用系统命令php一般是以apache用户身份去执行的,把apache加入到存储你文件的父文件夹属组里去,然后改该父文件夹权限为775,这样属组成员就有写的权限,而apache属于这个组就可以改写该目录下所有文件的权限,当然,属组最好不要是root,你可以为该文件夹改个其它普通用户组。改apache/php的运行用户方法不安全

1、在Linux下的命令行编辑程序:

[root@localhost root]# mkdir hello

//mkdir命令创建一个hello目录

[root@localhost root]# cd hello

//cd命令切换到刚才创建的hello目录

[root@localhost hello]# vi main.cpp

//在hello目录中用vi创建一个main.cpp文件 将下面的代码输入到main.cpp文件中

#include <QApplication>

#include <QLabel>

int main(int argc,char *argv[])

{

QApplication app(argc,argv)

QLabel *label = new QLabel(“Hello Qt”)

Label->show()

return app.exec()

}

2、然后在命令行编译程序:

[root@localhost hello]# qmake –project

//执行qmake –project,因为目录是hello,因此在hello目录下生成一个与平台无关的项目文件hello.pro,

[root@localhost hello]# qmake hello.pro

//执行qmake hello.pro项目文件后,在hello目录下生成一个与平台有关的Makefile文件。

[root@localhost hello]# make

//执行make进行编译源代码,并生成main.o目标文件及hello执行文件。

[root@localhost hello]# ./hello

//执行hello,就会d出Hello Qt窗口,到此说明成功了。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/6270468.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-19
下一篇 2023-03-19

发表评论

登录后才能评论

评论列表(0条)

保存