CMake error on Qt5Core

Problem

I tried to build source code that uses Qt5 using CMake. It quit with this error:

CMake Error at CMakeLists.txt:20 (find_package):
  By not providing "FindQt5Core.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Qt5Core", but
  CMake did not find one.

  Could not find a package configuration file provided by "Qt5Core" with any
  of the following names:

    Qt5CoreConfig.cmake
    qt5core-config.cmake

  Add the installation prefix of "Qt5Core" to CMAKE_PREFIX_PATH or set
  "Qt5Core_DIR" to a directory containing one of the above files.  If
  "Qt5Core" provides a separate development package or SDK, be sure it has
  been installed.

Solution

I checked for Qt5 packages and they seemed to be installed. There has been a naming change between Qt4 and Qt5 packages in Ubuntu and that threw me off.

What was needed is the package qtbase5-dev. Once I installed it, this error was solved:

$ sudo apt install qtbase5-dev

Tried with: Ubuntu 14.04

How to plot to multiple windows in Gnuplot

On my Ubuntu, Gnuplot plots into a Qt window (terminal type qt) by default. Every invocation of the plot command redraws the contents of this single Qt window.

Sometimes, I need to plot two different data sets in two different windows, so that I can compare them side-by-side. To do this specify a window number, an integer starting from 0.

For example:

set terminal qt 0
plot("data_0.txt")

set terminal qt 1
plot("data_1.txt")

Tried with: Gnuplot 5.0 and Ubuntu 14.04

How to compile Qt program using CMake

Qt is a fantastic library, but it comes with a lot of dependencies. You typically need to use its IDE and compilation tools. If you are already working with standard tools for editing code and compiling, then this generates a huge disadvantage. If you are using CMake, it has support for compiling Qt programs. And hopefully this makes it a bit more easier to use Qt for you. 🙂

Information on how to write a CMakeLists.txt to compile Qt can be found here. I have created a sample CMakeLists.txt that compiles a Qt program here:

Tried with: Qt 4, CMake 2.8.12.2 and Ubuntu 14.04

How to create Qt applications using Visual Studio

The typical method to create Qt applications is use its Qt Creator as the IDE to write and debug Qt code. (See this post for info.) If you use Visual Studio, you might find it frustrating to learn yet another IDE. In such a case, you can actually create Qt applications using Visual Studio.

  1. Download a suitable Qt installer from the downloads page and install it. Make sure you pick an installer that is 32-bit and built for the Visual Studio version that you have.

  2. Download and install the Qt Visual Studio add-in. You can find it at the bottom of the downloads page. Make sure you pick an installer that matches the Visual Studio version that you have. At the time of this writing, the Visual Studio add-ins were built only for 32-bit version of Qt. That is the reason I used 32-bit in Step 1.

  3. Open your Visual Studio. In the toolbar, there is a new menu named Qt5. Go to Qt5 > Qt Options. In the Qt Versions tab, add the path to the Qt you installed in Step 1. Note that you need to add the path to the parent directory of the bin directory of Qt. For example, on my particular installation this path was C:\Qt\Qt5.2.0\5.2.0\msvc2012.

  4. Create a Qt project. To do this, open File > New > Project and choose Qt5 Projects > Qt Application. After you provide the name and path, a Qt wizard pops up to handle the Qt specific details of this project. Once you are done with it, you will be dropped into a project with sample source and interface files.

  5. Build the Qt project by choosing Build > Build Solution. This is where the Qt add-in steps in and does its meta-object magic before letting the Visual C++ compiler to compile the generated files.

  6. The build should hopefully succeed. You can now execute and even debug the Qt application from Visual Studio.

Tried with: Qt 5.2.0, Qt Visual Studio add-in 1.2.2, Visual Studio 2012 and Windows 7 x64

How to add include directory to Qt project

The method to add an include directory to a Qt project depends on how the project was created.

If the project has a .pro file, then add this line to it:

INCLUDEPATH += "C:\some\include\path"

If you have Qt Creator open with the project, you can find the .pro file under the project name. Alternatively, you can use any editor to edit this file. Remember to run qmake after adding the include directory.

If the project has a .includes file, then just add the path directly to it:

C:\some\include\path

Tried with: Qt 5.2.0 and Windows 7 x64

How to get started with Qt on Windows

Qt is a popular application framework to develop cross-platform GUI applications. Installing and using it on Windows is quite easy:

  1. From the Qt downloads page, download a Windows installer. The online installer is the best choice, since it allows you to install Qt built for all types of platforms. However, I found that the online installer was very slow and would stop downloading after a while. So, I decided to go with an offline installer. For Windows offline installers, you need to decide among these parameters: compiler (MinGW or Visual C++), compiler version (Visual Studio 2010 or 2012), architecture (32-bit or 64-bit) and OpenGL support. Depending on the combination you pick, say Visual Studio 2012 32-bit with OpenGL, you can find a corresponding installer for offline installation. Download and run the installer and it will install Qt source, libraries and tools under C:\Qt directory. The actual executables will be placed in a deeper directory. For example, mine were in C:\Qt\Qt5.2.0\5.2.0\msvc2012

  2. From the Start menu, open Qt Creator. This is the IDE that needs to be used to write a Qt application.

  3. In Qt Creator, create a new project by choosing File > New File or Project. Choose Applications and Qt Widgets Application if you want to create a GUI program. In the following dialogs, you will be asked to provide a name for the project, directory to store the files and a kit to use for compilation.

  4. This creates a project file with extension .pro and three source files: main.cpp, mainwindow.cpp and mainwindow.h.

  5. To compile this project, right-click on the project name in Projects section and choose Run qmake. This runs qmake which creates Makefiles relevant to your environment. Right-click on project name again and choose Build. This uses the Makefiles that was created earlier to build an executable. Finally right-click and choose Run. This runs the executable, which should result in an empty window being displayed. From here on, you can add source code to these files or more source files to extend this window into a Qt application.

Tried with: Qt 5.2.0, Visual Studio 2012 and Windows 7 x64