PROBLEN
使用Qt Creator编写代码,打印的输出往往在自带的一个输出窗口中,而这个输出窗口中的输入是无法获取的。如何利用Qt IDE 调出系统的控制台并打印输出到控制台且从控制台获取输入呢?
SOLUTION
1、在pro文件中添加 CONFIG+= console
2、在最左侧选择项目(ctrl + 5)->构建运行 选择运行 将 Run in terminal选中。
TEST
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <iostream>
#include <cstdio>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_BtnExp_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "MainWindow.h"
#include "ui_MainWindow.h"
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_BtnExp_clicked()
{
cout<<"this is from cout"<<endl;
printf("this is from printf \r\n");
char chIn=getchar();
cout<<"user input char is "<<chIn<<endl;
}
main.cpp
#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}