I have installed the last QT Creator, running a gived example code, but i get
error: 'QGLFormat' file not found and
error: 'QGLWidget' file not found
My pro file looks like
GLEW_PATH = ../ext/glew-1.9.0
GLM_PATH = ../ext/glm-0.9.4.1
TEMPLATE = app
TARGET = myfilename
LIBS += -Wl,-rpath $${GLEW_PATH}/lib -L$${GLEW_PATH}/lib
LIBS += -lGLEW -lGL -lGLU -lm
INCLUDEPATH += $${GLEW_PATH}/include $${GLM_PATH}
SOURCES = main.cpp viewer.cpp
HEADERS = viewer.h
CONFIG += qt opengl warn_on thread uic4 release
QT += core gui opengl xml widgets
The errors happen here in my only header viewer.h, but others includes look fine
// GLEW lib: needs to be included first!!
#include <GL/glew.h>
// OpenGL library
#include <GL/gl.h>
// OpenGL Utility library
#include <GL/glu.h>
// OpenGL Mathematics
#include <glm/glm.hpp>
#include <QGLFormat> //Error here
#include <QGLWidget> //and here
#include <QMouseEvent>
#include <QKeyEvent>
...
with a main quite simple
#include <qapplication.h>
#include <QString>
#include "viewer.h"
int main(int argc,char** argv) {
QApplication application(argc,argv);
Viewer viewer;
viewer.show();
return application.exec();
}
I've been using PyQt for years now and had to recode a project in C++ using CLion. I have managed (after awhile) to get the code building on my MacBookPro, but when I move the project to Windows 10, all hell broke loose! I've reloaded mingw with the x86 version and gotten everything to work except MOC and AUTOUIC. The only lines I changed in the CMakeLists.txt file between Oses were the ones that pointed to the Qt install. As I said, I'm new to all this in C++ and may have some 'mistakes' in my makefile, but it works on OSX but not in Windows!
I am able to compile ui and resources files manually and get the build to compile, but I don't know how to resolve this issue.
Any help and guidance would be greatly appreciated!
====================[ Build | crapsStarter | Debug ]============================
C:\Users\a.fireheart.CLion2019.3\system\cygwin_cmake\bin\cmake.exe
--build /cygdrive/c/Users/a.fireheart/CLionProjects/crapsStarter/cmake-build-debug
--target crapsStarter -- -j 6 [ 14%] Automatic MOC for target crapsStarter
AutoMoc subprocess error
------------------------ The moc process failed to compile "/cygdrive/c/Users/a.fireheart/CLionProjects/crapsStarter/craps.h"
into
"/cygdrive/c/Users/a.fireheart/CLionProjects/crapsStarter/cmake-build-debug/crapsStarter_autogen/EWIEGA46WW/moc_craps.cpp".
Command
------- C:/Qt/5.14.1/winrt_x64_msvc2017/bin/moc.exe -I/cygdrive/c/Users/a.fireheart/CLionProjects/crapsStarter/cmake-build-debug
-I/cygdrive/c/Users/a.fireheart/CLionProjects/crapsStarter -I/cygdrive/c/Users/a.fireheart/CLionProjects/crapsStarter/cmake-build-debug/crapsStarter_autogen/include
-IC:/Qt/5.14.1/winrt_x64_msvc2017/include -IC:/Qt/5.14.1/winrt_x64_msvc2017/include/QtCore -IC:/Qt/5.14.1/winrt_x64_msvc2017/./mkspecs/winrt-x64-msvc2017 -IC:/Qt/5.14.1/winrt_x64_msvc2017/include/QtGui -IC:/Qt/5.14.1/winrt_x64_msvc2017/include/QtANGLE -IC:/Qt/5.14.1/winrt_x64_msvc2017/include/QtWidgets -I/usr/lib/gcc/x86_64-pc-cygwin/9.2.0/include/c++ -I/usr/lib/gcc/x86_64-pc-cygwin/9.2.0/include/c++/x86_64-pc-cygwin -I/usr/lib/gcc/x86_64-pc-cygwin/9.2.0/include/c++/backward -I/usr/lib/gcc/x86_64-pc-cygwin/9.2.0/include -I/usr/include -I/usr/include/w32api -DQT_CORE_LIB -DQT_GUI_LIB -DQT_WIDGETS_LIB --include /cygdrive/c/Users/a.fireheart/CLionProjects/crapsStarter/cmake-build-debug/crapsStarter_autogen/moc_predefs.h
-o /cygdrive/c/Users/a.fireheart/CLionProjects/crapsStarter/cmake-build-debug/crapsStarter_autogen/EWIEGA46WW/moc_craps.cpp
/cygdrive/c/Users/a.fireheart/CLionProjects/crapsStarter/craps.h
Output
make[3]: * [CMakeFiles/crapsStarter_autogen.dir/build.make:58:
CMakeFiles/crapsStarter_autogen] Error 1 make[2]:
[CMakeFiles/Makefile2:104: CMakeFiles/crapsStarter_autogen.dir/all]
Error 2 make[1]: [CMakeFiles/Makefile2:84:
CMakeFiles/crapsStarter.dir/rule] Error 2 make: * [Makefile:118:
crapsStarter] Error 2
***************** My CMakeLists.txt file content *******************************
cmake_minimum_required(VERSION 3.15)
project(crapsStarter)
set(CMAKE_CXX_STANDARD 17)
#set(RESOURCES crapsResources.qrc)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#set(CMAKE_AUTOUIC ON)
include_directories(cmake-build-debug/crapsStarter_autogen/include)
# Tell cmake where Qt is located
set(Qt5_DIR "C:/Qt/5.14.1/winrt_x64_msvc2017/lib/cmake/Qt5")
set(QT_INCLUDES "C:/Qt/5.14.1/winrt_x64_msvc2017/include")
MESSAGE("QT_INCLUDES: ${QT_INCLUDES}")
# Include a library search using find_package()
# via REQUIRED, specify that libraries are required
set(Qt5 NEED)
find_package(Qt5 COMPONENTS Core Gui Widgets REQUIRED)
set(SOURCE_FILES craps.cpp die.cpp crapsGame.cpp crapsResources.cpp)
add_executable(crapsStarter ${SOURCE_FILES})
# specify which libraries to connect
target_link_libraries(${PROJECT_NAME} Qt5::Core)
target_link_libraries(${PROJECT_NAME} Qt5::Gui)
target_link_libraries(${PROJECT_NAME} Qt5::Widgets)
craps.h
//
// Created by Arana Fireheart on 2/2/20.
//
#ifndef CRAPSSTARTER_CRAPS_H
#define CRAPSSTARTER_CRAPS_H
#include "ui_CrapsMainWindow.h"
#include "die.h"
#include <QMainWindow>
class CrapsMainWindow : public QMainWindow, private Ui::CrapsMainWindow {
Q_OBJECT
public:
CrapsMainWindow(QMainWindow *parent = nullptr);
void printStringRep();
void updateUI();
private:
Die die1, die2;
bool firstRoll = true;
int winsCount = 0;
public Q_SLOTS:
void rollButtonClickedHandler();
};
#include "moc_craps.cpp"
#endif //CRAPSSTARTER_CRAPS_H
craps.cpp
#include <iostream>
#include <stdio.h>
//#include <QApplication>
//#include <QWidget>
//#include <QGridLayout>
//#include <QPushButton>
//#include <QLabel>
//#include <QPixmap>
#include "die.h"
#include "craps.h"
#include "ui_CrapsMainWindow.h"
CrapsMainWindow :: CrapsMainWindow(QMainWindow *parent) {
// Build a GUI window with two dice.
setupUi(this);
Die die1, die2;
bool firstRoll = true;
int winsCount = 0;
QObject::connect(rollButton, SIGNAL(clicked()), this, SLOT(rollButtonClickedHandler()));
}
void CrapsMainWindow::printStringRep() {
// String representation for Craps.
char buffer[25];
int length = sprintf(buffer, "Die1: %i\nDie2: %i\n", die1.getValue(), die2.getValue());
printf("%s", buffer);
}
void CrapsMainWindow::updateUI() {
// printf("Inside updateUI()\n");
std::string die1ImageName = ":/dieImages/" + std::to_string(die1.getValue());
std::string die2ImageName = ":/dieImages/" + std::to_string(die2.getValue());
die1UI->setPixmap(QPixmap(QString::fromStdString(die1ImageName)));
die2UI->setPixmap(QPixmap(QString::fromStdString(die2ImageName)));
currentBankValueUI->setText(QString::fromStdString("100"));
}
// Player asked for another roll of the dice.
void CrapsMainWindow::rollButtonClickedHandler() {
//void Craps::rollButtonClickedHandler() {
printf("Roll button clicked\n");
die1.roll();
die2.roll();
printStringRep();
updateUI();
}
I ended up formatting the computer to ubuntu 16.04
Solutions I've tried based on the answers:
Just deleted 'using namespace std;' line and didn't work.
I put that line before and after '#include ', neither
worked.
Thanks Yksisarvinen
Deleted all files other than QT's defaults app (and the images, of course)
tried this, but got the same error
//#include "mainwindow.h"
//#include <QApplication>
#include <QtCore>
int main() { qDebug() << "Hello, world!"; }
/*
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
*/
Thanks Kuba Ober
Added the following line to the .pro file. This solution worked for compiling using QT
INCLUDEPATH +=/usr/include
but I still can't compile using the terminal (this is a part of my history)
cat main.cpp
g++ main.cpp
echo $LD_LIBRARY_PATH
g++ main.cpp
LD_LIBRARY_PATH='' g++ main.cpp
export LD_LIBRARY_PATH=''
echo $LD_LIBRARY_PATH
g++ main.cpp
ls /usr/include/c++/7/
echo $CXX
echo $CC
echo $CC=clang
export CC=clang
export CXX=clang++
echo $CC=clang
echo $CC
g++ main.cpp
echo $INCLUDEPATH
export INCLUDEPATH = /usr/include
export INCLUDEPATH=/usr/include
echo $INCLUDEPATH
ls
g++ main.cpp
echo $INCLUDEPATH
history
I can work now (and probably I'll format ubuntu on vacations), but I don't want to mark this as solved since there is still something not working propperly.
Thanks Marcelo Cardenas (a friend from university)
Thanks for the tips anyway, hope we can solve this
I have this QT app:
https://drive.google.com/file/d/15d4mYLgJcKOB5tpEwfuFBPXETUbR8CFK/view?usp=sharing
Which used to work. Now I'm getting
this.
500+ errors of supposedly supposedly undefined functions.
I don't know where to even look for the error.
I'm currently working with:
OS: Ubuntu 18.04.1 LTS
Kernel: 4.16.0-041600-generic
Cuda: Built on Tue_Jun_12_23:07:04_CDT_2018 Cuda compilation tools,
release 9.2, V9.2.148
gcc: (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
QMake: version 3.1
Qt version: 5.9.6
OpenGL version: 3.0 Mesa 18.0.5
OpenCV version: 4.0.0 (obtained by the command cat
/home/pablo/OpenCV-3.0.0/OpenCV-3.0.0-master/opencv/build/OpenCVConfig.cmake
)
Here is the .pro file:
#-------------------------------------------------
#
# Project created by QtCreator 2018-10-31T18:03:31
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Ayudantia2
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
And the header
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <ui_mainwindow.h>
#include <set>
//#include <mainwindow.h>
#include <QMainWindow>
#include <QFileDialog> //para buscar archivos por ventana
#include <QMessageBox> //para mensajes del sistema
#include <QImage> //para manipular imagenes basicamente(mas fome que opencv)
#include <cstdlib>
using namespace std;
#include <iostream>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void cargarTexto(QString);
void cargarImagen(int area);
void buscarImagen1();
void buscarImagen2();
void buscarImagen3();
void mostrarImagen4();
void mostrarImagen5();
void mostrarImagen6();
void filtrarImagen();
void ponderar();
void colorPlano();
signals:
void textoListo(QString);
int imagenLista(int);
int filtrar1(int);
int filtrar2(int);
int filtrar3(int);
int valueChanged();
void cambiocolor();
QString ruta1(QString);
QString ruta2(QString);
QString ruta3(QString);
private slots:
private:
Ui::MainWindow *ui;
QImage imagenes[8];
};
#endif // MAINWINDOW_H
and this id the mainWindow
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget* parent):
QMainWindow(parent),
ui(new Ui::MainWindow){
ui->setupUi(this);
QObject::connect(ui->actionCargar_imagen_1, SIGNAL(triggered()),this, SLOT(buscarImagen1()));
QObject::connect(ui->actionCargar_imagen_2, SIGNAL(triggered()),this, SLOT(buscarImagen2()));
QObject::connect(ui->actionCargar_imagen_3, SIGNAL(triggered()),this, SLOT(buscarImagen3()));
QObject::connect(ui->Fusion, SIGNAL(valueChanged(int)),this, SLOT(ponderar()));
QObject::connect(this, SIGNAL(filtrar1(int)),this, SLOT(mostrarImagen4()));
QObject::connect(this, SIGNAL(filtrar2(int)),this, SLOT(mostrarImagen5()));
QObject::connect(this, SIGNAL(filtrar3(int)),this, SLOT(mostrarImagen6()));
QObject::connect(this, SIGNAL(imagenLista(int)),this, SLOT(cargarImagen(int)));
QObject::connect(ui->btn_Filtrar, SIGNAL(released()),this, SLOT(filtrarImagen()));
QObject::connect(ui->RedSlider, SIGNAL(valueChanged(int)),this,SLOT(colorPlano()));
QObject::connect(ui->BlueSlider, SIGNAL(valueChanged(int)),this,SLOT(colorPlano()));
QObject::connect(ui->GreenSlider, SIGNAL(valueChanged(int)),this,SLOT(colorPlano()));
QObject::connect(this, SIGNAL(cambiocolor()),this, SLOT(mostrarImagen7()));
QImage imagenes[8];
for(int i=0;i<8;i++){
imagenes[i]=QImage();
}
}
////////////////////////////////////////////////////////////////
void MainWindow::buscarImagen1(){
QString fileName = QFileDialog::getOpenFileName(this, tr("Abrir Imagen"), "./", tr("Imagen (*.png *.jpg *.jpeg *.bmp *.ppm);; All files (*.*)"));
if(fileName == "") return;
//emit ruta1(fileName);
emit textoListo(fileName);
imagenes[0] = QImage(fileName);
imagenes[0]= imagenes[0].scaled(170,170);
if(imagenes[0].isNull()){
QMessageBox::information(this, tr("Error de carga!!"),tr("No se puede cargar %1. ").arg(fileName));
imagenes[0] = QImage();
return;
}
ui->Ruta1->setText(fileName);
emit imagenLista(0);
}
void MainWindow::buscarImagen2(){
QString fileName = QFileDialog::getOpenFileName(this, tr("Abrir Imagen"), "./", tr("Imagen (*.png *.jpg *.jpeg *.bmp *.ppm);; All files (*.*)"));
if(fileName == "") return;
//emit ruta2(fileName);
emit textoListo(fileName);
imagenes[1] = QImage(fileName);
imagenes[1]= imagenes[1].scaled(170,170);
if(imagenes[1].isNull()){
QMessageBox::information(this, tr("Error de carga!!"),tr("No se puede cargar %1. ").arg(fileName));
imagenes[1] = QImage();
return;
}
ui->Ruta2->setText(fileName);
emit imagenLista(1);
}
void MainWindow::buscarImagen3(){
QString fileName = QFileDialog::getOpenFileName(this, tr("Abrir Imagen"), "./", tr("Imagen (*.png *.jpg *.jpeg *.bmp *.ppm);; All files (*.*)"));
if(fileName == "") return;
//emit ruta3(fileName);
emit textoListo(fileName);
imagenes[2] = QImage(fileName);
imagenes[2]= imagenes[2].scaled(170,170);
if(imagenes[2].isNull()){
QMessageBox::information(this, tr("Error de carga!!"),tr("No se puede cargar %1. ").arg(fileName));
imagenes[2] = QImage();
return;
}
ui->Ruta3->setText(fileName);
emit imagenLista(2);
}
///////////////////////////////////////////////////////////////
void MainWindow::mostrarImagen4(){
emit imagenLista(4);
}
void MainWindow::mostrarImagen5(){
emit imagenLista(5);
}
void MainWindow::mostrarImagen6(){
emit imagenLista(6);
}
///////////////////////////////////////////////////////////////scrollArea_1
void MainWindow::cargarTexto(QString texto){
ui->Ruta1->setText(texto);
}
///////////////////////////
void MainWindow::cargarImagen(int area) {
QImage *out = new QImage(imagenes[area]);
QLabel *label = new QLabel;
label->setPixmap(QPixmap::fromImage(*out, Qt::AutoColor));
if (area == 0) ui->scrollArea_1->setWidget(label);
if (area == 1) ui->scrollArea_2->setWidget(label);
if (area == 2) ui->scrollArea_3->setWidget(label);
if (area == 3) ui->scrollArea_4->setWidget(label);
if (area == 4) ui->scrollArea_5->setWidget(label);
if (area == 5) ui->scrollArea_6->setWidget(label);
if (area == 6) ui->scrollArea_7->setWidget(label);
if (area == 7) ui->scrollArea_8->setWidget(label);
}
//////////////////////////////////////////////////////////////
void MainWindow::filtrarImagen(){
int R,G,B;
QColor pixelRGB;
imagenes[4]=imagenes[0];
if(imagenes[4].isNull()) imagenes[4] = QImage();
imagenes[5]=imagenes[1];
if(imagenes[5].isNull()) imagenes[5] = QImage();
imagenes[6]=imagenes[2];
if(imagenes[6].isNull()) imagenes[6] = QImage();
for(int k=0;k<3;k++){
if (imagenes[k].isNull()) continue;
for(int i=0;i<imagenes[k].height();i++){
for(int j=0;j<imagenes[k].width();j++){
pixelRGB=imagenes[k].pixelColor(i,j);
R = pixelRGB.red();
G = pixelRGB.green();
B = pixelRGB.blue();
if(ui->Tresched_chBox->isChecked()){
if(R < ui->RedSlider->value()){
R=0;
}
if(G < ui->GreenSlider->value()){
G=0;
}
if(B < ui->BlueSlider->value()){
B=0;
}
}
if(ui->Tresched_chBox->isChecked()==false){
if(R > ui->RedSlider->value()){
R=0;
}
if(G > ui->GreenSlider->value()){
G=0;
}
if(B > ui->BlueSlider->value()){
B=0;
}
}
//printf("(%i,%i,%i)",R,G,B);
imagenes[k+4].setPixelColor(i,j,QColor(R,G,B,0));
}
}
}
emit filtrar1(4);
emit filtrar2(5);
emit filtrar3(6);
}
//////////////////////////////////////////////////////////////
void MainWindow::ponderar(){
if((imagenes[1].isNull() == false) and (imagenes[2].isNull() == false)){
imagenes[3] = imagenes[2];
float ponderacion = (float) ui->Fusion->value();
QColor pixelRGB1,pixelRGB2;
int R1,G1,B1,R2,G2,B2,R3,G3,B3;
int alfa1,alfa2,alfa3;
for(int i=0;i<imagenes[1].height();i++){
for(int j=0;j<imagenes[1].width();j++){
pixelRGB1=imagenes[1].pixelColor(i,j);
R1 = pixelRGB1.red();
G1 = pixelRGB1.green();
B1 = pixelRGB1.blue();
alfa1 = pixelRGB1.alpha();
pixelRGB2=imagenes[2].pixelColor(i,j);
R2 = pixelRGB2.red();
G2 = pixelRGB2.green();
B2 = pixelRGB2.blue();
alfa2 = pixelRGB2.alpha();
R3=(R1*ponderacion/100.0 + R2*(100.0-ponderacion)/100.0);
G3=(G1*ponderacion/100.0 + G2*(100.0-ponderacion)/100.0);
B3=(B1*ponderacion/100.0 + B2*(100.0-ponderacion)/100.0);
alfa3=(alfa1*ponderacion/100.0 + alfa2*(100.0-ponderacion)/100.0);
//printf("(%i,%i,%i)",R3,G3,B3);
imagenes[3].setPixelColor(i,j,QColor(R3,G3,B3,alfa3));
}
}
emit imagenLista(3);
}
}
//////////////////////////////////////////////////////////////
void MainWindow::colorPlano(){
imagenes[7] = QImage(175,175,QImage::Format_RGB32);
int R,G,B;
R = ui->RedSlider->value();
G = ui->GreenSlider->value();
B = ui->BlueSlider->value();
for(int i=0;i<imagenes[7].height();i++){
for(int j=0;j<imagenes[7].width();j++){
imagenes[7].setPixelColor(i,j,QColor(R,G,B));
}
}
emit cambiocolor();
emit imagenLista(7);
}
//////////////////////////////////////////////////////////////
MainWindow::~MainWindow(){
delete ui;
}
/////////////////////////////////////////////
The rest of the code is on the link on the top of this question.
Thank you for helping me, I'm new to Ubuntu, qt and C++ and I want to improve, but it's quite frustrating when every solution generates another error.
It seems that your source folder contains obsolete build products from another system. Modern Qt Creator never puts those output files inside of the source folders. Close Qt Creator, then remove all files other than the below:
Ayudantia2.pro
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
marge.jpeg
pdi.jpeg
The files above must be the only files left in the source folder. Make sure you don't miss the hidden files, specifically remove:
.qmake.stash remove
Then open Qt Creator, open the project, configure it for the current Qt version, and attempt building again.
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.
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.
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.
My goal is to run a simple OpenCV contribute (extra) module in Qt 5.5.1.
Steps:
1) created a Qt Application app;
2) changed the main.cpp file to :
#include <QApplication>
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <cstring>
#include <QDebug>
using namespace std;
using namespace cv;
int main(int argc, char*argv[])
{
qDebug() <<"start!" << endl;
QApplication a(argc, argv);
string trackingAlg = "KCF";
MultiTracker trackers(trackingAlg);
qDebug() <<"success!" << endl;
return a.exec();
}
and here is my .pro file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = tracker4
CONFIG += c++11
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /usr/local/include/opencv
LIBS += `pkg-config opencv --libs`
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_videoio -lopencv_tracking
The program compiles successfully, but in the runtime, it crashes without even running the main() function. I don't get such an error when I run this in Code::Blocks but in Qt I still have this issue.
I am using Qt 5.5.1 and Ubuntu 14.04.
Can anyone generate this "seg fault" error in his/her machine?
For QT 5, build the OpenCV with QT=OFF.
For QT 4, build the OpenCV with QT=ON.