Using Cmake with Qt Creator

38,169

Solution 1

You can add files using glob expression in your CMakeLists.txt, like this:

file(GLOB SRC . *.cpp)
add_executable (your_exe_name ${SRC})

Cmake will pick your new cpp files next time you run it and QtCreator will show them in the project browser.

Update

This solution may be useful but as noted in comments - this is not a good practice. Every time somebody add new source file and commit changes, you need to rerun cmake to build all the sources. Usually I just touch one of the CMakeLists.txt files if my build is broken after I pool recent changes from repository. After that make will run cmake automatically and I didn't need to run it by hands. Despite of that I think that explicit source lists in CMakeLists.txt is a good thing, they called thing CMake Lists for a reason.

Solution 2

When you add new files in QtCreator using the "New File or Project..." dialog it only creates the files on disk, it doesn't automatically add the files to the CMakeLists.txt. You need to do this by hand by editing the CMakeLists.txt file.

The next time you build the project, CMake will be re-run, and QtCreator will pick up the new files and show them in the project browser.

Solution 3

I solve this problem that I added new files in standard way (CTRL+N), then added needed files in CMakeLists. After that, right click on project in project tree view and choose option Run CMake. After this, files showed in project list tree. Only build was not enough.

Solution 4

I tested here and happened the same behavior because those options you were asking were really disabled.

Use File -> "New File or Project..." or CTRL+N to add new files and after that add to CMakeLists.txt

Solution 5

I'm adding an updated answer for newer versions of QtCreator (4.x, I don't know precisely which release but at least from 4.7). In the Tools > Options... menu, choose the Build & Run section and then the CMake tab. You will see the Adding Files settings, and you can set it to Copy file paths :

enter image description here

This way, when you want to add a new file to your project, in the Project view, right click on the desired CMake Executable/Library's name and select Add New..., go through the Add dialog, and when you'll validate the dialog, QtCreator will open CMakeLists.txt in the Editor view. Finally, paste the content of the clipboard at the end of the corresponding source file list and save CMakeLists.txt. The CMake project will be parsed, and your new file will show up in the Project view.

Share:
38,169
themean
Author by

themean

Updated on July 26, 2022

Comments

  • themean
    themean almost 2 years

    I would like to use Qt creator and Cmake together (please, don't ask me about my motivation, accept this as a given.)

    I successfully set up Qt creator to use cmake "Cmake": see this, this and this documents regarding how I did that.

    I successfully create hello world project, but I can't create files in project, only add existing files to project tree and after that adding it to cmake list. Standard operation of Qt creator "Add New..." doesn't work and I can't find why.

    Is there anybody who uses Qt creator and "Cmake" together? Is the combination actually possible?

    Note: I'm using Qt creator v2.4.1.