1

I'm working on a very basic C++ application using Qt5.6 with CMake. Git Repo Here.

My problem? My main.cpp can #include Qt classes like <QtCore/QObject>, but my defined classes cannot.

error: QtCore/QObject: No such file or directory

I have downloaded the latest version of Qt with Qt Creator here.

Screenshot depicting the Qt Creator IDE with the error message "QtCore/QObject: No such file or directory" emanating from a user-defined header file.

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

CMakeLists.txt

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
    )

main.cpp

#include <iostream>

#include <QtWidgets/QApplication>
#include <QtCore/QObject>
//#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;
}

User defined class: TcpServer.cpp

#include "TcpServer.h"
#include <QtNetwork/QTcpSocket>
#include <QtCore/QByteArray>
#include <QtCore/QtDebug>
#include <QtCore/QString>
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>

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->listen(QHostAddress::Any, 9999))
        qDebug() << "Server could not start";
    else
        qDebug() << "Server started!";
    vCapture = new VideoCapture(0);
}
void TcpServer::newConnection()
{
    QTcpSocket *socket = server->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->write(ContentType);
    std::vector<uchar> buff;
    Mat img; //OpenCV Material
    while (1) {
        // Image to Byte Array via OPENCV Method
        buff.clear();buff.empty();
        vCapture->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->write(BoundaryString);
        socket->write(CurrentImg); // Write The Encoded Image
        socket->flush();
    }
}

User Defined Class Header which throws the error: TcpServer.h

#ifndef TCPSERVER_H
#define TCPSERVER_H

#include <QtCore/QObject>
#include <QtNetwork/QTcpServer>
#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

For reference I am working on these two related stack overflow questions.

How to Create a HTTP MJPEG Streaming Server With QTcp-Server Sockets?

Error while using QTcpSocket.

Community
  • 1
  • 1
Daniel Arnett
  • 493
  • 2
  • 8
  • 18
  • show the content of the project(CMake) file – ixSci May 11 '16 at 16:04
  • It's all in the Git repository above. Here is the CMakeLists https://github.com/DanielArnett/opencv-mjpg/blob/master/CMakeLists.txt#L3 – Daniel Arnett May 11 '16 at 16:05
  • 2
    It is a rule of Stack Overflow that code should be included into question post itself, not referenced to other site. See [ask] for more info. – Tsyvarev May 11 '16 at 16:09
  • I will go through the How to Ask, thank you for your feedback @Tsyvarev. The question has been updated. – Daniel Arnett May 11 '16 at 16:14
  • 1
    I'm not sure, but why aren't you using a Qt Project? It uses QMake instead of CMake, and with better Qt support. – user1887915 May 11 '16 at 17:06
  • @user1887915 My team uses CMake, so it's easier for me to port existing projects. For this specific project I was having trouble including OpenCV, so I reverted to CMake. – Daniel Arnett May 11 '16 at 17:12
  • @user1887915 Perhaps the best solution is to just switch to qmake, but I can't figure out how to include opencv with it.... – Daniel Arnett May 11 '16 at 17:23
  • `CONFIG += link_pkgconfig PKGCONFIG += opencv` provided by http://stackoverflow.com/a/3517716/1887915 – user1887915 May 11 '16 at 17:30
  • error: cannot find -lippicv – Daniel Arnett May 11 '16 at 17:37
  • Side note: You're not supposed to `#include `: it hides project configuration issues and they surface during link time. You should `#include ` or simply `#include ` as a whole. That way the project won't compile if the configuration is wrong. Any Qt examples that include `` are misleading. – Kuba hasn't forgotten Monica May 11 '16 at 17:44
  • I appreciate the note @KubaOber, thanks! The project has been updated. – Daniel Arnett May 11 '16 at 17:49
  • @user1887915 I believe that is the way to link opencv on OSX. On Ubuntu Linux the appropriate way to link opencv is with `LIBS += \`pkg-config opencv --libs\``, according to this excellent intro to qt and opencv guide: https://code.google.com/archive/p/qt-opencv-multithreaded/wikis/Documentation.wiki – Daniel Arnett May 11 '16 at 17:58

1 Answers1

4

Add find_package(Qt5Core) in addition to find_package(Qt5Widgets) and add the following lines:

include_directories(${Qt5Widgets_INCLUDE_DIRS})
include_directories(${Qt5Core_INCLUDE_DIRS})

And do not forget to add Qt5::Core to the target_link_libraries. BTW, I see you are using QtNetwork; you will have to make the same steps for that module too. And for every other module you will use.

ixSci
  • 13,100
  • 5
  • 45
  • 79
  • Made changes, updated Github to match, still have the same error. – Daniel Arnett May 11 '16 at 16:19
  • Also, when I try `find_package(Qt5Widgets Qt5Core)` I get the response `error: find_package called with invalid argument "Qt5Core"`, so I broke it up into 3 statements. – Daniel Arnett May 11 '16 at 16:50
  • @DanielArnett, sorry about the mistake in the answer. Did you try to make a clean build? – ixSci May 11 '16 at 17:11
  • Cleaned CMake and cleaned the build- still no luck. – Daniel Arnett May 11 '16 at 17:15
  • @DanielArnett, try to add `REQUIRED` to `find_package` for Qt modules like you have it for OpenCV and make sure CMake is re-run. – ixSci May 11 '16 at 17:38
  • Still no luck, cleaned everything and I'm receiving the same error. – Daniel Arnett May 11 '16 at 18:02
  • @DanielArnett, well, it is next to impossible. If you did what you said then it found Qt — otherwise there would be errors on CMake run. And if it found Qt it is not possible that the includes weren't set. You CMake file is right, I've cloned your repo, removed opencv(I don't have it) and it compiles fine. – ixSci May 11 '16 at 18:07
  • Thanks for going the distance. I've come to believe that it's a combination of my build environment and Qt/CMake compatibility issues. Switching to qmake now. – Daniel Arnett May 11 '16 at 18:33