简单地说这行到你的亲文件:
CONFIG += console
在Qt5.x也从Win7到Win10你可以做这样的事情:
//includes
#include <QApplication>
#include <QMainWindow>
#include <QString>
#include <QObject>
#include <QDir>
#include <QSettings>
#include <windows.h>
#include <stdio.h>
#include <iostream>
//
// Add to project file:
// CONFIG += console
//
服务器:
//Here we have the Server class able to send signals
//Server will be used in main.cpp for console
//and/or in MainWindow to handle the signals
class Server : public QObject
{
Q_OBJECT
public:
Server(QObject *parent = 0) : QObject(parent)
{
//do server stuff
//this->setName("Test");
//std::cout << this->getName() << std::endl;
//std::cout << "Enter URL: << std::endl;
//std::string url;
//std::cin >> url;
//_url = QString::fromStdString(url);
//emit finished();
}
signals:
void finished();
private:
QString _url;
};
The Ma inWindow:
//Here is the MainWindow using Server
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0) : QMainWindow()
{
server = new Server(this); //use server in hybrid mode (console and gui)
connect(server, SIGNAL(finished()), this, SLOT(close())); //establish connection
}
private:
Server *server;
};
主要:
int main(int argc, char *argv[])
{
QString iniPath = QFileInfo(QDir::fromNativeSeparators(argv[0])).absolutePath(); //get the current dir
QSettings settings(iniPath+"/settings.ini", QSettings::IniFormat); //open ini
bool gui = settings.value("gui", false).toBool(); //read ini
if(gui) //decide
{
#if defined(Q_OS_WIN)
// hide console window, but not in your case
// ::ShowWindow(::GetConsoleWindow(), SW_HIDE);
#endif
//std::cout will print to the console in the bg like you wished
QApplication a(argc, argv);
MainWindow *w = new MainWindow;
w->show();
int e = a.exec();
delete w; //needed to execute deconstructor
exit(e); //needed to exit the hidden console
return e;
}
else
{
QCoreApplication a(argc, argv);
Server *instance = new Server; //use server in console only
exit(0);
return a.exec();
}
}
我试了一下还没有的 “CONFIG + =控制台”,但你需要重定向数据流和创建您自己的控制台:
#ifdef _WIN32
if (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole()){
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
freopen("CONIN$", "r", stdin);
}
#endif
但是这只有在你通过调试器启动时才有效,否则所有的输入都是针对系统的。意思是,如果你通过std :: cin键入一个名字,系统会尝试执行命令作为命令。 (非常奇怪)
其他两个警告这个尝试是,你不能使用:: FreeConsole()它不会关闭它,如果你通过控制台启动它,应用程序将不会关闭。
最后还有一个Qt help section in QApplication这个话题。我试过的例子有一个应用程序,它不会为GUI工作,它在一个无限循环的地方stucked和GUI不会被渲染或者它只是崩溃:
QCoreApplication* createApplication(int &argc, char *argv[])
{
for (int i = 1; i < argc; ++i)
if (!qstrcmp(argv[i], "-no-gui"))
return new QCoreApplication(argc, argv);
return new QApplication(argc, argv);
}
int main(int argc, char* argv[])
{
QScopedPointer<QCoreApplication> app(createApplication(argc, argv));
if (qobject_cast<QApplication *>(app.data())) {
// start GUI version...
} else {
// start non-GUI version...
}
return app->exec();
}
因此,如果您使用的是Windows和Qt,那么只需使用控制台选项,如果需要使用GUI并通过退出关闭它,请隐藏控制台。
如果您想要配置GUI或不使用.ini文件,这意味着您的程序已经能够同时执行控制台和GUI,因此它使用QApplication而不是QCoreApplication,它已经将所有GUI代码编译并链接在一起但不要创建MainWindow。或者你制作了2个不同的二进制文件,你不需要.ini文件,这取决于用户选择他想要的程序。在任何情况下,您的程序中始终只有1个QApplication(或QCoreApplication),您不会同时使用两者。 – ymoreau