简体   繁体   English

QProcess :: execute(“清除”)问题

[英]QProcess::execute(“clear”) Issue

I'm writing a small console app in Qt and I want to be able to clear the terminal on a user command. 我正在Qt中编写一个小型控制台应用程序,并且希望能够通过用户命令清除终端。 I found this: 我找到了这个:

How clear screen in QT console? QT控制台的屏幕如何清晰?

which almost answers my question, but its not working. 几乎可以回答我的问题,但无法正常工作。

When the function "QProcess::execute("clear");" 当函数“ QProcess :: execute(” clear“);” is run, I get this output to the terminal: 运行后,我将此输出发送到终端:

TERM environment variable not set. TERM环境变量未设置。

I'm pretty new to Linux and though I've set environment variables before, its always been in the terminal before I ran the program. 我对Linux很陌生,尽管我之前已经设置了环境变量,但是在运行程序之前,它始终位于终端中。 Here, I'd like to take care of this programmatically if possible. 在这里,如果可能的话,我想以编程方式进行处理。

My guess is that I could use QProcess::setProcessEnvironment() but I'm not really sure how exactly. 我的猜测是我可以使用QProcess :: setProcessEnvironment(),但我不确定该如何精确。

Is it possible to set the environment variables in this way, and if so how? 是否可以通过这种方式设置环境变量?

Any help would be greatly appreciated! 任何帮助将不胜感激!

Here's the sample code I'm working with: 这是我正在使用的示例代码:

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QTextStream qin(stdin);
    QTextStream qout(stdout);
    QString cmd;

    while(1)
    {
        cmd = qin.readLine();

        qout<<"command is: "<<cmd<<endl;

        if(cmd == "clear")
        {
            QProcess::execute("clear");
        }
    }
    return a.exec();
}

The code below works fine for me. 下面的代码对我来说很好。 Please make sure that the clear command works fine in your console first. 请首先确保clear命令在您的控制台中正常运行。

main.cpp main.cpp中

#include <QProcess>
#include <QDebug>

int main()
{
    QProcess::execute("clear");
    qDebug() << QProcessEnvironment::systemEnvironment().contains("TERM");
    return 0;
}

main.pro main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

Build and Run 生成并运行

qmake && make && ./main

Note that if you are using QtCreator, you will need add the environment variable with its value explicitly in the build settings tab. 请注意,如果您使用的是QtCreator,则需要在构建设置选项卡中显式添加环境变量及其值。 Here you can find more details in the documentation: 您可以在文档中找到更多详细信息:

QtCreator - Using Environment Variables QtCreator-使用环境变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM