I am trying to develop a user interface in Qt, which is connected to some logic. I have successfully sent signals from the logic to the user interface, but now I require to do it the other way around too. I face a QMainWindow: No such file or directory
error when I try to compile the project.
I know for sure that it is a problem of including the classes to each other, and therefore I have tried to perform some forward declaration. But I have been unable to do it properly. I have tried many ways but I don't seem to achieve it.
I tried to create an empty class in the client's header file, class QTGUI;
and include the qtgui.hpp
only in client.cpp
, and the same analogous thing with QTGUI
files. What am I doing wrong?
Thank you very much in advance.
Updated CMakeLists and source codes. The original stuff is below.
Main CMakeLists
cmake_minimum_required(VERSION 2.6)
project(backendCode)
find_package(Boost COMPONENTS system)
include_directories(${Boost_INCLUDE_DIR})
SET(THREADLIBS "-lpthread -lboost_thread")
SET(FSLIBS "-lboost_filesystem -lboost_system")
add_subdirectory(gui)
add_subdirectory(server)
GUI CMakeLists: In comparison with the original code, I included the client stuff here.
cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
project(QtGui)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Network REQUIRED)
include_directories(${Qt5Widgets_INCLUDE_DIRS})
add_definitions(${QT_DEFINITIONS})
add_executable(main main.cpp qtgui.cpp ../common/inotify.cpp
../client/client.cpp ../common/message.cpp)
target_link_libraries(main Qt5::Widgets Qt5::Network ${Boost_LIBRARIES}
${THREADLIBS})
QTGUI.h
#ifndef QTGUI_H
#define QTGUI_H
#include <QMainWindow>
#include <QFileSystemWatcher>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/signals2.hpp>
#include "../common/inotify.hpp"
class Client;
namespace Ui {
class QTGUI;
}
class QTGUI : public QMainWindow
{
Q_OBJECT
public:
...
private:
boost::shared_ptr<Client> client_;
Ui::QTGUI* ui;
};
#endif // QTGUI_H
QTGUI.cpp
#include "QTGUI.h"
#include "ui_qtgui.h"
#include <iostream>
#include <QHostAddress>
#include <QRegExp>
#include <QIntValidator>
#include <QFileDialog>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include "../client/client.hpp"
...
CLIENT.HPP
#ifndef CLIENT_HPP
#define CLIENT_HPP
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/signals2.hpp>
#include "../common/message.hpp"
class QTGUI;
class Client : public boost::enable_shared_from_this<Client>
{
{
public:
...
private:
boost::shared_ptr<QTGUI> ui_;
};
#endif // CLIENT_H
CLIENT.CPP
#include <boost/bind.hpp>
#include <cstdio> /* sprintf */
#include "client.hpp"
#include "../gui/QTGUI.h"
#include <unistd.h>
Client::Client():
...
Original code
I have the following in my main CMakeLists:
cmake_minimum_required(VERSION 2.6)
project(backendCode)
find_package(Boost COMPONENTS system)
include_directories(${Boost_INCLUDE_DIR})
SET(BOOSTHREAD "-lpthread -lboost_thread")
SET(FSLIBS "-lboost_filesystem -lboost_system")
add_subdirectory(gui)
add_subdirectory(client)
add_subdirectory(server)
The following, in the gui CMakeLists:
cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
project(QtGUI)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Network REQUIRED)
include_directories(${Qt5Widgets_INCLUDE_DIRS})
add_definitions(${QT_DEFINITIONS})
add_executable(main main.cpp qtgui.cpp ../common/inotify.cpp)
target_link_libraries(main client Qt5::Widgets Qt5::Network ${Boost_LIBRARIES}
${BOOSTHREAD})
In the qtgui.h header file I have the following code:
#ifndef QTGUI_H
#define QTGUI_H
#include <QMainWindow>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/signals2.hpp>
#include "../client/client.hpp"
namespace Ui {
class QTGUI;
}
class QTGUI : public QMainWindow
{
Q_OBJECT
...
private:
boost::shared_ptr<Client> client_;
};
#endif
Finally, in the client.hpp file I have the following code:
#ifndef CLIENT_HPP
#define CLIENT_HPP
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/signals2.hpp>
#include "../gui/qtgui.h"
class QTGUI;
class Client : public boost::enable_shared_from_this<Client>
{
...
private:
boost::shared_ptr<QTGUI> ui_;
};
#endif
EDIT: Added the namespace
thing into the qtgui.h
.