2017-08-04 455 views
1

我为我的服务器编写了一个控制台应用程序。它工作得很好,我可以通过终端启动它,一切都可以。 对于桌面环境,在程序的settings.ini文件中设置一个标志以打开MainWindow以显示正在运行的控制台应用程序的某些信息将会非常好。后台的控制台可以保持打开状态。我需要的是一个窗口,以及在控制台运行的主应用程序和MainWindow之间的一些SINGAL/SLOTS。带可选GUI的Qt控制台应用程序

如何实现这一点?我想通了,我必须用QApplication和QCoreApplication处理吗?

+0

如果您想要配置GUI或不使用.ini文件,这意味着您的程序已经能够同时执行控制台和GUI,因此它使用QApplication而不是QCoreApplication,它已经将所有GUI代码编译并链接在一起但不要创建MainWindow。或者你制作了2个不同的二进制文件,你不需要.ini文件,这取决于用户选择他想要的程序。在任何情况下,您的程序中始终只有1个QApplication(或QCoreApplication),您不会同时使用两者。 – ymoreau

回答

0

简单地说这行到你的亲文件:

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并通过退出关闭它,请隐藏控制台。

相关问题