如何查找是否从终端或GUI运行 [英] how to find out if running from terminal or GUI

查看:130
本文介绍了如何查找是否从终端或GUI运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想建立一个类,如果使用shell或从GUI运行,它的行为是不同的。 #includemyclass.h...



但是,在构造函数中,我想区分Shell运行和GUI运行。



我可以很容易地实现它,使用一个参数,当声明它,但我想要探索我的选项,将传递给构造函数。



使用C ++在ubuntu和我的GUI是使用Qt。

解决方案

从桌面启动程序(双击或从桌面文件/开始菜单)通常会将他们的stdin文件描述符重定向到管道。您可以检测到这一点:

  #include< cstdio> // fileno()
#include< unistd.h> // isatty()

if(isatty(fileno(stdin)))
//我们从命令行启动。
else
//我们从桌面内启动


I am trying to build a class that would behave is a different way if run using a shell or from a GUI.

It could be included in both forms using #include "myclass.h"...

However, in the constructor I would like to differentiate between Shell runs and GUI runs.

I can easily achieve it using a parameter that would be passed to the constructor when declaring it but I want to explore my options.

I am using C++ on ubuntu and my GUI is using Qt.

解决方案

Launching programs from the desktop (double click or from a desktop file/start menu) will usually redirect their stdin file descriptor to a pipe. You can detect this:

#include <cstdio>    // fileno()
#include <unistd.h>  // isatty()

if (isatty(fileno(stdin)))
    // We were launched from the command line.
else
    // We were launched from inside the desktop

这篇关于如何查找是否从终端或GUI运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆