2010-02-23 45 views

回答

1

我只是想用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(); 

}

希望它可以帮助一点!

+1

我认为他正在谈论使用cin与某些qt对象,尤其是QString,而不仅仅是整数。 – 2010-02-25 07:12:56

7

是的,它是可能的,并按预期工作,虽然你可以做一些事情,如使用线程,这可能会导致这种方法的问题。

不过,我会建议一个更地道(QT)的方式从标准输入读取:

QString yourText; 
QFile file; 
file.open(stdin, QIODevice::ReadOnly); 
QTextStream qtin(&file); 
qtin >> yourText; 
+0

谢谢你这样一段有趣的片段。 – sivabudh 2010-06-10 20:10:08

+0

你可以用cout(qout)做类似的事情。其中一个更大的好处是对许多Qt类型的本地支持。 – 2010-06-10 21:57:58

20

我测试了Kaleb Pederson的回答,发现比他提出的解决方案更consise方式(虽然我要感谢他指着我正确的方向):

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作为中间人。

+1

Coolbeans。我不喜欢使用“stdin”作为假文件的想法。 – mpen 2010-06-10 21:42:47