I want to connect a signal with non-void signature to a lambda function.
My code looks like the following
QTimeLine *a = new QTimeLine(DURATION, this);
connect(a, &QTimeLine::valueChanged, [a,this](qreal r) mutable { this->setMaximumHeight(r);});
in a way similar to the SIGNAL-SLOT approach:
connect(a, SIGNAL(valueChanged(qreal),this,SLOT(doStuff(qreal)));
My connect-to-lambda compiles, but it won't change this->height().
What did I get wrong? How should I write the lambda so that it takes the qreal from valueChanged?
I read the related documentation, but I couldn't find useful examples.
****EDIT****
In fact it works, I was getting the QTimeLine settings wrong. And yes, I don't need to capture a.
I was trying to animate a custom insertRow() method of a QTableWidget.
I also made the lambda change the height of the table row instead of the contained widget's. For reference, here's the working snippet:
QTimeLine *a = new QTimeLine(DURATION,this);
connect(a,&QTimeLine::valueChanged,[this](qreal r) mutable {
this->list->setRowHeight(0,r * ROW::HEIGHT);
});
a->start();
Thanks a lot for the quick replies anyway.
Should just work. Here is a complete SSCCE that demonstrates it working. Check what you are doing different in principles.
main.cpp
#include <QTimeLine>
#include <QObject>
#include <QDebug>
#include <QCoreApplication>
class Foo
{
void setMaximumHeight(int h) {height = h; qDebug() << "Height:" << height;}
public:
void doStuff() { QObject::connect(&timeLine, &QTimeLine::valueChanged, [this](qreal r) mutable { setMaximumHeight(r);}); timeLine.start(); }
int maximumHeight() const { return height; }
int height{0};
int DURATION{100};
QTimeLine timeLine{DURATION};
};
int main(int argc, char **argv)
{
QCoreApplication application(argc, argv);
Foo foo;
foo.doStuff();
return application.exec();
}
main.pro
TEMPLATE = app
TARGET = main
QT = core
CONFIG += c++11
SOURCES += main.cpp
Build and Run
qmake && make && ./main
Related
I need to change my text in QLabel every time when I clicked in order to my info updates.
When I change the value of A, the value of B must change, too.
I have two button that can change values in two QLabel (value of A, value of B).
main.cpp:
Counter A, B;
QObject::connect(&A, &Counter::changeValue, &B, &Counter::setValue);
QObject::connect(&A, &Counter::changeValue, &B, &Counter::Increment);
QObject::connect(&A, &Counter::changeValue, &B, &Counter::Decrement );
QObject::connect(Add, &QPushButton::clicked, &A, &Counter::clickedAdd(QLabel* obj));
QObject::connect(Sub, &QPushButton::clicked, &B, &Counter::clickedSub(QLabel* obj));
class Counter: public QObject{
private:
int count;
public slots:
int Increment () {
count++;
emit changeValue(count);
}
int Decrement () {
count--;
emit changeValue(count);
}
void clickedAdd(QLabel* obj){
int new_count = Increment();
obj_label->setText(QString::number(new_count));_
}
void clickedSub(QLabel* obj){
int new_count = Deccrement();
obj_label->setText(QString::number(new_count));_
}
void setValue(int new_count){
m_count = new_count;
emit changeValue(new_count);
}
public signals:
void changeValue(int);
How can I change my text in two QLabel's? Because in this way it stays const - 0....
When I try to connect:
QObject::connect(Add, &QPushButton::clicked, &A, &Counter::clickedAdd(QLabel* obj));
It writes an error:
Call to non-static member function without an object argument.
But I pass the function an argument - QLabel*.
Assuming OP wants an application with
two counters which can be incremented and decremented
a GUI
displaying the counter values
buttons to increment/decrement the counters interactively.
From this requirement, I would derive the structure of the program:
a class for the Counter (as already exposed by OP)
a GUI.
For the latter, I often saw classes as well in numerous sample codes but I believe: for such a minimal GUI / application, this could even all be done in main() directly.
testQCounter.cc:
#include <iostream>
#include <string>
// Qt header:
#include <QtWidgets>
// OPs Counter Class
class Counter : public QObject {
Q_OBJECT
private:
int count = 0;
public slots:
int Increment() {
count++;
emit changeValue(count);
return count;
}
int Decrement() {
count--;
emit changeValue(count);
return count;
}
int getValue() const { return count; }
void setValue(int new_count) {
count = new_count;
emit changeValue(new_count);
}
signals:
void changeValue(int);
};
#include "testQCounter.moc"
void setLabelValue(QLabel& qLbl, int value)
{
qLbl.setText(QString::number(value));
}
// main application
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup data
Counter a, b;
// setup GUI
QWidget qWinMain;
qWinMain.setWindowTitle("Counter Sample");
QGridLayout qGrid;
QLabel qLblATitle("Counter A");
qGrid.addWidget(&qLblATitle, 0, 0);
QPushButton qBtnIncA("+");
qGrid.addWidget(&qBtnIncA, 1, 0);
QLabel qLblA;
qLblA.setAlignment(Qt::AlignRight);
qLblA.setFrameStyle(QLabel::Box);
qGrid.addWidget(&qLblA, 2, 0);
QPushButton qBtnDecA("-");
qGrid.addWidget(&qBtnDecA, 3, 0);
QLabel qLblBTitle("Counter B");
qGrid.addWidget(&qLblBTitle, 0, 1);
QPushButton qBtnIncB("+");
qGrid.addWidget(&qBtnIncB, 1, 1);
QLabel qLblB("");
qLblB.setAlignment(Qt::AlignRight);
qLblB.setFrameStyle(QLabel::Box);
qGrid.addWidget(&qLblB, 2, 1);
QPushButton qBtnDecB("-");
qGrid.addWidget(&qBtnDecB, 3, 1);
qWinMain.setLayout(&qGrid);
qWinMain.show();
setLabelValue(qLblA, a.getValue());
setLabelValue(qLblB, b.getValue());
// install signal handlers
// connect clicked signal of buttons to counter a
QObject::connect(&qBtnDecA, &QPushButton::clicked, &a, &Counter::Decrement);
QObject::connect(&qBtnIncA, &QPushButton::clicked, &a, &Counter::Increment);
// connect changeValue signal of counter a to a function
QObject::connect(&a, &Counter::changeValue,
[&](int value) { setLabelValue(qLblA, value); });
// connect clicked signal of buttons to counter b
QObject::connect(&qBtnDecB, &QPushButton::clicked, &b, &Counter::Decrement);
QObject::connect(&qBtnIncB, &QPushButton::clicked, &b, &Counter::Increment);
// connect changeValue signal of counter b to b function
QObject::connect(&b, &Counter::changeValue,
[&](int value) { setLabelValue(qLblB, value); });
// runtime loop
return app.exec();
}
Output:
Qt Version: 5.15.1
Notes:
Concerning the update of the counter value label:
The signature of QLabel::setText() (the potential slot) is void QLabel::setText(const QString&).
The signature of the signal to connect is void Counter::changeValue(int).
Obviously, these signatures are not compatible.
For convenience, I introduced a function
void setLabelValue(QLabel& qLbl, int value)
{
qLbl.setText(QString::number(value));
}
but this doesn't fix the incompatibility because the function still has another parameter QLabel& which is not in the emitted signal.
This is a very usual case and the very usual solution is to bind the resp. QLabel reference to the signal.
It can be done most easily using a lambda.
When I first saw lambdas in C++, I found the syntax non-intuitive and somehow scaring.
However, after having read the doc. and tutorials I got used to it, and today, I couldn't imagine to live without.
I must admit before I learnt about lambdas I had to fiddle with bind() and hide() (in gtkmm with sigc++). This was a real nightmare…
Counter defines a signal. (I fixed the wrong syntax of OP.)
To make this linking properly, I had to add some things:
Q_OBJECT
#include "testQCounter.moc"
support for the Qt moc in my build script.
My build script is a Visual Studio project which I prepared with a CMake script.
I had to extend my CMakeLists.txt for moc (as I usually build without moc).
The CMakeLists.txt used to build the build script for testQCounter.cc:
project(QCounter)
cmake_minimum_required(VERSION 3.10.0)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories("${CMAKE_SOURCE_DIR}")
add_executable(testQCounter testQCounter.cc)
target_link_libraries(testQCounter Qt5::Widgets)
For my production, I would use separate classes for the GUI stuff as well, of course.
So, considering that the sample has two counters with nearly identical GUIs, it may make sense to introduce a counter widget – CounterEdit.
testQCounter2.cc:
#include <iostream>
#include <string>
// Qt header:
#include <QtWidgets>
// OPs Counter Class
class Counter : public QObject {
Q_OBJECT
private:
int count = 0;
public slots:
int Increment() {
count++;
emit changeValue(count);
return count;
}
int Decrement() {
count--;
emit changeValue(count);
return count;
}
int value() const { return count; }
void setValue(int new_count) {
count = new_count;
emit changeValue(new_count);
}
signals:
void changeValue(int);
};
#include "testQCounter2.moc"
class CounterEdit : public QWidget {
private:
Counter* pCounter = nullptr;
QVBoxLayout qVBox;
QLabel qLblTitle;
QPushButton qBtnInc;
QLabel qLblValue;
QPushButton qBtnDec;
QMetaObject::Connection connectionInc;
QMetaObject::Connection connectionDec;
QMetaObject::Connection connectionValue;
public:
CounterEdit(const QString& title, QWidget* pQParent = nullptr) :
QWidget(pQParent),
qLblTitle(title),
qBtnInc("+"),
qLblValue(""),
qBtnDec("-")
{
qLblTitle.setAlignment(Qt::AlignCenter);
qVBox.addWidget(&qLblTitle);
qVBox.addWidget(&qBtnInc);
qLblValue.setAlignment(Qt::AlignRight);
qLblValue.setFrameStyle(QLabel::Box);
qVBox.addWidget(&qLblValue);
qVBox.addWidget(&qBtnDec);
setLayout(&qVBox);
}
virtual ~CounterEdit()
{
QObject::disconnect(connectionInc);
QObject::disconnect(connectionDec);
QObject::disconnect(connectionValue);
}
CounterEdit(const CounterEdit&) = delete;
CounterEdit& operator=(const CounterEdit&) = delete;
Counter* counter() { return pCounter; }
const Counter* counter() const { return pCounter; }
void updateValue();
void updatevalue(int) { updateValue(); }
void setCounter(Counter* pCounter);
};
void CounterEdit::updateValue()
{
if (pCounter) {
qLblValue.setText(QString::number(pCounter->value()));
} else {
qLblValue.setText(QString());
}
}
void CounterEdit::setCounter(Counter* pCounter)
{
QObject::disconnect(connectionInc);
QObject::disconnect(connectionDec);
QObject::disconnect(connectionValue);
this->pCounter = pCounter;
if (pCounter) {
qLblValue.setText(QString::number(pCounter->value()));
connectionInc
= QObject::connect(&qBtnInc, &QPushButton::clicked, pCounter, &Counter::Increment);
connectionDec
= QObject::connect(&qBtnDec, &QPushButton::clicked, pCounter, &Counter::Decrement);
connectionValue
= QObject::connect(pCounter, &Counter::changeValue, this, &CounterEdit::updateValue);
}
}
// main application
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup data
Counter a, b;
// setup GUI
QWidget qWinMain;
qWinMain.setWindowTitle("Counter Sample");
QHBoxLayout qHBox;
CounterEdit editA("Counter A:");
qHBox.addWidget(&editA);
CounterEdit editB("Counter B:");
qHBox.addWidget(&editB);
qWinMain.setLayout(&qHBox);
qWinMain.show();
editA.setCounter(&a);
editB.setCounter(&b);
// runtime loop
return app.exec();
}
Output:
Qt Version: 5.15.1
Notes:
As data model and GUI are not anymore hardwired by design, I changed the management of the signal-slot connections a bit:
The connections are done on demand (in CounterEdit::setCounter()).
Connections are disconnected when not anymore needed.
It's not strictly necessary to store the connections like I did in the sample.
In Qt, a connection may be disconnected as well by providing signal and slot like in connect().
I don't like this for two reasons:
I'm paranoid.
This won't work for lambdas.
While the actual member function for update (CounterEdit::updateValue()) is parameter-less, I provided a second flavor (CounterEdit::updateValue(int)) which is just a wrapper.
However, this 2nd flavor has an identical signature like Counter::changeValue() and, hence, can be used as slot without an adapter.
I have two classes named IPCBase and DispatchData. Now I want to pass QDataStrean Object drom IPCBase to DispatchData. First I tried to send it directly using Connect Statement. But it is giving error like QDataStream object is not registered in QRegisterMatatype.
edit :: I have refered this link as well
When, where and why use namespace when registering custom types for Qt
So I have done something like
typedef QDataStream* myDataStrem;
Q_DECLARE_METATYPE(myDataStrem)
and then connect statement in another class(DispatchData)
connect(mpThrIPCReceiver, SIGNAL(dispatchReadData(const int&, myDataStrem)),
this, SLOT(onIPCDataReceived(const int&, myDataStrem)));
onIPCDataReceived Slot
void DispatchData::onIPCDataReceived(const int& msgType, myDataStrem dataReceived)
{
// dataReceived >> str1; Here it is giving error
// qDebug()<<"is"<<str1;
MemberFuncPointer f = mIPCCommandMapper.value(msgType);
(this->*f)(*dataReceived);
//This is function pointer which will rout it to respective function depending on the Message type.
and then it will come here
void DispatchData::onStartCountingCycle(QDataStream &dataReceived)
{
int data = 0;
dataReceived >> data; //Here it is crashing
//Giving error like
//pure virtual method called
//terminate called without an active exception
// I have debugged it and here dataReceived is becoming Readonly.
}
It seems like you're passing around a dangling pointer: the data stream seems to not exist anymore by the time the receiving thread gets to it. Even if you extended its lifetime in the source object, it's a bad idea to pass a raw pointer through signal-slot connections. If the source class might vanish while the receiver thread has a pending slot call, you'll still be using a dangling pointer at the receiver. You'd be best served by passing around a QSharedPointer or std::shared_ptr.
The following works, you can of course use any type in the shared pointer.
#include <QtCore>
#include <cstdio>
struct Class : public QObject {
Q_SIGNAL void source(QSharedPointer<QTextStream>);
Q_SLOT void destination(QSharedPointer<QTextStream> stream) {
*stream << "Hello" << endl;
}
Q_OBJECT
};
Q_DECLARE_METATYPE(QSharedPointer<QTextStream>)
int main(int argc, char ** argv) {
QCoreApplication app{argc, argv};
Class c;
c.connect(&c, &Class::source, &c, &Class::destination, Qt::QueuedConnection);
auto out = QSharedPointer<QTextStream>(new QTextStream(stdout));
emit c.source(out);
QMetaObject::invokeMethod(&app, "quit", Qt::QueuedConnection);
*out << "About to exec" << endl;
return app.exec();
}
#include "main.moc"
Output:
About to exec
Hello
On modern Qt (5.6 at least), you don't need to call qRegisterMetatype in this case.
The same using std::shared_ptr:
// https://github.com/KubaO/stackoverflown/tree/master/questions/datastream-pass-37850584
#include <QtCore>
#include <cstdio>
#include <memory>
struct Class : public QObject {
Q_SIGNAL void source(std::shared_ptr<QTextStream>);
Q_SLOT void destination(std::shared_ptr<QTextStream> stream) {
*stream << "Hello" << endl;
}
Q_OBJECT
};
Q_DECLARE_METATYPE(std::shared_ptr<QTextStream>)
int main(int argc, char ** argv) {
QCoreApplication app{argc, argv};
Class c;
c.connect(&c, &Class::source, &c, &Class::destination, Qt::QueuedConnection);
auto out = std::make_shared<QTextStream>(stdout);
emit c.source(out);
QMetaObject::invokeMethod(&app, "quit", Qt::QueuedConnection);
*out << "About to exec" << endl;
return app.exec();
}
#include "main.moc"
This is a minimal code to illustrate what I need. It doesn't work, because (as rightly the error message says when compiling) at_thread_exit is not a member of boost::thread. I know is related to the namespace this_thread, I've been going through the documentation at the boost page, but cannot follow how to use at_thread_exit. I haven't been able to find any simple example of how to use it using google.
#include <boost/thread.hpp>
#include <iostream>
class A{
public:
void callme(){
int a = 1;
}
void runThread() {
boost::thread td(&A::callme,this);
td.at_thread_exit(&A::done,this);
td.join();
}
void done() {
std::cout << "I am done!!!\n";
}
};
int main(int argc, char **argv) {
A *a = new A();
a->runThread();
delete a;
return EXIT_SUCCESS;
}
boost::thread td([this]{
callme();
done();
});
at_thread_exit only works within the same thread; it would require sychronization otherwise, and that would make every thread pay for it when only some threads use it.
I tried to connect my app to OpenViBE through VRPN server. My app works well until I try to add code to connect my app to VRPN server.
My code looks like this:
MainWindow.c code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtUiTools/QUiLoader>
#include <QFile>
#include <QMessageBox>
#include <QFileDialog>
#include <iostream>
using namespace std;
#include "vrpn_Analog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
currentImage = 0;
labelSize = ui->label_2->size();
createActions();
openFileDialog();
}
void MainWindow::checkChannels()
{
vrpn_Analog_Remote *vrpnAnalog = new vrpn_Analog_Remote("Mouse0#localhost");
vrpnAnalog->register_change_handler( 0, handle_analog );
}
void VRPN_CALLBACK MainWindow::handle_analog( void* userData, const vrpn_ANALOGCB a )
{
int nbChannels = a.num_channel;
cout << "Analog : ";
for( int i=0; i < a.num_channel; i++ )
{
cout << a.channel[i] << " ";
}
cout << endl;
}
MainWindow.h code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileInfoList>
#include "vrpn_Analog.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void resizeEvent(QResizeEvent *);
private slots:
void openFileDialog();
private:
void checkChannels();
void VRPN_CALLBACK handle_analog( void* userData, const vrpn_ANALOGCB a );
};
#endif // MAINWINDOW_H
With this code, when I try to run my app I get:
error: C3867: 'MainWindow::handle_analog': function call missing argument list; use '&MainWindow::handle_analog' to create a pointer to member
I try to edit code by error advice, but I get another error:
error: C2664: 'vrpn_Analog_Remote::register_change_handler' : cannot convert parameter 2 from 'void (__stdcall MainWindow::* )(void *,const vrpn_ANALOGCB)' to 'vrpn_ANALOGCHANGEHANDLER'
There is no context in which this conversion is possible
I search around, but I don't find any usable solution.
Methods checkChannels and handle_analog I "copy" from this code, where all works fine:
#include <QtCore/QCoreApplication>
#include <iostream>
#include "vrpn_Analog.h"
void VRPN_CALLBACK vrpn_analog_callback(void* user_data, vrpn_ANALOGCB analog)
{
for (int i = 0; i < analog.num_channel; i++)
{
if (analog.channel[i] > 0)
{
std::cout << "Analog Channel : " << i << " / Analog Value : " << analog.channel[i] << std::endl;
}
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
/* flag used to stop the program execution */
bool running = true;
/* VRPN Analog object */
vrpn_Analog_Remote* VRPNAnalog;
/* Binding of the VRPN Analog to a callback */
VRPNAnalog = new vrpn_Analog_Remote("openvibe_vrpn_analog#localhost");
VRPNAnalog->register_change_handler(NULL, vrpn_analog_callback);
/* The main loop of the program, each VRPN object must be called in order to process data */
while (running)
{
VRPNAnalog->mainloop();
}
return 0;
return a.exec();
}
Where I'm doing mistake? Thanks for all replies.
I had a similar error in Visual Studio: "function call missing argument list; use '&className::functionName' to create a pointer to member"..
I was just missing the parenthesis when calling the getter, so className.get_variable_a()
The error message tells you that the argument you provided does not match vrpn_ANALOGCHANGEHANDLER. You didn't show the definition of that. I checked online and it suggested
typedef void (*vrpn_ANALOGCHANGEHANDLER)(void *userdata, const vrpn_ANALOGCB info);
so I'm going with that.
Your code attempts to pass a pointer-to-member-function, which cannot be converted to a pointer-to-function. This is because a pointer-to-member-function can only be called on an object, so it wouldn't know what object to use.
If you look at the code you are "copying off", you will see that vrpn_analog_callback is a free function. However in your code it is a member function. You need to change your code so that the callback is a free function (or a static member function).
If your intent is that the callback should call the member function on the same MainWindow object that you are registering the handler on, then do this:
// In MainWindow's class definition, add this:
static void VRPN_CALLBACK cb_handle_analog( void* userData, const vrpn_ANALOGCB a )
{
static_cast<MainWindow *>(userData)->handle_analog(NULL, a);
}
// In checkChannels()
vrpnAnalog->register_change_handler( this, cb_handle_analog );
You cannot directly call a non-static class method using this callback. This is because the method is expecting to be called with the class this pointer.
If you don't need any data from your class, then just make the method static. If you do need data from the class, you can make a static "stub" that takes the class pointer in the userData parameter and then calls the original method. Something like:
Declaration:
static void VRPN_CALLBACK handle_analog_stub( void* userData, const vrpn_ANALOGCB a );
Definition
void VRPN_CALLBACK MainWindow::handle_analog_stub( void* userData, const vrpn_ANALOGCB a )
{
MainWindow *mainWindow = static_cast<MainWindow*>(userData);
mainWindow->handle_analog(NULL, a);
}
Then when you call the function use:
vrpnAnalog->register_change_handler( this, handle_analog_stub );
(Updated to static_cast to pointer, thanks rpavlik)
I have a Qt Unit test (sub)project, which generates me one class (with the main generated by QTEST_APPLESS_MAIN).I can start this from within Qt Creator as console app.
Q: How would I add additional classes as test cases to this particular project.
If these classes only have "test" slots (private Q_SLOTS), the methods are not called, but just the ones of the class with QTEST_APPLESS_MAIN
Since there can be only one main(..), I cannot use QTEST_APPLESS_MAIN with more than one class in the project (is that correct?)
Of course, I can manually "wire" the slots in the (additional) classes with the one class containing the main, but this is very tedious.
So what is the best way to run unit test over several classes in a unit test project?
PS:
In " Using QT Unit Tests in a project - conflicting main(...) functions " a Blog is mentioned, however, I cannot download the zip describing the solution.
As per the solution you linked to, the way to accomplish testing two (or more) classes within a single Qt unit test project is to ensure that each class to be tested has a corresponding test class, and that you've created a custom int main that executes each test class.
For example:
class TestClassA : public QObject
{
Q_OBJECT
public:
TestClassA();
...
private Q_SLOTS:
void testCase1();
...
};
class TestClassB : public QObject
{
Q_OBJECT
public:
TestClassB();
...
private Q_SLOTS:
void testCase2();
...
};
void TestClassA::testCase1()
{
// Define test here.
}
void TestClassB::testCase2()
{
// Define test here.
}
// Additional tests defined here.
// Note: This is equivalent to QTEST_APPLESS_MAIN for multiple test classes.
int main(int argc, char** argv)
{
int status = 0;
{
TestClassA tc;
status |= QTest::qExec(&tc, argc, argv);
}
{
TestClassB tc;
status |= QTest::qExec(&tc, argc, argv);
}
return status;
}
Obviously, the different test classes can be spread out over multiple translation units, then simply included in the translation unit with your int main. Don't forget to include the appropriate .moc files.
Based in the accepted answer and if you are using C++11 you could be interested in a solution using lambdas. It avoids you write the same code everytime. Although you can replace the lambda with a function, I think a lambda is cleaner.
#include <QtTest>
#include "test1.h"
#include "test2.h"
int main(int argc, char** argv)
{
int status = 0;
auto ASSERT_TEST = [&status, argc, argv](QObject* obj) {
status |= QTest::qExec(obj, argc, argv);
delete obj;
};
ASSERT_TEST(new Test1());
ASSERT_TEST(new Test2());
return status;
}
#ifndef TEST1_H
#define TEST1_H
Sample test
#include <QtTest>
class Test1 : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testCase1();
};
Searching for this same answer, I found a very good solution from http://qtcreator.blogspot.de/2009/10/running-multiple-unit-tests.html. He creates a namespace with a container that registers all the tests created (via the DECLARE_TEST macro), and then uses it to run all the tests on the list. I rewrote it to fit my code and I post my version here (My Qt Creator version: 4.1.0):
/* BASED ON
* http://qtcreator.blogspot.de/2009/10/running-multiple-unit-tests.html
*/
#ifndef TESTCOLLECTOR_H
#define TESTCOLLECTOR_H
#include <QtTest>
#include <memory>
#include <map>
#include <string>
namespace TestCollector{
typedef std::map<std::string, std::shared_ptr<QObject> > TestList;
inline TestList& GetTestList()
{
static TestList list;
return list;
}
inline int RunAllTests(int argc, char **argv) {
int result = 0;
for (const auto&i:GetTestList()) {
result += QTest::qExec(i.second.get(), argc, argv);
}
return result;
}
template <class T>
class UnitTestClass {
public:
UnitTestClass(const std::string& pTestName) {
auto& testList = TestCollector::GetTestList();
if (0==testList.count(pTestName)) {
testList.insert(std::make_pair(pTestName, std::make_shared<T>()));
}
}
};
}
#define ADD_TEST(className) static TestCollector::UnitTestClass<className> \
test(#className);
#endif // TESTCOLLECTOR_H
Then, just add the ADD_TEST(class) line in your test header like this:
#ifndef TESTRANDOMENGINES_H
#define TESTRANDOMENGINES_H
#include <QtTest>
#include "TestCollector.h"
class TestRandomEngines : public QObject
{
Q_OBJECT
private Q_SLOTS:
void test1();
};
ADD_TEST(TestRandomEngines)
#endif // TESTRANDOMENGINES_H
And and to run all the tests, just do:
#include "TestCollector.h"
#include <iostream>
int main(int argc, char *argv[]) {
auto nFailedTests = TestCollector::RunAllTests(argc, argv);
std::cout << "Total number of failed tests: "
<< nFailedTests << std::endl;
return nFailedTests;
}
I'm using the following code to collect all test results:
#include "testclassa.h"
#include "testclassb.h"
#include <QtTest>
#include <QDebug>
int main(int argc, char** argv){
int failedTests = 0;
TestClassA testClassA
TestClassB testClassB
failedTests += QTest::qExec(&testClassA, argc, argv);
failedTests += QTest::qExec(&testClassB, argc, argv);
if(failedTests > 0){
qDebug() << "total number of failed tests: " << failedTests;
}else{
qDebug() << "all tests passed :)";
}
return failedTests;
}
Build with CMake and not QMake, and add two test targets.
add_executable(firstTest tst_testfirst.cpp)
add_test(NAME firstTest COMMAND firstTest)
add_executable(secondTest tst_testsecond.cpp)
add_test(NAME secondTest COMMAND secondTest)
Both tst_testfirst.cpp and tst_testsecond.cpp have their own QTEST_MAIN lines.
Qt Creator will run both test classes. If you're running them from the command line, you run the tests with "ctest".
The way I do it:
Create a general "subdirs" project.
Put the code under test in a C++ library subproject.
Instead of using a unit test project, I use a console application subproject.
Link the library to this console application, don't forget to handle the dependencies in the .pro file at the top of the hierarchy.
In this console subproject, define as many test classes as you wish, and launch them in the main of this same project.
I basically made a slight variation of this post.