在Qt中如何寫控制臺(tái)程序
找到兩種方法可以寫控制臺(tái)程序,控制臺(tái)程序在Qt下也很容易實(shí)現(xiàn),那么,非常好用的cin和cout又可以派上大用場(chǎng)了,其實(shí)使用這些標(biāo)準(zhǔn)庫函數(shù)也很簡(jiǎn)單,和在Visual Studio編譯器下一樣,不過Qt更簡(jiǎn)單!
***種,直接用標(biāo)準(zhǔn)c++寫,具體如下:
1. 建立 HelloConsole 目錄
2. 在該目錄下新建 main.cpp
- #include <iostream>
- using namespace std;
- int main(int argc, char **argv)
- {
- cout << "Hello!" << endl;
- return 0;
- }
- #include <iostream>
- using namespace std;
- int main(int argc, char **argv)
- {
- cout << "Hello!" << endl;
- return 0;
- }
3. 在 HelloConsole 目錄下輸入
- qmake -project
建立項(xiàng)目文件 HelloConsole.pro
4. 修改 HelloConsole.pro,在其中加入一行
- CONFIG += console
5. 在 HelloConsole 目錄下輸入
- qmake
- mingw32-make
6. 生成的可執(zhí)行文件在debug目錄下
第二種,使用qt自帶的類,編譯過程與***種一致,不同的是代碼:
- #include <QTextStream>
- static QTextStream cout(stdout, QIODevice::WriteOnly);
- int main(int argc, char **argv)
- {
- cout << "Hello!" << endl;
- return 0;
- }
- #include <QTextStream>
- static QTextStream cout(stdout, QIODevice::WriteOnly);
- int main(int argc, char **argv)
- {
- cout << "Hello!" << endl;
- return 0;
- }
在qt中寫控制臺(tái)程序,關(guān)鍵是在項(xiàng)目文件中加上一行
- CONFIG += console
【編輯推薦】