QT下,如何获取控制台输入

最近工作中为了测试某个模块,需要把原先输入模块部分,改成控制台输入来方便测试。在QT中,我们可以使用 QTextStream 类来读取用户的输入来达到目的。下面是一个简单的例子:

cpp 复制代码
#include <QCoreApplication>
#include <QTextStream>

int main(int argc, char *argv[]) {
    QCoreApplication app(argc, argv);
    
    QTextStream cin(stdin);
    QString input;
    while (true) {
        input = cin.readLine();
        if (input == "q") {
            break;
        }
        qDebug() << "You entered: " << input;
    }

    return app.exec();
}

当然,我们也可以把这段代码写到一个线程中来执行,例如:

cpp 复制代码
void startForTestThread()
{
    QThread threadM;
    ForTest pForTestM = new ForTest();
    pForTestM->moveToThread(&threadM);
    threadM.start();
    connect(&threadM, SIGNAL(started()), this, SLOT(waitForEnter()));
}

void waitForEnter()
{
    // for test
    QTextStream cin(stdin);

    QString input;
    while (true) {
        input = cin.readLine();
        if (input == "q") {
            break;
        }
        qDebug() << "You entered: " << input;
        
        // do something with "input"
        sendSignalSimulate(input.toInt());
    }
}

只要在你的程序合适位置调用startForTestThread函数即可。

综上,我们就可以通过输入不同的参数来进行测试了。

相关推荐
Y1rong3 小时前
C++ QT之记事本
开发语言·qt
IOT-Power7 小时前
QT 串口 源码结构框架
qt
世转神风-11 小时前
winDbg专有名词解释
qt·windbg
2401_8534482315 小时前
项目图片功能制作
linux·数据库·qt·系统移植
深蓝海拓15 小时前
QT中具有重载特性的一些部件
python·qt·pyqt
yangops15 小时前
QT Creator解决: C3861: “_mm_loadu_si64”: 找不到标识符
qt·_mm_loadu_si64·c3861
火山灿火山17 小时前
Qt常用控件(四)
开发语言·qt
蓝天智能19 小时前
Qt实战:Unix_Linux下QTableView Checkbox不显示?问题排查+样式定制全攻略
linux·qt·unix
蓝天智能19 小时前
QT实战:设置按钮背景图标的几种方式及分析
开发语言·qt
小c君tt20 小时前
FFmpeg音视频-库使用2
qt·ffmpeg·音视频