在Qt中如何寫控制臺程序
找到兩種方法可以寫控制臺程序,控制臺程序在Qt下也很容易實現,那么,非常好用的cin和cout又可以派上大用場了,其實使用這些標準庫函數也很簡單,和在Visual Studio編譯器下一樣,不過Qt更簡單!
***種,直接用標準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
建立項目文件 HelloConsole.pro
4. 修改 HelloConsole.pro,在其中加入一行
- CONFIG += console
5. 在 HelloConsole 目錄下輸入
- qmake
- mingw32-make
6. 生成的可執行文件在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中寫控制臺程序,關鍵是在項目文件中加上一行
- CONFIG += console
【編輯推薦】