Using CMake in Qt: location ui_*.h is not recognized by Qt Creator

1k views Asked by At

I tried to use Qt Creator to manage User Interface files *.ui:

mainwindow.cpp:

#include "mainwindow.hpp"

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget * parent)
    : QMainWindow{parent}
    , ui{new Ui::MainWindow}
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{ ; }

mainwindow.hpp:

#pragma once

#include <QMainWindow>
#include <QScopedPointer>

namespace Ui {

class MainWindow;

}

class MainWindow
        : public QMainWindow
{

    Q_OBJECT

public :

    explicit MainWindow(QWidget * parent = Q_NULLPTR);
    ~MainWindow();

private :

    QScopedPointer< Ui::MainWindow > ui;

    Q_DISABLE_COPY(MainWindow)

};

CMakeLists.txt:

cmake_minimum_required(VERSION 3.8)

project("gui" LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt5 CONFIG REQUIRED Core Widgets)

set(UI_FILES "mainwindow.ui")

set(SOURCES)
list(APPEND SOURCES "main.cpp")
list(APPEND SOURCES "mainwindow.cpp")

add_executable(${PROJECT_NAME} ${OS_BUNDLE} ${SOURCES} ${UI_FILES})

target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Widgets)

set_target_properties(${PROJECT_NAME} PROPERTIES
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED YES
    CXX_EXTENSIONS YES
    )

When I put cursor on #include "ui_mainwindow.h" filename and push F2, then I get the following error message:

error messagbox

I have investigated the problem: it origins from different locations, mentioned in file D:/Projects/build/proj/Debug/src/gui/CMakeFiles/gui.dir/CXX.includecache:

gui_autogen/include/ui_mainwindow.h
D:/Projects/proj/gui/gui_autogen/include/ui_mainwindow.h

These are two consecutive lines, which the only contains ui_mainwindow.h as substring in whole the file. Second line contains wrong location, even if I add target_include_directories(${PROJECT_NAME} PRIVATE "${PROJECT_BINARY_DIR}/gui_autogen/include") to CMakeLists.txt, then cmake either way can't generate right cache file.

What is workaround to this? I think I can edit (patch) some *.cmake files in Qt subsystem of CMake distribution. I almost sure this is closely related to CMAKE_AUTOMOC directive and other stuff.

ADDITIONAL:

Main problem is that I can't create slot from form editor's Go to slot context menu item to autogenerate slot from Qt Cretator.

1

There are 1 answers

0
Tomilov Anatoliy On

Finally I found the workaround. It is a Qt Creator's parser limitation: pointer to Ui::MainWindow should be a plain old raw pointer. That is I cannot use any RAII-wrapper, like std::unique_ptr or QScopedPointer and should manually delete Ui::MainWindow instance at destruction time. Maybe sometimes it will be fixed at least for QScopedPointer. I hope.