为什么我的C ++类不能使用Qt?

时间:2016-05-11 15:44:16

标签: c++ qt cmake qt-creator

我正在使用Qt5.6和CMake在一个非常基本的C ++应用程序上工作。 Git Repo Here

我的问题?我的main.cpp#include个Qt类,如<QtCore/QObject>,但我定义的类不能。

error: QtCore/QObject: No such file or directory

我已下载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.

这可能是一个不正确设置的Qt环境吗?我不明白main.cpp如何访问Qt,但我定义的类不能。{/ p>

的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;
}

用户定义的类: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();
    }
}

用户定义的类标头,它会抛出错误: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

作为参考,我正在处理这两个相关的堆栈溢出问题。

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

Error while using QTcpSocket.

1 个答案:

答案 0 :(得分:4)

除了find_package(Qt5Core)之外,添加find_package(Qt5Widgets)并添加以下行:

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

不要忘记将Qt5::Core添加到target_link_libraries。 顺便说一句,我看到你正在使用QtNetwork;您还必须为该模块执行相同的步骤。对于您将使用的每个其他模块。