2010-02-23 71 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