QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8424|回复: 5

基础篇 -- 使用QProcess和QThread运行外部指令

[复制链接]
发表于 2008-3-9 12:55:05 | 显示全部楼层 |阅读模式
最简单的方法
  1. #include <QProcess>
  2. #include <QThread>
  3. class MyThread : public QThread
  4. {
  5. public:
  6. void run();
  7. };

  8. void MyThread::run()
  9. {
  10. QProcess::execute("service lighttpd restart");
  11. }

  12. int main()
  13. {
  14. MyThread *thread=new MyThread;
  15. thread->start();
  16. }
复制代码


细化操作--不打印显示任何的操作结果

  1. #include <QProcess>
  2. #include <QThread>
  3. #include <QString>
  4. #include <QStringList>
  5. class MyThread : public QThread
  6. {
  7. public:
  8. void run();
  9. };

  10. void MyThread::run()
  11. {
  12. QProcess *testc=new QProcess;
  13. QString program = "rm";
  14. QStringList arguments;
  15. arguments << "./a.txt";
  16. testc->start(program, arguments);
  17. }

  18. int main()
  19. {
  20. MyThread *thread=new MyThread;
  21. thread->start();
  22. }
复制代码

[ 本帖最后由 haulm 于 2008-3-9 12:57 编辑 ]
发表于 2008-3-9 17:10:13 | 显示全部楼层
to haulm:
你的这几篇都是具体应用的,有没有详细介绍Qt的,
比如Qt类库的结构
没个类的属性和方法
和他们的作用

还有Qt的信号机制的
回复

使用道具 举报

 楼主| 发表于 2008-3-9 20:33:08 | 显示全部楼层
这个应该去看pdf格式的《24学时Qt编程》以及公社早前我有收集汤坤的60页的Qt4图片教程。虽然《24学时Qt编程》讲的是Qt3编程,但配合Qt4助手还是不错的。虽然Qt3和Qt4有很大差别,但基础内容还是差不多的。
回复

使用道具 举报

发表于 2008-3-10 13:04:38 | 显示全部楼层
为什么还要用 QThread ?直接调用 QProcess::execute 就可以了

  1. #include <QProcess>
  2. int main()
  3. {
  4.         QProcess::execute("ls");
  5.         return 0;
  6. }

复制代码
回复

使用道具 举报

 楼主| 发表于 2008-3-10 15:12:01 | 显示全部楼层
是的,但是这类的编程肯定要用到 QThread ,这样会更好一些。
回复

使用道具 举报

发表于 2008-3-10 17:11:58 | 显示全部楼层
尤其在要等待一个事务结束后才能执行另一个的顺序型。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-4-27 03:00 , Processed in 0.108439 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表