带有可选GUI的Qt控制台应用程序

问题描述 投票:1回答:1

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

怎么实现这个?我想,我必须处理QApplication和QCoreApplication吗?

qt console qt4 qt5
1个回答
0
投票

只需将此行放入您的专业档案:

CONFIG += console

在从Win7到Win10的Qt5.x中,您可以执行以下操作:

//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;

};

MainWindow:

//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 + = console”,但是你需要重定向流并自己创建控制台:

#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,它在无限循环中停留在某处,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则隐藏控制台并通过退出将其关闭。

© www.soinside.com 2019 - 2024. All rights reserved.