Can I use Qt without qmake or Qt Creator?
Sure you can. Although it is more convenient with qmake or CMake, you can do:
CXXFLAGS += -Ipath_to_your_qt_includes
LDFLAGS += -Lpath_to_your_qt_libs
LDLIBS += -lqt-mt (for Qt3)
or
LDLIBS += -lQtCore -lQtGui (for Qt4, add what you need)
my_prog: my_prog.cpp
(in a makefile)
Update - invoking moc
:
Quote from moc manpage:
Here is a useful makefile rule if you
only use GNU make:m%.cpp: %.h
moc $< -o $@
I'd personally name the output rather %.moc.cpp
(than m%.cpp
). You then add the dependency of my_prog
on my_prog.moc.cpp
my_prog: my_prog.cpp my_prog.moc.cpp
Similarly for uic. The situation here is more complicated, since you have to generate rules for headers and source files, and you have to add a dependency on a header file to ensure it gets generated before the sources are compiled. Something like this might work:
my_prog: my_prog.o my_prog.moc.o my_prog.ui.o
$(CXX) $(LDFLAGS) -o my_prog $^ $(LDLIBS)
my_prog.o: my_prog.cpp my_prog.ui.h
How to compile a Qt program without qtCreator on Windows?
I finally managed to do it 100% from command line, without the qtCreator IDE, but not yet without qmake
. Steps to reproduce:
Let's assume Microsoft MSVC 2019 is installed.
Install
qt-opensource-windows-x86-5.14.2.exe
. (This is the latest Windows offline installer I could find), double check that you install at leastmsvc2017_64
.Note: Don't use
qtbase-everywhere-opensource-src-5.15.4.zip
: using theinclude
subfolder from this package forcl.exe /I ...
is not enough. (I thought it would, at first)Create a folder
example
containing themain.cpp
file aboveOpen a command line window in this folder and do:
vcvarsall.bat x64
Now either do
"c:\path\to\msvc2017_64\bin\qmake.exe" -project
to create aexample.pro
project file or create it manually with:TEMPLATE = app
TARGET = qt_example
INCLUDEPATH += .
CONFIG += console
SOURCES += main.cppDo
"c:\path\to\msvc2017_64\bin\qmake.exe"
. This will create aMakefile
file.Run
nmake
. This is Microsoft MSVC's equivalent of themake
tool.Copy
c:\path\to\msvc2017_64\bin\Qt5Core.dll
into therelease
folderRun
release\example.exe
. Working!
Addendum: here is solution now for a minimal GUI app:
main.cpp
#include <QtCore/QCoreApplication>
#include <QTextStream>
#include <QMessageBox>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMessageBox::information(NULL, "Hello", "Hello", "Ok");
return a.exec();
}
qt_example_gui.pro
TEMPLATE = app
TARGET = qt_example_gui
INCLUDEPATH += .
SOURCES += main.cpp
QT += gui widgets
Do the vcvarsall.bat x64
, qmake
, nmake
like in the solution above. No be sure you have this file structure:
release\qt_example_gui.exe
release\Qt5Core.dll
release\Qt5Gui.dll
release\Qt5Widgets.dll
release\platforms\qwindows.dll
Run the .exe, that's it!
How to avoid qmake Build Step if using QT Creator
This is quite easy, I use makefile only project alot because I like qt-creator as an IDE. In the IDE goto the projects tab on the left.
Select the "build" tab near the top of that page, looks like: (build | run)
.
In the build steps:
- remove the qmake build step by press hovering the mouse over that step and clicking the X that appears.
Edit the
build directory
so that it is the same directory as your makefile.Note: you will have to click the
shadow build
check box next to it to enable it.- remove any other steps you don't want (infact just remove them all for now).
- Add a new step
make
step. It will try to use the default make, but you can override that if you want. Also add any arguments likedebug
or-j4
etc...
Then you are done :)
Next time you hit build it will simply invoke that make command.
Note: You will need to do the same for any other configurations you have (like release, debug, etc...). You can also just add loads more configurations for doing other make options (if you have them) for instance make doxygen
or such...
Just remembered you can also either:
- I am not sure why, but when I tested it (as OP did) you can't seem to just setup a
make
step with parameters-C ../
, it seems to want to look in the "build directory" first.
I tend to use the build location since its a nice feature of the IDE.
Note an issue with newer versions of Qtcreator as a makefile IDE is that you cant share your build settings with other people (i.e. can't configure control them) because they are locked to your PC profile... long story... but its very sad and I no longer use qt creator for that one reason.
Set .exe icon for Qt windows app without qmake/VS
Yes, you can. Instead of windres.exe
, the Windows SDK provides rc.exe
, a.k.a. the Resource Compiler. You need first an image in .ico format (named here "app.ico") and a text file named "app.rc" with this content:
IDI_ICON1 ICON DISCARDABLE "app.ico"
You can compile this file with this command:
rc app.rc
That command will produce a file with the name "app.res". This RES file can be processed by the linker directly, or you may convert it to .obj with this command:
cvtres /MACHINE:X64 app.res
That command produces a file with the name "app.obj" (for architecture x86_64, use cvtres /h
to see other parameters). You need to give this .obj file to the linker when producing the executable. "cvtres.exe" comes from the VC compiler bin directory, and not the Windows SDK.
How to use a build system other than qmake with a Qt project?
My CMake template:
cmake_minimum_required (VERSION 2.8)
project(qttest)
set(PROJECT_VERSION 0.0.1)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -pthread -fno-permissive -pedantic -Wall -Wextra -fPIC")
endif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(QT_VERSION_REQ "5.2")
find_package(Qt5Core ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Quick ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Widgets ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5DBus ${QT_VERSION_REQ} REQUIRED)
set(CMAKE_AUTOMOC ON)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
list(APPEND SOURCES
)
list(APPEND MAIN_SOURCES
main.cpp
${SOURCES}
)
list(APPEND LIBS
Qt5::Core
Qt5::Quick
Qt5::Widgets
Qt5::DBus
)
add_executable(${PROJECT_NAME} ${MAIN_SOURCES})
target_link_libraries(${PROJECT_NAME} ${LIBS})
Why does Qt Creator need a kit for non-qt cmake project
You are free to have kits in Qt Creator with or without Qt, with or without a C++ compiler, with or without cmake binaries, etc.
Qt Creator uses kits as a collection of things that are used together in (multiple) projects, so that you do not have to define these same settings again and again. The settings available in a kit depend on the plugins you have enabled and Creator is perfectly happy if some information is not set -- as long as this information is not needed by the project you are working on. So if you open a qmake-based project, creator will complain if a kit has no Qt version set (which is what provides the qmake binary). If you try to open a cmake-based project, then Creator will complain about Kits without a cmake binary set up. Kits are in no way limited to qmake projects, but does also apply to cmake, autotools, nim, python projects (and whatever else is supported;-).
For CMake projects the Kit defines the cmake binary to use, the Generator to use with that binary, default values to pass to cmake, the compiler used by the code model built into Qt Creator (and by the cmake project itself in newer Qt Creator versions), the Qt version that is used by the code model (and by the cmake project in newer QtC versions), the default environment the project will see when building and some more things.
Related Topics
C++ Why the Assignment Operator Should Return a Const Ref in Order to Avoid (A=B)=C
Performance Penalty for Working with Interfaces in C++
Converting Between C++ Std::Vector and C Array Without Copying
Delete All Items from a C++ Std::Vector
How to Use Signal Inside a C++ Class
Localtime VS Localtime_S and Appropriate Input Arguments
How to Convert Rgb -> Yuv -> Rgb (Both Ways)
Why Is (Void) 0 a No Operation in C and C++
C++ Handling of Excess Precision
How to Add the Mingw Bin Directory to My System Path
Do Polymorphism or Conditionals Promote Better Design
How to Find the Name of the Calling Function
How to Encode a String to Base64 Using Only Boost
Double-Checked Lock Singleton in C++11
"Apientry _Twinmain" and "Winapi Winmain" Difference
What Are Potential Dangers When Using Boost::Shared_Ptr
Debug and Release Library Linking with Cmake (Visual Studio)