可以cin
在Qt中使用吗?我可以使用cout
但找不到如何cin
在 Qt 控制台应用程序中使用的示例。
问问题
22995 次
3 回答
24
我测试了Kaleb Pederson的答案,并找到了比他提出的解决方案更简洁的方法(尽管我必须感谢他指出我正确的方向):
QTextStream qtin(stdin);
QString line = qtin.readLine(); // This is how you read the entire line
QString word;
qtin >> word; // This is how you read a word (separated by space) at a time.
换句话说,你真的不需要 QFile 作为你的中间人。
于 2010-06-10T21:38:39.543 回答
8
是的,这是可能的,并且可以按预期工作,尽管您可以做一些事情,比如使用线程,这可能会导致这种方法出现问题。
但是,我会推荐一种更惯用的(Qt)方式从标准输入读取:
QString yourText;
QFile file;
file.open(stdin, QIODevice::ReadOnly);
QTextStream qtin(&file);
qtin >> yourText;
于 2010-02-23T21:49:20.070 回答
3
我刚刚用 QtCreator 尝试了以下代码,它似乎正在工作:
#include <QtCore/QCoreApplication>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
cout << endl << "hello" << endl;
int nb;
cout << "Enter a number " << endl;
cin>>nb;
cout << "Your number is "<< nb<< endl;
return a.exec();
}
希望它有点帮助!
于 2010-02-23T21:49:15.923 回答