Why can't my C++ classes use Qt?
<p>I'm working on a very basic C++ application using Qt5.6 with CMake. <a href="https://github.com/DanielArnett/opencv-mjpg" rel="nofollow noreferrer">Git Repo Here</a>.</p>

<p>My problem? My <code>main.cpp</code> can <code>#include</code> Qt classes like <code>&lt;QtCore/QObject&gt;</code>, but my defined classes cannot.</p>

<pre><code>error: QtCore/QObject: No such file or directory
</code></pre>

<p>I have downloaded the <a href="http://www.qt.io/download-open-source/" rel="nofollow noreferrer">latest version of Qt with Qt Creator here</a>.</p>

<p><a href="https://i.stack.imgur.com/XNAFe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XNAFe.png" alt="Screenshot depicting the Qt Creator IDE with the error message &quot;QtCore/QObject: No such file or directory&quot; emanating from a user-defined header file."></a></p>

<p>Could this be an improperly set up Qt environment? I don't understand how <code>main.cpp</code> can access Qt but my defined classes cannot.</p>

<p>CMakeLists.txt</p>

<pre><code>cmake_minimum_required(VERSION 2.8.11)

project(testproject)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

############ OpenCV PACKAGE #########
set(BUILD_SHARED_LIBS ON)
set(OpenCV_FIND_QUIETLY FALSE)
find_package( OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )

# Find the QtWidgets library
find_package(Qt5Widgets)
qt5_wrap_cpp(tcp_hdr_moc ${PROJECT_SOURCE_DIR}/TcpServer.h)
# Tell CMake to create the helloworld executable
add_executable(helloworld WIN32 main.cpp
    TcpServer.h TcpServer.cpp
    )
# Use the Widgets module from Qt 5.
target_link_libraries(helloworld Qt5::Widgets
    ${OpenCV_LIBS}
    ${PROJECT_SOURCE_DIR}/TcpServer.cpp
    ${PROJECT_SOURCE_DIR}/TcpServer.h
    )
</code></pre>

<p>main.cpp</p>

<pre><code>#include &lt;iostream&gt;

#include &lt;QtWidgets/QApplication&gt;
#include &lt;QtCore/QObject&gt;
//#include "TcpServer.h"

using namespace std;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QString test = "Hello";
    QObject test2;
    int i = 0;
//    TcpServer server;
}
</code></pre>

<p>User defined class: TcpServer.cpp</p>

<pre><code>#include "TcpServer.h"
#include &lt;QtNetwork/QTcpSocket&gt;
#include &lt;QtCore/QByteArray&gt;
#include &lt;QtCore/QtDebug&gt;
#include &lt;QtCore/QString&gt;
#include &lt;opencv2/core.hpp&gt;
#include &lt;opencv2/opencv.hpp&gt;

TcpServer::TcpServer(QObject *parent) :
QObject(parent)
{
    server = new QTcpServer(this);
    // whenever a user connects, it will emit signal
    connect(server, SIGNAL(newConnection()),
        this, SLOT(newConnection()));
    if (!server-&gt;listen(QHostAddress::Any, 9999))
        qDebug() &lt;&lt; "Server could not start";
    else
        qDebug() &lt;&lt; "Server started!";
    vCapture = new VideoCapture(0);
}
void TcpServer::newConnection()
{
    QTcpSocket *socket = server-&gt;nextPendingConnection();
    QByteArray ContentType = ("HTTP/1.0 200 OK\r\n" \
        "Cache-Control: no-cache\r\n" \
        "Cache-Control: private\r\n" \
        "Content-Type: multipart/x-mixed-replace;boundary=--boundary\r\n");
    socket-&gt;write(ContentType);
    std::vector&lt;uchar&gt; buff;
    Mat img; //OpenCV Material
    while (1) {
        // Image to Byte Array via OPENCV Method
        buff.clear();buff.empty();
        vCapture-&gt;read(img); //Read from webcam

        //TODO set the compression parameters.
        imencode(".jpg", img, buff);
        std::string content(buff.begin(), buff.end());
        QByteArray CurrentImg(QByteArray::fromStdString(content));
        QByteArray BoundaryString = ("--boundary\r\n" \
            "Content-Type: image/jpeg\r\n" \
            "Content-Length: ");
        BoundaryString.append(QString::number(CurrentImg.length()));
        BoundaryString.append("\r\n\r\n");

        socket-&gt;write(BoundaryString);
        socket-&gt;write(CurrentImg); // Write The Encoded Image
        socket-&gt;flush();
    }
}
</code></pre>

<p>User Defined Class Header which throws the error: TcpServer.h</p>

<pre><code>#ifndef TCPSERVER_H
#define TCPSERVER_H

#include &lt;QtCore/QObject&gt;
#include &lt;QtNetwork/QTcpServer&gt;
#include "opencv2/videoio.hpp"

using namespace cv;
class TcpServer : public QObject
{
    Q_OBJECT

public:
    explicit TcpServer(QObject *parent = 0);
    void newConnection();
private:
    QTcpServer* server;
    VideoCapture* vCapture;
};

#endif
</code></pre>

<p>For reference I am working on these two related stack overflow questions.</p>

<p><a href="https://stackoverflow.com/questions/33064955/how-to-create-a-http-mjpeg-streaming-server-with-qtcp-server-sockets">How to Create a HTTP MJPEG Streaming Server With QTcp-Server Sockets?</a></p>

<p><a href="https://stackoverflow.com/questions/37025383/error-while-using-qtcpsocket">Error while using QTcpSocket.</a></p>