Qt World Summit: Submit your Presentation

uic + CMake + GCC + custom widgets + out-of-source problem

  • Hello. I have a problem with a CMake out-of-source build not setting up the uic-generated header so that it can reach my in-source header with a custom widget. Interestingly, MSVC manages to find it. Do you have any hints? I guess I could throw the source directory into the include path, but that doesn't seem clean.

    Here's a minimized example. It does nothing, but it compiles under MSVC 19.25.28614 while not compiling under GCC 10.1.0 (both with out-of-source CMake 3.16/3.17 builds), and I think it illustrates the problem. Of course, in the program I was working on the BetterButton equivalents actually do something, the UI is utilized, etc..

    I tried to search for existing information of the internet, but I didn't manage to come up with a combination of keywords that would get anywhere close.


    #ifndef HEADER_H
    #define HEADER_H
    #include <QPushButton>
    using BetterButton = QPushButton;
    #endif //HEADER_H


    cmake_minimum_required(VERSION 3.14)
    find_package(Qt5 COMPONENTS Widgets Gui REQUIRED)
    set_target_properties(minitest PROPERTIES AUTOUIC ON AUTOMOC ON AUTORCC ON)
    target_link_libraries(minitest PRIVATE Qt5::Core Qt5::Widgets Qt5::Gui)


    #include "ui_ui.h"
    int main() {
            return 0;


    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <widget class="QWidget" name="Form">
      <property name="geometry">
      <property name="windowTitle">
      <widget class="BetterButton" name="pushButton">
       <property name="geometry">
       <property name="text">

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    From the looks of it your forgot to enable CMAKE_AUTOUIC.

    See here.

  • @SGaist Thank you for your assistance.
    Firstly, adding the three lines setting those global variables does not eliminate the error in question.
    Secondly, do they change anything given the following line was already present?

    set_target_properties(minitest PROPERTIES AUTOUIC ON AUTOMOC ON AUTORCC ON)

  • I've just realized that the error message would probably be helpful.

    In file included from (scrubbed)/min-s/main.cpp:1:
    (scrubbed)/min-m/minitest_autogen/include/ui_ui.h:15:10: fatal error: betterbutton.h: No such file or directory
       15 | #include "betterbutton.h"
          |          ^~~~~~~~~~~~~~~~
    compilation terminated.

  • Lifetime Qt Champion

    @stan423321 said in uic + CMake + GCC + custom widgets + out-of-source problem:


    Where is it located?

  • All 4 files described in opening post are in the min-s directory. Next to it there's a min-m directory where I build it.

  • Lifetime Qt Champion

    You don't seem to add the directory to the include search paths.

  • Is that the standard practice for working with .ui files? It doesn't seem right to me, this breaks the separation of system headers and project ones. Also, I can't help but wonder why MSVC wouldn't need this...

  • Lifetime Qt Champion

    I am not sure to follow your question here.

    You told us that you have files in subdirectories but yet you do not provide the necessary include paths for the compiler to find the headers in your sub folder.

  • Well, if I include them from .cpp files, both GCC and MSVC automatically know to look in the folder with the source file. I have never before used the include path for in-project files, all the advice I have followed before recommended using include paths just for libraries (e.g. Qt). Not to mention, in a more complex setup, it would break relative paths.

  • Lifetime Qt Champion

    It depends all on how you write your includes in your code.

    cmake provides the CMAKE_INCLUDE_CURRENT_DIR variable so you can ensure that your project folder is used for the includes.

  • Well, huh. I thought I tried that one with the bigger project before, but it does work with the test program here, so that must have been something else. Thank you for your assistance.