How to add GUI to a Qt console application?



  • I created a Non-Qt project with the qmake build system. I created another project, but this time a Qt Widgets Application. They both work individually, but I now want to have only one project: i.e. add the GUI project to the console project. How should I modify the .pro file of the console application to achieve it?

    This is my console project:

    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt
    
    SOURCES += src/gregorian.c \
        src/main.c
    
    

    And this is my GUI project:

    #-------------------------------------------------
    #
    # Project created by QtCreator 2016-08-21T16:32:41
    #
    #-------------------------------------------------
    
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = ZellerGUI
    TEMPLATE = app
    
    
    SOURCES += main.cpp\
            zellergui.cpp \
        gregorian.cpp
    
    HEADERS  += zellergui.h \
        gregorian.h
    
    FORMS    += zellergui.ui
    

    By the way, if I know in advance that I might have a GUI, is it recommended to create a Qt Widgets Application from the start? If it happens to remain without GUI, can it be later modified to a console application?

    Thank you.


  • Lifetime Qt Champion

    Hi,

    You have an example in the detailed documentation of QApplication on how to start an application either as console or GUI app.

    The most simple path would be to copy your classes from your console application to your GUI application and then modify the main.cpp to start in on or the other mode and you should be fine.

    Hope it helps



  • @SGaist Thank you, I will try it!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.