How to use QtTest with qbs - qt

I can't find clear example of building tests with qbs.
I tried like this
import qbs
CppApplication {
consoleApplication: true
files: [ "TestTask.h", "TestTask.cpp" ]
Depends { name: "Qt"; submodules: [ "core", "testlib" ] }
}
TestTask is a QObject subclass.
But compiler says that I missed main() function.

For compile test your need main.cpp. For example:
#include <QCoreApplication>
#include <QTest>
#include "TestTask.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTest::qExec(new TestTask, argc, argv);
return 0;
}
Your must also add main.cpp in files (qbs file).

Related

FFmpeg on Qt Creator

I have got FFmpeg compiled using MSYS2. I have a strange issue that appeared only recently.the app crashes.
This is my main file:
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
extern "C" {
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavcodec/avcodec.h>
}
int main(int argc, char *argv[])
{
//qDebug() << av_version_info();
// avdevice_register_all();
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
The program runs but crashes if I call 'avdevice_register_all()' and others FFmpeg functions,but only call 'av_version_info()' is runs, not crashes.
I don't really know how to solve this?
I have already tried to use google solving the problem,but no result.

Doctest and qapplication

I must integrate doctest lib to a QT core project (Qt5 exactly).
At a point, I need a qcoreapplication to run event loop.
But when I try implement my doctest main with a qcoreapplication it looks doctest context and qcoreapplication parasite themself.
Do there are some workarounds? like qeventloop without qcoreapplication? other ways?
Did someone have already encountered this kind of case with other test libs?
Thanks for answers ;)
Edit 1:
For confidentiality reason i can't share actual base code. But i have same issue with basic code.
At my actual point i fall in an infinite loop at the end of execution.
I may just miss something on QCoreApplication execution end.
#define DOCTEST_CONFIG_IMPLEMENT
#include <QDebug>
#include <QObject>
#include <QSignalSpy>
#include <QApplication>
#include <QEventLoop>
#include "../../Core/external/doctest/doctest.h"
TEST_CASE("Basic class") {
SUBCASE("2*2") {
qDebug()<<"2*2";
CHECK_EQ(2*2, 4);
}
SUBCASE("1+1") {
qDebug()<<"1+1";
CHECK_EQ(1+1, 2);
}
SUBCASE("Termination") {
qDebug()<<"End!!";
QCoreApplication::instance()->quit();
}
}
int main(int argc, char** argv) {
doctest::Context context;
// defaults
context.setOption("abort-after", 5);
context.setOption("order-by", "name");
QCoreApplication app (argc, argv);
context.applyCommandLine(argc, argv);
context.setOption("no-breaks", true);
int res = context.run();
if(context.shouldExit())
return res;
int client_stuff_return_code = 0;
return res + client_stuff_return_code + app.exec(); // the result from doctest is propagated here as well
}

how to import a QML Component resource in a QML file

I have the following directory structure:
ui/
|- resources.qrc
|- qml/
|- main_window_presenter.qml
|- MyPresenter.qml
resources.qrc contents:
<RCC>
<qresource prefix="/">
<file>qml/MyPresenter.qml</file>
<file>qml/main_window_presenter.qml</file>
</qresource>
</RCC>
MyPresenter.qml contents:
import QtQuick 2.11
FocusScope {
id: root
property Item view
property QtObject model
Component.onCompleted: {
root.view.anchors.fill = root
root.view.focus = true
}
}
main_window_presenter.qml contents:
import "."
MyPresenter {
id: root
}
main.cpp contents:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(":/qml/main_window_presenter.qml");
return app.exec();
}
When I run the application I get
QQmlApplicationEngine failed to load component
file::/qml/main_window_presenter.qml:1 import "." has no qmldir and no namespace
If I delete import "." at main_window_presenter.qml I get
QQmlApplicationEngine failed to load component
file::/qml/main_window_presenter.qml:3 MyPresenter is not a type
I think I shouldn't need an import statement because they are in the same directory. I am using meson build system with this relevant part in meson.build(exe_moc_headers are defined before):
qt5_module = import('qt5')
exe_processed = qt5_module.preprocess(moc_headers : exe_moc_headers, qresources : 'ui/resources.qrc')
As #eyllanesc suggested QQuickView works instead of QQmlApplicationEngine:
#include <QGuiApplication>
#include <QQuickView>
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
QQuickView* view{new QQuickView};
view->setSource(QUrl("qrc:///qml/main_window_presenter.qml"));
view->show();
return app.exec();
}
I might have figured it myself if the error message wasn't indicating that type is not found by saying "MyPresenter is not a type". This led me to believe that its a referencing issue.

Setting LocalStorage Location via setOfflineStoragePath

I'm trying to set the LocalStorage location (sqlite db) for my QML application but once i rebuild and run the application i still can't see the subfolder, INI file and the sqlite DB created on the desired location (in a subfolder within the resources folder). Here is what's in my main file.
Appreciate any one could pint what's I'm doing wrong here?
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QString>
#include <QDebug>
#include <QDir>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
//engine.setOfflineStoragePath("qrc:/");
auto offlineStoragePath = QUrl::fromLocalFile(engine.offlineStoragePath());
engine.rootContext()->setContextProperty("offlineStoragePath", offlineStoragePath);
QString customPath = "qrc:/OffLineStorage";
QDir dir;
if(dir.mkpath(QString(customPath))){
qDebug() << "Default path >> "+engine.offlineStoragePath();
engine.setOfflineStoragePath(QString(customPath));
qDebug() << "New path >> "+engine.offlineStoragePath();
}
return app.exec();
}
By looking at the code snippet in your question, everything looks fine.
Anyway, you should verify if the following line actually returns true as you expect:
dir.mkpath(QString(customPath)
If no, the body of the if statement isn't executed in any case, thus setOfflineStoragePath is never invoked.
As a hint, using qrc:/OffLineStorage as a path for your storage doesn't seem to be a good idea. I'm not sure it will work once in the production environment (to be checked, it sounds strange, but it could work).
Try use engine.setOfflineStoragePath before engine.load.
Using qrc:/OffLineStorage as a path for your storage doesn't seem to
be a good idea. I'm not sure it will work once in the production
environment
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QString>
#include <QDebug>
#include <QDir>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QString customPath = "qrc:/OffLineStorage";
QDir dir;
if(dir.mkpath(QString(customPath))){
qDebug() << "Default path >> "+engine.offlineStoragePath();
engine.setOfflineStoragePath(QString(customPath));
qDebug() << "New path >> "+engine.offlineStoragePath();
}
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.clearComponentCache();
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}

Creating an object of QDeclarativeView results in segmentation fault

.h
#include <QObject>
#include <QDebug>
class MyClass : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE void cppMethod (const QString &msg)
{
qDebug() << "Called the C++ method with" << msg;
}
public slots:
void cppSlot (int number)
{
qDebug() << "Called the C++ slot with" << number;
}
};
.cpp
#include <QtCore/QCoreApplication>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDeclarativeContext>
#include <QDeclarativeView>
#include <QVariant>
#include <QMetaObject>
#include "cppFromQml.h"
int main (int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDeclarativeView view;
return a.exec();
}
This results in segmentation fault. What's the way out?
Qt: 4.8.1
note that you're not using MyClass, and - just my guess - a declarative view will need a QApplication to properly run.
To better understand, I created a project, dumped almost all away (just kept the .pro, where I added qt += declarative), and changed a bit your code as follow:
#include <QApplication>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDeclarativeContext>
#include <QDeclarativeView>
#include <QVariant>
#include <QMetaObject>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDeclarativeView view;
view.show();
return a.exec();
}
now it runs and display an empty view, as expected...

Resources