Build Qt Tests with CMake - qt

Can anyone give me an example of some QT test code and a CMakeLists.txt that build with Cmake and ran with CTest. I can't seem to find any!
-Kurtis

Here is an example of using cmake 2.8.11 and Qt5.2. Note that cmake now supports testfiles with a .moc-include at the bottom.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.11)
project(foo)
enable_testing()
# Tell CMake to run moc when necessary:
set(CMAKE_AUTOMOC ON)
# As moc files are generated in the binary dir, tell CMake
# to always look for includes there:
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Test REQUIRED)
add_executable(foo foo.cpp)
add_test(foo foo)
target_link_libraries(foo Qt5::Test)
foo.cpp:
#include <QTest>
class Foo : public QObject {
Q_OBJECT
private slots:
void t1() { QVERIFY(true); }
};
QTEST_MAIN(Foo)
#include "foo.moc"

An example taken from Charm (Tests/CMakeLists.txt):
SET( TestApplication_SRCS TestApplication.cpp )
SET( TEST_LIBRARIES CharmCore ${QT_QTTEST_LIBRARY} ${QT_LIBRARIES} )
SET( SqLiteStorageTests_SRCS SqLiteStorageTests.cpp )
QT4_AUTOMOC( ${SqLiteStorageTests_SRCS} )
ADD_EXECUTABLE( SqLiteStorageTests ${SqLiteStorageTests_SRCS} )
TARGET_LINK_LIBRARIES( SqLiteStorageTests ${TEST_LIBRARIES} )
ADD_TEST( NAME SqLiteStorageTests COMMAND SqLiteStorageTests )
The only difference to a normal executable is that you call ADD_TEST macro.
Have a look at e.g. Charm to see it in action.

Related

CMake shows console

I made an app and when I run in QtCreator everything goes well, my question is when I generate the release, a consola is shown along with my app, why does this happen, and how can I avoid it.
Here my code.
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlTableModel>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase db=QSqlDatabase::addDatabase("QPSQL");
if(!db.isDriverAvailable("QPSQL")){
QMessageBox::critical(this,qApp->applicationName(),db.lastError().text());
return;
}
db.setDatabaseName("covid");
db.setHostName("localhost");
db.setPassword("2311046");
db.setPort(5432);
db.setUserName("postgres");
if(!db.open()){
QMessageBox::critical(this,qApp->applicationName(),db.lastError().text());
return;
}
QMessageBox::information(this,qApp->applicationName(),"Todo bien");
}
MainWindow::~MainWindow()
{
delete ui;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(untitled1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.
#if(ANDROID)
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
# if (ANDROID_ABI STREQUAL "armeabi-v7a")
# set(ANDROID_EXTRA_LIBS
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
# endif()
#endif()
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
find_package(Qt5 COMPONENTS Sql REQUIRED)
if(ANDROID)
add_library(untitled1 SHARED
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
else()
add_executable(untitled1
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
endif()
target_link_libraries(untitled1 PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(untitled1 PUBLIC Qt${QT_VERSION_MAJOR}::Sql)
Here I show the Windows that are shown every time I run the .exe:
This should be quite simple. Just add:
# ...
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Make sure we build executables as GUI apps, rather than console apps, on Windows
set(CMAKE_WIN32_EXECUTABLE TRUE)
# ...
As the documentation explains:
When [WIN32_EXECUTABLE] is set to true the executable when linked on Windows will be created with a WinMain() entry point instead of just main(). This makes it a GUI executable instead of a console application. [...] This property is initialized by the value of the CMAKE_WIN32_EXECUTABLE variable if it is set when a target is created.

CMake cannot find QWidgets

I have a CMakeLists.txt file which looks like this:
# CMakeLists.txt
project(myApp)
# Set minimum version of CMake
cmake_minimum_required(VERSION 3.5)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Find Qt5Core and Qt5Widgets
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
# For finding Qt includes
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${Qt5Widgets_INCLUDE_DIRS})
include_directories(${Qt5Core_INCLUDE_DIRS})
# Collect source files
set(SOURCE
main.cpp
mainwindow.cpp
)
# Collect header files
set(HEADERS
mainwindow.h
)
# Create executable
add_executable(prog ${SOURCE})
# Linking Editor with Qt libraries
target_link_libraries(prog Qt5::Core Qt5::Widgets)
When I run cmake and then make the following error message is presented;
Scanning dependencies of target Prog
[ 40%] Building CXX object CMakeFiles/Prog.dir/main.cpp.o
In file included from /path/main.cpp:7:0:
/path/mainwindow.h:7:20: fatal error: QWidgets: No such file or directory
compilation terminated.
Where am I doing wrong with the CMake file?
mainwindow.h looks like this:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
/*
* mainwindow.h
*/
#include <QMainWindow>
#include <QWidgets>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidgets *parent = 0);
~MainWindow();
};
#endif
It should be
#include <QWidget>
class MainWindow : public QMainWindow
{
...

"undefined reference to `boost::system::generic_category()" qtcreator with cmake

I try to use boost in qtcreator(windows) with Cmake, Cmake can find boost "system" and "thread" sucessfully, but there is there is a problem "undefined reference to `boost::system::generic_category()" while compiling. I searched problem like this, adding "-lboost_system" maybe a solution,but there is no ".pro" file in my project,so how to add it in CMakeLists.
Below is part of my CmakeLists:
project(demo)
cmake_minimum_required(VERSION 2.8.9)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(DEBUG_FLAG _d)
set(CMAKE_AUTOMOC ON)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Xml REQUIRED)
find_package(Qt5Quick REQUIRED)
find_package(Qt5Qml REQUIRED)
find_package(Qt5Declarative)
find_package(OpenGL REQUIRED)
find_package(Boost 1.58 REQUIRED COMPONENTS system thread)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIRS})
set(OGRE_RESOURCE_FOLDER ${PROJECT_SOURCE_DIR}/resources)
set(OGRE_HOME "D:/3rd/OGRE")
set(OGRE_PLUGIN_FOLDER "${OGRE_HOME}\\bin")
include_directories(
"${OGRE_HOME}\\include"
"${OGRE_HOME}\\include\\OGRE"
"${OGRE_HOME}\\include\\OGRE\\RenderSystems\\GL"
"${OGRE_HOME}\\boost")
link_directories(${LINK_DIRECORIES} "${OGRE_HOME}\\lib" "${OGRE_HOME}\\lib\\opt")
include_directories("C:/3rd/Eigen" "C:/3rd/robotics/include" "D:/3rd/Eigen" "D:/3rd/robotics/include")
link_directories("C:/3rd/robotics/lib" "D:/3rd/robotics/lib")
add_library(robot src/robot.cpp)
add_library(QOgre src/QOgreWidget.cpp)
qt5_use_modules(QOgre OpenGL Widgets Xml)
add_executable(OGREDemo src/QOgreWidgetDemo.cpp src/ogredemo.cpp )
qt5_use_modules(OGREDemo OpenGL Widgets Xml)
target_link_libraries(OGREDemo ${OPENGL_LIBRARIES} ${BOOST_LINK_LIBS_DEBUG}
OgreMain${DEBUG_FLAG} OIS${DEBUG_FLAG}
orocos-kdl${DEBUG_FLAG} robot QOgre )
file(GLOB_RECURSE QMLS "." "*.qml")
qt5_add_resources(RESOURCES_ADDED resources/qml.qrc)
set_source_files_properties(${RESOURCE_ADDED} PROPERTIES GENERATED ON)
add_executable(recat src/main.cpp src/QmlOgre.cpp ${RESOURCES_ADDED} ${QMLS})
qt5_use_modules(recat Widgets Xml Quick Qml Core Gui Declarative)
target_link_libraries(recat ${OPENGL_LIBRARIES} ${BOOST_LINK_LIBS_DEBUG}
OgreMain${DEBUG_FLAG} OIS${DEBUG_FLAG} RenderSystem_GL${DEBUG_FLAG}
orocos-kdl${DEBUG_FLAG} robot)
configure_file (
"${PROJECT_SOURCE_DIR}/plugins.cfg.in"
"${PROJECT_BINARY_DIR}/plugins.cfg"
)
configure_file (
"${PROJECT_SOURCE_DIR}/resources.cfg.in"
"${PROJECT_BINARY_DIR}/resources.cfg"
)
And detailed problem description:
D:\3rd\robotics_android\src\QOgreWidgetDemo.cpp:-1: error: undefined reference to `boost::system::generic_category()'
error: CMakeFiles\OGREDemo.dir/objects.a(QOgreWidgetDemo.cpp.obj): bad reloc address 0x1b in section `.text$_ZNK5boost6system10error_code7messageEv[__ZNK5boost6system10error_code7messageEv]'
The right variable to link against is Boost_SYSTEM_LIBRARY_DEBUG. As #usr1234567 stated it is not necessary to use link_directories.
For reference see: https://cmake.org/cmake/help/v3.0/module/FindBoost.html

CMake tests fails but if just run testing executable so everything is ok

I'm using mingw (4.9.1) that was stored with last Qt official build. CMake version is 3.1.2. The situation is when i called make test so i got failed tests with exceptions, but, if i just run this test *.exe by manually — everything is ok, tests running!
What can be wrong?
And yes, I checked the libs its liked with...
There are these files...
CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
CMAKE_POLICY(SET CMP0020 NEW)
#ENABLE_TESTING( true )
SET( CMAKE_AUTOMOC ON )
SET( CMAKE_INCLUDE_CURRENT_DIR ON )
SET(CMAKE_PREFIX_PATH $ENV{QTDIR})
FIND_PACKAGE( Qt5Core REQUIRED )
FIND_PACKAGE( Qt5Test REQUIRED )
ADD_EXECUTABLE( coretest coretest.cpp )
ADD_TEST( coretest coretest )
QT5_USE_MODULES( coretest Core Test )
TARGET_LINK_LIBRARIES( coretest ${QT_LIBRARIES} )
And single test code file (coretest.cpp):
#include <QTest>
class Coretest : public QObject {
Q_OBJECT
private slots:
void t1();
};
void Coretest::t1()
{
QVERIFY(true);
}
QTEST_MAIN(Coretest)
#include "coretest.moc"
Well, I've got the solution. Just forget to check Path var...

QT Plugin with CMake

Greetings all,
I am trying to implement a QT Plugin with CMake. But this "Q_EXPORT_PLUGIN2" directive stops my class from compiling. I can compile the plugin if I commented this out,but it won't work as a plugin if I do so.
QT doc says:
Q_EXPORT_PLUGIN2 ( PluginName, ClassName )
The value of PluginName should
correspond to the TARGET specified in
the plugin's project file
What about in CMake case? What should be the value for 'PluginName'?
Here is my Plugin Interface :
#ifndef RZPLUGIN3DVIEWERFACTORY_H_
#define RZPLUGIN3DVIEWERFACTORY_H_
#include <QObject>
#include "plugin/IRzPluginFactory.h"
class RzPlugin3DViewerFactory :public QObject,public IRzPluginFactory{
Q_OBJECT
Q_INTERFACES(IRzPluginFactory)
private:
QString uid;
public:
RzPlugin3DViewerFactory();
virtual ~RzPlugin3DViewerFactory();
IRzPlugin* createPluginInstance();
IRzPluginContext* createPluginContextInstance();
QString & getPluginUID();
};
#endif /* RZPLUGIN3DVIEWERFACTORY_H_ */
And implementation
#include "RzPlugin3DViewerFactory.h"
#include "RzPlugin3DViewer.h"
RzPlugin3DViewerFactory::RzPlugin3DViewerFactory() {
uid.append("RzPlugin3DView");
}
RzPlugin3DViewerFactory::~RzPlugin3DViewerFactory() {
// TODO Auto-generated destructor stub
}
IRzPlugin* RzPlugin3DViewerFactory::createPluginInstance(){
RzPlugin3DViewer *p=new RzPlugin3DViewer;
return p;
}
IRzPluginContext* RzPlugin3DViewerFactory::createPluginContextInstance()
{
return NULL;
}
QString & RzPlugin3DViewerFactory::getPluginUID()
{
return uid;
}
Q_EXPORT_PLUGIN2(pnp_extrafilters, RzPlugin3DViewerFactory)
Error Message is :
[ 12%] Building CXX object
CMakeFiles/RzDL3DView.dir/RzPlugin3DViewerFactory.cpp
.obj
C:\svn\osaka3d\trunk\osaka3d\rinzo-platform\src\dlplugins\threedviewer\RzPlugin3
DViewerFactory.cpp:36: error: expected
constructor, destructor, or type
conversi on before '(' token make[2]:
*** [CMakeFiles/RzDL3DView.dir/RzPlugin3DViewerFactory.cpp.obj]
Error 1
make[1]: *
[CMakeFiles/RzDL3DView.dir/all] Error
2 make: * [all] Error 2
Ok , I fixed the problem by giving the project name specified in Cmake file.
PROJECT (RinzoDLPlugin3DViewer CXX C)
So,now in CPP file its
Q_EXPORT_PLUGIN2(RinzoDLPlugin3DViewer , RzPlugin3DViewerFactory)
and included qpluginh.h
#include <qplugin.h>
I think the macro should be Q_EXPORT_PLUGIN2(pnp_rzplugin3dviewerfactory, RzPlugin3DViewerFactory) or whatever you have listed as the target name in the .pro file. In fact, the "pnp" part stands for "Plug & Paint" which is the Qt demo program for writing plugins :)
Edit:
Since I misunderstood how CMake works, this information isn't really relevant to the OP. I did do a quick search however and turned up this discussion of Qt, plugins and CMake. I hope there is some useful info there.
http://lists.trolltech.com/qt-interest/2007-05/msg00506.html

Resources