SVG icons dont show up in Qt5 - c++

I am using SVG icons in my application from the ressource file, but when I run the app the icons are just not displayed. Using jpg icons in the same way works pretty fine.

Problem
Since Qt5.1 the framework has been modularized.
Most likely you are missing the svg module. The application will still compile without complaining.
Solution
Make sure the SVG module is installed on your system and linked (with qmake (Howto), cmake (Howto) or plain make). If it was linked successfully QImageReader::supportedImageFormats() will list SVG.

If you're using cmake, you need something like this to link Svg Qt libraries.
find_package(Qt5Svg REQUIRED)
target_link_libraries( ${APP_NAME} Qt5::Svg )
A full example (sorry ManuelSchneid3r) could be the following one.
## application name
set( APP_NAME "myapp" )
## project name
project( ${APP_NAME} )
## require a minimum version of CMake
CMAKE_MINIMUM_REQUIRED ( VERSION 2.6 FATAL_ERROR )
## add definitions, compiler switches, etc.
ADD_DEFINITIONS( -Wall -O2 )
SET( CMAKE_CXX_FLAGS -g )
## include (or not) the full compiler output
SET( CMAKE_VERBOSE_MAKEFILE OFF )
# find Qt
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Svg REQUIRED)
include_directories(${Qt5Widgets_INCLUDE_DIRS})
include_directories(${Qt5Core_INCLUDE_DIRS})
include_directories(${Qt5Gui_INCLUDE_DIRS})
include_directories(${Qt5Svg_INCLUDE_DIRS})
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# The AUTOUIC target property controls whether cmake(1) inspects the
# C++ files in the target to determine if they require uic to be run,
# and to create rules to execute uic at the appropriate time.
set(CMAKE_AUTOUIC ON)
## sources
file( GLOB MAIN_SRC *.cpp )
set( SOURCES ${MAIN_SRC} )
## executable
add_executable( ${APP_NAME} ${SOURCES} )
## link
target_link_libraries( ${APP_NAME} Qt5::Widgets Qt5::Svg )

Related

Creating installer using cpack and windeployqt, To contain qt and VTK libraries

Trying to package an application with Microsoft Visual Studio 2022 and qt6 libraries I do not no were to start from. I've done a bit of research most people used qt5 and when I replace the text with qt6 it doesn't seem to work. I used dependency walker to find the necessary libraries and I seem to have achieved a way to add the Visual Studio libraries but not qt and VTK.
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
foreach(p
CMP0071 # 3.10: Let AUTOMOC and AUTOUIC process GENERATED files
)
if(POLICY ${p})
cmake_policy(SET ${p} NEW)
endif()
endforeach()
PROJECT( QtVTKExample )
#set(CMAKE_INSTALL_UCRT_LIBRARIES TRUE)
if (MSVC_VERSION EQUAL 1930 AND MSVC_TOOLSET_VERSION EQUAL 142)
cmake_host_system_information(RESULT VS_DIR QUERY VS_17_DIR)
file(GLOB MSVC_REDIST_LIBRARIES "${VS_DIR}/VC/Redist/MSVC/*/${MSVC_C_ARCHITECTURE_ID}/Microsoft.VC143.CRT/*.dll")
list(APPEND CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS "${MSVC_REDIST_LIBRARIES}")
set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_NO_WARNINGS TRUE)
endif()
include(InstallRequiredSystemLibraries)
# Add to top of file CPACK stuff
include(CTest)
if(WIN32)
set(CPACK_GENERATOR "NSIS")
else()
set(CPACK_GENERATOR "ZIP")
endif()
include(CPack)
find_package(VTK COMPONENTS
vtkCommonColor
vtkCommonCore
vtkCommonDataModel
vtkInteractionStyle
vtkRenderingContextOpenGL2
vtkRenderingCore
vtkRenderingFreeType
vtkRenderingGL2PSOpenGL2
vtkRenderingOpenGL2
QUIET
)
# The CMake build process might generate some new files in the current
# directory. This makes sure they can be found.
set( CMAKE_INCLUDE_CURRENT_DIR ON )
# This allows CMake to run one of Qt's build tools called moc
# if it is needed. moc.exe can be found in Qt's bin directory.
# We'll look at what moc does later.
set( CMAKE_AUTOMOC ON )
set( CMAKE_AUTOUIC ON )
# Find the Qt widgets package. This locates the relevant include and
# lib directories, and the necessary static libraries for linking.
find_package( Qt6Widgets )
set( UIS mainwindow.ui )
qt6_wrap_ui( UI_Srcs ${UIS} )
set( ICONS Icons/icons.qrc )
qt6_add_resources( QRC_Srcs ${ICONS} )
# Also link to VTK
find_package( VTK REQUIRED )
include( ${VTK_USE_FILE} )
# Define the executable output and its sources
add_executable( QtVTKExample MACOSX_BUNDLE
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
tabcontent.cpp
tabcontent.h
tabcontent.ui
currentstl.cpp
currentstl.h
recentstl.cpp
recentstl.h
${UI_Srcs}
${QRC_Srcs}
)
# Tell CMake that the executable depends on the Qt::Widget libraries.
target_link_libraries( QtVTKExample Qt6::Widgets )
# Tell CMake that the executable depends on the VTK libraries
target_link_libraries( QtVTKExample ${VTK_LIBRARIES} )
# /calc_cmake/CMakeLists.txt
install(TARGETS QtVTKExample
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static)

OpenCV c++: Undefined symbols for architecture arm64: [duplicate]

I consider this a fundamental step for creating projects that use OpenCV libraries so you don't need to manually include all the libraries. There is not detailed information on this topic, at least for a newbie that just wants to use OpenCV as soon as posible, so:
Which is the easiest and scalable way to create a multiplatform c++ OpenCV with Cmake?
First: create a folder Project containing two subfolders src and include, and a file called CMakeLists.txt.
Second: Put your cpp inside the src folder and your headers in the include folders.
Third: Your CMakeLists.txt should look like this:
cmake_minimum_required(VERSION 2.8)
PROJECT (name)
find_package(OpenCV REQUIRED )
set( NAME_SRC
src/main.cpp
)
set( NAME_HEADERS
include/header.h
)
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
link_directories( ${CMAKE_BINARY_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( name ${NAME_SRC} ${NAME_HEADERS} )
target_link_libraries( sample_pcTest ${OpenCV_LIBS} )
Fourth: Open CMake GUI and select the root folder as input and create a build folder for the output. Click configure, then generate, and choose the generator (VisualStudio, Eclipse, ...)
I am using opencv3.0 and cmake3.8,
config below work for me!
######## A simple cmakelists.txt file for OpenCV() #############
cmake_minimum_required(VERSION 2.8)
PROJECT(word)
FIND_PACKAGE( OpenCV REQUIRED )
INCLUDE_DIRECTORIES( ${OpenCV_INCLUDE_DIRS} )
ADD_EXECUTABLE(word main.c)
TARGET_LINK_LIBRARIES (word ${OpenCV_LIBS})
########### end ####################################

CMake Automoc Error 1 - Can't compile project

I just moved a project I built in Qt5 into my CMake project tree.
I exported the project in the CMake directory. However as I try to build the project the compiler gives me the following error:
[src/GUIconceptStudy/CMakeFiles/GUIconceptStudy_automoc] Error 1
See also the following print screen attached:
Also the CMakeLists.txt files is below:
cmake_minimum_required (VERSION 3.1)
project(GUIconceptStudy)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
find_package( OpenCV REQUIRED )
find_package( Boost COMPONENTS system thread filesystem REQUIRED)
#find_package (sqlite3)
find_package(Qt5 REQUIRED COMPONENTS Core Quick)
###
### make sure we use c++11
###
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
###
###boost include stuff (required for all libcam)
###
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
find_package(Qt5Widgets)
find_package(Qt5PrintSupport)
#find all the qt UI stuff
file(GLOB UI
"ui/*.ui"
)
#make them into headers
qt5_wrap_ui (UI_HDRS ${UI})
###
### add all your non QT sources
###
# find all non ui sources
file(GLOB SRCS
"src/*.h"
"src/*.cpp"
"src/*.hpp"
)
# find all ui related sources
file(GLOB UI_SRCS
"ui/*.h"
"ui/*.cpp"
"ui/*.hpp"
)
###
### Add executables
###
add_executable(GUIconceptStudy main/main.cpp ui/qrc/res.qrc ${SRCS} ${UI_HDRS} ${UI_SRCS})
target_link_libraries (GUIconceptStudy Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS} Qt5::PrintSupport Qt5::Core Qt5::Quick)
###
### Add Library
###
add_library(GUIconceptStudy_lib SHARED ui/qrc/res.qrc ${SRCS} ${UI_HDRS} ${UI_SRCS})
target_link_libraries (GUIconceptStudy_lib Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS} Qt5::PrintSupport Qt5::Core Qt5::Quick)
After looking at different on-line sources I could't find anything particularly useful. Anyone who can shed a little bit of light on what the problem could be?
It's a shot in a dark but this is most likely caused by the missing set(CMAKE_INCLUDE_CURRENT_DIR ON). As said in the documentation it should set because generated files are not in your source directory:
Generated moc_*.cpp and *.moc files are placed in the build directory so it is convenient to set the CMAKE_INCLUDE_CURRENT_DIR variable.
Another mistake I see people doing is mixing Qt processing pipelines. I've mentioned this already in my other answers. As it says in documentation on AUTOUIC property you shouldn't use qt5_wrap_ui function when this property is enabled.

Trying to use VTK-6.3.0 and Qt-4.8.5 with QtCreator-4.1 on Windows 10

For a project I need to use VTK and Qt (with QtCreator on C++) on Windows 10 but I have an issue.
Let me explain what I've done before going further:
I Install QtCreator-4.1 with MinGW 32bit and Qt-5.7
I download and install Qt-4.8.5 in the Qt directory (It isn't proposed in the QtCreator's MaintenanceTool)
I download VTK-6.3.0
I compile VTK with QtCreator (and a kit with Qt4) on Release with BUILD_SHARED_LIBS = True and VTK_Group_Qt = True
Then, I use an example and the CMakeLists.txt my teacher gave me, using Qt and VTK, but I have an error:
cannot find -lQVTK
In this code we use:
#include <QVTKWidget.h>
In my build directory I find : "libQVTKWidgetPlugin.dll.a" and "libQVTKWidgetPlugin.dll".
Here is the CMakeLists.txt I used (it was written for Linux user, but I'm not enough experienced to change it...):
project(foot)
cmake_minimum_required(VERSION 2.8)
# Where I built VTK
set(VTK_DIR "C:/Lib/VTK/build-VTK-6.3.0-Desktop_Qt_4_8_5_MinGW_32bit-Release")
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
set( Srcs main.cpp mainwindow.cpp )
set( Hdrs mainwindow.h )
set( MOC_Hdrs mainwindow.h )
# Use the include path and library for Qt that is used by VTK.
include_directories(
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
if(VTK_QT_VERSION VERSION_GREATER "4")
find_package(Qt5Widgets)
qt5_wrap_ui(UI_Srcs)
add_executable(qtevents
MACOSX_BUNDLE ${Srcs} ${Hdrs} ${MOC_Hdrs})
qt5_use_modules(qtevents Core Gui Widgets)
target_link_libraries(qtevents ${VTK_LIBRARIES})
else()
find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})
# Use what VTK built with
set(QT_QMAKE_EXECUTABLE ${VTK_QT_QMAKE_EXECUTABLE} CACHE FILEPATH "")
set(QT_MOC_EXECUTABLE ${VTK_QT_MOC_EXECUTABLE} CACHE FILEPATH "")
set(QT_UIC_EXECUTABLE ${VTK_QT_UIC_EXECUTABLE} CACHE FILEPATH "")
qt4_wrap_cpp(MOCSrcs ${MOC_Hdrs})
add_executable(foot ${Srcs} ${Hdrs} ${MOC_Hdrs})
target_link_libraries(foot ${VTK_LIBRARIES} QVTK)
endif()
I really don't understand what I've done wrong and my teacher can't help me...
If you have any idea...
I would be glad to give you more info if needed!

how do i create a custom (widget) plugin for qt designer with cmake ( and visual studio )

The amount of tutorials, how to create a qt designer plugin is very thin..and the ones i found always use qt creator ( like this one : http://qt-project.org/doc/qt-4.8/designer-customwidgetplugin.html ). Where i have to add some qt definitions in the .pro file
CONFIG += designer plugin
I use CMake and Visual Studio for coding, so it would be really awesome if someone could tell me how i successfully create a .dll that i can put in the plugins/designer folder to have the custom widget show up in Qt Designer
Disclaimer: I know this is an old question but Even now I didn't find complete resources on how to do it.
I can't answer you for the Visual Studio part since I build on the (windows) command line, but here is my cmake.
I assume you have already created the following files related to the plugin, i.e.:
widget.h
widget.cpp
widget.ui
widgetPlugin.h -> QDesignerCustomWidgetInterface class
widgetPlugin.cpp
And that you want to create a library with multiple plugins, i.e. created the related files:
plugins.h -> QDesignerCustomWidgetCollectionInterface class
plugins.cpp
The content of the files simply follow what's in the tutorials.
The CMakeLists.txt is:
cmake_minimum_required(VERSION 2.8)
set(PROJECT Plugins)
project(${PROJECT})
# Needed to compile against ui and moc generated files
include_directories(${CMAKE_CURRENT_BINARY_DIR})
set(SOURCES
plugins.cpp
widgetPlugin.cpp
widget.cpp
)
set(HEADERS
plugins.h
widgetPlugin.h
widget.h
)
set(FORMS
widget.ui
)
# This is experimental, it works but it may be not optimal, don't hesitate to change this
find_package(Qt4 REQUIRED QtCore QtGui QtDesigner)
if (QT4_FOUND AND QT_QTCORE_FOUND AND QT_QTGUI_FOUND AND QT_QTDESIGNER_FOUND)
set(QT_USE_QTDESIGNER TRUE)
include(${QT_USE_FILE})
else()
message(FATAL_ERROR "no qt...")
endif()
qt4_wrap_cpp(HEADERS_MOC ${HEADERS})
qt4_wrap_ui(FORMS_HEADERS ${FORMS})
qt4_add_resources(RESOURCES_RCC ${RESOURCES})
# Here too, I'm not sure every define is necessary
include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_definitions(-DQT_PLUGIN)
add_definitions(-DQT_NO_DEBUG)
add_definitions(-DQT_SHARED)
add_definitions(-DQDESIGNER_EXPORT_WIDGETS)
add_library(${PROJECT} SHARED
${SOURCES}
${HEADERS_MOC}
${FORMS_HEADERS}
${RESOURCES_RCC}
)
target_link_libraries(${PROJECT} ${QT_LIBRARIES})
# Install the library in QtDesigner plugin directory
install(TARGETS ${PROJECT}
DESTINATION ${QT_PLUGINS_DIR}/designer
)
To reload the plugins in QtDesigner, go to Help > About Plugins > Reload.
Then in the other CMakeLists.txt, I didn't want to include the library since there are also useless *Plugin files. So I included again the files I wanted :
cmake_minimum_required(VERSION 2.8)
set(PROJECT GPAUSX)
project(${PROJECT})
# Include the other CMakeLists.txt
subdirs(Plugins)
find_package(Qt4 REQUIRED)
# Files to insert
set(SOURCES
main.cpp
MainWindow.cpp
${Plugins_SOURCE_DIR}/widget.cpp
)
set(HEADERS
MainWindow.h
${Plugins_SOURCE_DIR}/widget.h
)
set(FORMS
MainWindow.ui
${Plugins_SOURCE_DIR}/widget.ui
)
qt4_wrap_cpp(HEADERS_MOC ${HEADERS})
qt4_wrap_ui(FORMS_HEADERS ${FORMS})
qt4_add_resources(RESOURCES_RCC ${RESOURCES})
include(${QT_USE_FILE})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_definitions(${QT_DEFINITIONS})
# I'm no expert in libraries so, intuitively I'd say this is useless but it won't compile if I don't define it.
# This clearly needs to get fixed.
add_definitions(-DQDESIGNER_EXPORT_WIDGETS)
# Possible variants making it compile :
# 1/ either include Plugins_BINARY_DIR or include .uis
# including the binary dir makes use of the already generated .uis
# 2/ either target Plugins or add Plugins .uis, .hs and .cpps with -DQDESIGNER_EXPORT_WIDGETS
# if you target plugins, make sure you compile with the same flags
add_executable(${PROJECT}
${SOURCES}
${HEADERS_MOC}
${FORMS_HEADERS}
${RESOURCES_RCC}
)
target_link_libraries(${PROJECT}
# Uncomment the following if you want to target Plugins
#Plugins
${QT_LIBRARIES}
)
Hope you'll find it useful !
Just use the cmake's qt auto tools and add all sources(.cpp .ui .qrc etc) to the target as follow:
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt4 REQUIRED QtCore QtGui QtDesigner #...
)
include(${QT_USE_FILE})
add_definitions(${QT_DEFINITIONS})
add_library(someplugin SHARED
path/to/plugin/someplugin.cpp
path/to/plugin/someplugin.qrc
path/to/plugin/someplugin.ui
path/to/plugin/s/other/src.cpp
#...
)
target_link_libraries(someplugin ${QT_LIBRARIES})
install(TARGETS someplugin
DESTINATION ${QT_PLUGINS_DIR}/designer
)