Add QCamera to CMakeList (ROS integrated Qt Project)
-
Hi, we are working on a Qt project that works with ROS.
We would like to add a camera view but for some reason, ROS can't compile QCamera class. It says "no such file or directory".
I thought that it happens because of CMakeList.txt, we didn't add QCamera to CMakeList and ROS system gets the libraries from it. I don't know where or how to add it so I need help.
Here is our CMakeList.txt:
cmake_minimum_required(VERSION 2.8.3) project(sabun) ## Compile as C++11, supported in ROS Kinetic and newer add_compile_options(-std=c++11) ## Find catkin macros and libraries find_package(catkin REQUIRED COMPONENTS roscpp std_msgs ) # Find Qt5 libraries find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick Widgets ) # Configuration to make Qt compiles go smoother set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) include_directories( ${CMAKE_BINARY_DIR} ) # Configure Catkin dependencies catkin_package( CATKIN_DEPENDS roscpp std_msgs ) include_directories( include ${catkin_INCLUDE_DIRS} ) ############################### # Basic, non-qt talker/listener add_executable(listener src/listener.cpp) add_executable(talker src/talker.cpp) target_link_libraries(listener ${catkin_LIBRARIES} ) target_link_libraries(talker ${catkin_LIBRARIES} ) ################################################ # Cmd line talker/listener running qt event loop add_executable(listener_qt src/listener_qt.cpp) add_executable(talker_qt src/talker_qt.cpp) # Configure Qt5 Modules # Equivalent to doing # Qt += Core # In Qt .pro file qt5_use_modules(listener_qt Core) qt5_use_modules(talker_qt Core) target_link_libraries(listener_qt ${catkin_LIBRARIES} ) target_link_libraries(talker_qt ${catkin_LIBRARIES} ) ###################################### # Talker and Listener using QML gui # Run MOC on headers of Q_OBJECT classes set(MOC_HDRS include/qml_mediator.h) qt5_wrap_cpp(MOC_SRCS ${MOC_HDRS}) # Run RCC on QRC file set(RCC_FILES src/display.qrc) qt5_add_resources(RCC_SOURCES ${RCC_FILES}) # Build executables with source, MOC result files, RCC result files add_executable(listener_qml src/listener_qml.cpp ${MOC_SRCS} ${RCC_SOURCES}) add_executable(talker_qml src/talker_qml.cpp ${MOC_SRCS} ${RCC_SOURCES}) # Configure Qt5 Modules # Equivalent to doing # Qt += Core Qml Quick # In Qt .pro file qt5_use_modules(listener_qml Core Qml Quick) qt5_use_modules(talker_qml Core Qml Quick) target_link_libraries(listener_qml ${catkin_LIBRARIES} ) target_link_libraries(talker_qml ${catkin_LIBRARIES} ) ######################################## # Talker and Listener using QWidgets gui # Run MOC on headers of Q_OBJECT classes set(MOC_HDRS include/mainwindow.h) qt5_wrap_cpp(MOC_SRCS ${MOC_HDRS}) # Run UIC on ui definitions set(UI_FILES ui/mainwindow.ui) qt5_wrap_ui(UI_SRCS ${UI_FILES}) # Build executables with source, MOC result files, UIC result files add_executable(talker_wig src/talker_wig.cpp src/mainwindow.cpp ${MOC_SRCS} ${UI_SRCS}) add_executable(listener_wig src/listener_wig.cpp src/mainwindow.cpp ${MOC_SRCS} ${UI_SRCS}) # Configure Qt5 Modules # Equivalent to doing # Qt += Core Widgets # In Qt .pro file qt5_use_modules(talker_wig Core Widgets) qt5_use_modules(listener_wig Core Widgets) target_link_libraries(talker_wig ${catkin_LIBRARIES} ) target_link_libraries(listener_wig ${catkin_LIBRARIES} )
-
@aliemrenebiler said in Add QCamera to CMakeList (ROS integrated Qt Project):
Here is what I wrote:
No, it's much easier:
change:# Find Qt5 libraries find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick Widgets )
qt5_use_modules(talker_wig Core Widgets)
into# Find Qt5 libraries find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick Widgets Multimedia )
qt5_use_modules(talker_wig Core Widgets Multimedia)
For people ending here from Google
qt5_use_modules
is obsolete, OP needs to support a very old version of CMake. The modern way is to simply usetarget_link_libraries(talker_wig Qt5::Core Qt5::Widgets Qt5::Multimedia)
-
Hi,
QCamera is part of the Qt multi multimedia module.
-
This post is deleted!
-
find_package(Qt5Multimedia REQUIRED)
-
@jsulm I think that is the right way but didn't work yet.
Here is what I wrote:
find_package(Qt5Multimedia REQUIRED QCamera QCameraViewfinder QCameraInfo QCameraImageCapture )
These are the subclasses that I use. Still has the same error about QCamera.
-
Also found this page.
It says I should add:
set(CMAKE_PREFIX_PATH "~/sw/Qt/Qt_5_4/5.4/gcc_64/")
to make CMakeList.txt see QtMultimedia. But the directory is not right for Ubuntu.
Any ideas?
-
@aliemrenebiler said in Add QCamera to CMakeList (ROS integrated Qt Project):
Here is what I wrote:
No, it's much easier:
change:# Find Qt5 libraries find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick Widgets )
qt5_use_modules(talker_wig Core Widgets)
into# Find Qt5 libraries find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick Widgets Multimedia )
qt5_use_modules(talker_wig Core Widgets Multimedia)
For people ending here from Google
qt5_use_modules
is obsolete, OP needs to support a very old version of CMake. The modern way is to simply usetarget_link_libraries(talker_wig Qt5::Core Qt5::Widgets Qt5::Multimedia)
-
@VRonin said in Add QCamera to CMakeList (ROS integrated Qt Project):
Multimedia
THANK YOU SO MUCH! That solved the Camera class problem.
Bu a similar error occured this time. qcamereviewfinder.h also should be added.
How can i add it?
CMakeList.txt file:
# Find Qt5 libraries find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick Widgets Multimedia ) find_package(Qt5Multimedia REQUIRED COMPONENTS QCamera QCameraViewfinder QCameraInfo QCameraImageCapture )
-
@aliemrenebiler said in Add QCamera to CMakeList (ROS integrated Qt Project):
find_package(Qt5Multimedia REQUIRED COMPONENTS
QCamera
QCameraViewfinder
QCameraInfo
QCameraImageCapture
)This is useless
Did you add
qt5_use_modules(talker_wig Core Widgets Multimedia)
andqt5_use_modules(listener_wig Core Widgets Multimedia)
-
I suspect something might be going wrong in
qt5_wrap_ui
just to confirm this is the case, could you add#include <QCameraViewFinder>
in one of your cpp files and see if that works?Do you really need to support CMake 2.8.3 or is it an example you downloaded and it's not a hard requirement?
-
@VRonin We already added QCameraViewFinder but it is
#include <QCameraViewfinder>
with "f".CMake version is not important, it was in the example.
Full code where QCameraViewfinder exist:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QCameraViewfinder> #include <QCameraInfo> #include <QtWidgets> Q_DECLARE_METATYPE(QCameraInfo) MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(this, &MainWindow::newString, this, &MainWindow::newStringSlot); connect(this, &MainWindow::newString2, this, &MainWindow::newString2Slot); //Camera devices: QActionGroup *videoDevicesGroup = new QActionGroup(this); videoDevicesGroup->setExclusive(true); const QList<QCameraInfo> availableCameras = QCameraInfo::availableCameras(); for (const QCameraInfo &cameraInfo : availableCameras) { QAction *videoDeviceAction = new QAction(cameraInfo.description(), videoDevicesGroup); videoDeviceAction->setCheckable(true); videoDeviceAction->setData(QVariant::fromValue(cameraInfo)); if (cameraInfo == QCameraInfo::defaultCamera()) videoDeviceAction->setChecked(true); } connect(videoDevicesGroup, &QActionGroup::triggered, this, &MainWindow::updateCameraDevice); setCamera(QCameraInfo::defaultCamera()); } void MainWindow::newStringSlot(QString txt) { ui->list_lines->addItem(txt); } void MainWindow::newString2Slot(QString txt2) { ui->list_lines_2->addItem(txt2); } void MainWindow::setCamera(const QCameraInfo &cameraInfo) { m_camera.reset(new QCamera(cameraInfo)); connect(m_camera.data(), QOverload<QCamera::Error>::of(&QCamera::error), this, &MainWindow::displayCameraError); m_camera->setViewfinder(ui->viewfinder); updateLockStatus(m_camera->lockStatus(), QCamera::UserRequest); connect(m_camera.data(), QOverload<QCamera::LockStatus, QCamera::LockChangeReason>::of(&QCamera::lockStatusChanged), this, &MainWindow::updateLockStatus); m_camera->start(); } void MainWindow::keyPressEvent(QKeyEvent * event) { if (event->isAutoRepeat()) return; switch (event->key()) { case Qt::Key_CameraFocus: displayViewfinder(); m_camera->searchAndLock(); event->accept(); break; default: QMainWindow::keyPressEvent(event); } } void MainWindow::keyReleaseEvent(QKeyEvent *event) { if (event->isAutoRepeat()) return; switch (event->key()) { case Qt::Key_CameraFocus: m_camera->unlock(); break; default: QMainWindow::keyReleaseEvent(event); } } void MainWindow::updateLockStatus(QCamera::LockStatus status, QCamera::LockChangeReason reason) { QColor indicationColor = Qt::black; switch (status) { case QCamera::Searching: indicationColor = Qt::yellow; break; case QCamera::Locked: indicationColor = Qt::darkGreen; break; case QCamera::Unlocked: indicationColor = reason == QCamera::LockFailed ? Qt::red : Qt::black; } } void MainWindow::startCamera() { m_camera->start(); } void MainWindow::stopCamera() { m_camera->stop(); } void MainWindow::setExposureCompensation(int index) { m_camera->exposure()->setExposureCompensation(index*0.5); } void MainWindow::displayCameraError() { QMessageBox::warning(this, tr("Camera Error"), m_camera->errorString()); } void MainWindow::updateCameraDevice(QAction *action) { setCamera(qvariant_cast<QCameraInfo>(action->data())); } void MainWindow::displayViewfinder() { ui->stackedWidget->setCurrentIndex(0); } void MainWindow::closeEvent(QCloseEvent *event) { if (m_isCapturingImage) { setEnabled(false); m_applicationExiting = true; event->ignore(); } else { event->accept(); } } MainWindow::~MainWindow() { delete ui; }
-
Edit: Oops, I see VRonin, already mentioned this above.
Make sure to also add it to a
target_link_libraries
statement in your CMakeLists.txt project file, e.g.:target_link_libraries(${PROJECT_NAME} Qt5::Multimedia)
This does end up affecting the include directories too which I hadn't expected.
-
@aliemrenebiler said in Add QCamera to CMakeList (ROS integrated Qt Project):
CMake version is not important,
Then don't use obsolete concepts:
cmake_minimum_required(VERSION 3.9) project(sabun) ## Find catkin macros and libraries find_package(catkin REQUIRED COMPONENTS roscpp std_msgs ) # Configure Catkin dependencies catkin_package( CATKIN_DEPENDS roscpp std_msgs ) ############################### # Basic, non-qt talker/listener add_executable(listener src/listener.cpp) add_executable(talker src/talker.cpp) set_target_properties(listener PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON ) set_target_properties(talker PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON ) target_include_directories(listener PRIVATE ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${catkin_INCLUDE_DIRS}) target_include_directories(talker PRIVATE ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${catkin_INCLUDE_DIRS}) target_link_libraries(listener PRIVATE ${catkin_LIBRARIES}) target_link_libraries(talker PRIVATE ${catkin_LIBRARIES}) ################################################ # Cmd line talker/listener running qt event loop # Find Qt5 libraries find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick Widgets Multimedia ) add_executable(listener_qt src/listener_qt.cpp) add_executable(talker_qt src/talker_qt.cpp) target_include_directories(listener_qt PRIVATE ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${catkin_INCLUDE_DIRS}) target_include_directories(talker_qt PRIVATE ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${catkin_INCLUDE_DIRS}) set_target_properties(listener_qt PROPERTIES AUTOMOC ON CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON ) set_target_properties(talker_qt PROPERTIES AUTOMOC ON CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON ) target_link_libraries(listener_qt PRIVATE ${catkin_LIBRARIES} Qt5::Core Qt5::Multimedia ) target_link_libraries(talker_qt PRIVATE ${catkin_LIBRARIES} Qt5::Core Qt5::Multimedia ) ###################################### # Talker and Listener using QML gui # Build executables with source, MOC result files, RCC result files add_executable(listener_qml src/listener_qml.cpp include/qml_mediator.h src/display.qrc) add_executable(talker_qml src/talker_qml.cpp include/qml_mediator.h src/display.qrc) target_include_directories(listener_qml PRIVATE ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${catkin_INCLUDE_DIRS}) target_include_directories(talker_qml PRIVATE ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${catkin_INCLUDE_DIRS}) set_target_properties(listener_qml PROPERTIES AUTOMOC ON AUTORCC ON CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON ) set_target_properties(talker_qml PROPERTIES AUTOMOC ON AUTORCC ON CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON ) target_link_libraries(listener_qml PRIVATE ${catkin_LIBRARIES} Qt5::Core Qt5::Qml Qt5::Quick Qt5::Multimedia ) target_link_libraries(talker_qml PRIVATE ${catkin_LIBRARIES} Qt5::Core Qt5::Qml Qt5::Quick Qt5::Multimedia ) ######################################## # Talker and Listener using QWidgets gui add_executable(talker_wig src/talker_wig.cpp src/mainwindow.cpp include/mainwindow.h ui/mainwindow.ui) add_executable(listener_wig src/listener_wig.cpp src/mainwindow.cpp include/mainwindow.h ui/mainwindow.ui) target_include_directories(talker_wig PRIVATE ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${catkin_INCLUDE_DIRS}) target_include_directories(listener_wig PRIVATE ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${catkin_INCLUDE_DIRS}) set_target_properties(listener_wig PROPERTIES AUTOMOC ON AUTORCC ON AUTOUIC ON CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON ) set_target_properties(talker_wig PROPERTIES AUTOMOC ON AUTORCC ON AUTOUIC ON CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON ) target_link_libraries(listener_wig PRIVATE ${catkin_LIBRARIES} Qt5::Core Qt5::Widgets Qt5::Multimedia ) target_link_libraries(talker_wig PRIVATE ${catkin_LIBRARIES} Qt5::Core Qt5::Widgets Qt5::Multimedia )
-
@VRonin sorry but that didn't work :(
Makefile:145: recipe for target 'all' failed make: *** [all] Error 2 Invoking "make -j8 -l8" failed
We just discovered that we don't need CameraViewfinder. We completely deleted it from mainwindow.cpp but still ui_mainwindow.h sees it.
In file included from /home/aliemre/QTCreator_Files/aliemre_ws/src/ROS-QML-Deneme-V5/ROS_QML_Example-master/src/mainwindow.cpp:2:0: /home/aliemre/QTCreator_Files/aliemre_ws/build/ROS-QML-Deneme-V5/ROS_QML_Example-master/ui_mainwindow.h:24:10: fatal error: qcameraviewfinder.h: No such file or directory #include "qcameraviewfinder.h"
-
@aliemrenebiler said in Add QCamera to CMakeList (ROS integrated Qt Project):
Makefile:145: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j8 -l8" failedThis is not a great error, did it spit out anything more detailed and useful?
ui_mainwindow.h sees it.
Can you post the contents of it?
-
@VRonin ui_mainwindow.h is not a library file, it is basicly "mainwindow.ui"
After I wrote that, I discovered that qcameraviewfinder.h is neccessary. I added it again and will try to implement other things on your CMakeList.txt, I will post it later.
[EDIT]
I tried to add it like this.find_package(Qt5Multimedia REQUIRED COMPONENTS qcameraviewfinder.h )
-
I will close this section and start a new one.