简体   繁体   English

使用QQuickView rootContext设置可以从QML调用的线程

[英]using QQuickView rootContext to setup a thread that can be invoked from QML

I am a new guy to Qt although I have been writing in C for many years only about a year or so using c++. 我是Qt的新手,尽管我使用C ++从事C编写已经很多年了。 We are writing a camera app which has some C++ code for accessing frame buffers, video, etc. and a GUI that is written in QML. 我们正在编写一个相机应用程序,其中包含一些用于访问帧缓冲区,视频等的C ++代码以及一个用QML编写的GUI。 It is necessary to invoke gstreamer in a c++ class which needs to run in a separate thread and needs to be invoked from the QML code (because the QML code hangs waiting for threads to finish if it is invoked before starting the QML). 必须在c ++类中调用gstreamer,该c ++类需要在单独的线程中运行并且需要从QML代码中调用(因为如果在启动QML之前调用它,则QML代码会挂起,等待线程完成)。

I found what looked like a great way to do it in the answer to someone else's question: How Start a Qthread from qml? 我在回答其他人的问题: 如何从qml启动Qthread时发现了一种看起来不错的方法 . Unfortunately when I attempted to modify my code to run as shown in one of the answers I am getting a SIGABT signal. 不幸的是,当我试图修改我的代码以如答案之一所示运行时,我收到了SIGABT信号。 This happens when the code executes the following line: 当代码执行以下行时,会发生这种情况:

view.engine()->rootContext()->setContextProperty("thread", &gstworker);

The main.cpp function looks like this: main.cpp函数如下所示:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QQmlContext>

#include <gst/gst.h>
#include "common.h"
#include "fbctl.h"
#include "gstcameraif.h"
#include "gstworker.h"

int main(int argc, char *argv[])
{
    DBG_PRINT("main.cpp: register FbCtl type\n");
    qmlRegisterType<FbCtl>("test.fbctl.qt", 1, 0, "FbCtl");

    DBG_PRINT("main.cpp: register GstCameraIf type\n");
    qmlRegisterType<GstCameraIf>("test.gstcameraif.qt", 1, 0, "GstCameraIf");

    DBG_PRINT("main.cpp: register GstWorker type");
    qmlRegisterType<GstWorker>("test.gstworker.qt", 1, 0, "GstWorker");


    gst_init(&argc, &argv);

    GstCameraIf gStreamer;

    FbCtl   fbSetup;            // get a local instance of the frame buffer control object
    fbSetup.setupOverlay();     // initialize the overlay setup
    fbSetup.stopProcess();      // close opened descriptors, unmap frame buffers

    qputenv("QT_QPA_EGLFS_FB", "dev/fb1");              // redirects QT to use fb1 (overlay) and gstreamer will go to fb0

    GstWorker gstworker;
    QQuickView view;

    view.engine()->rootContext()->setContextProperty("thread", &gstworker);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    gst_deinit();

    return app.exec();
}

The function call list (stack) shows the following trace: 函数调用列表(堆栈)显示以下跟踪:

1  __GI_raise                               raise.c          54   0xb5ae1910 
2  __GI_abort                               abort.c          89   0xb5ae2ca0 
3  qt_message_fatal                         qlogging.cpp     1687 0xb5e0fff8 
4  QMessageLogger::fatal                    qlogging.cpp     795  0xb5e0fff8 
5  qt_pixmap_thread_test                    qpixmap.cpp      74   0xb670ce98 
6  QPixmap::QPixmap                         qpixmap.cpp      109  0xb670ce98 
7  QCursorData::QCursorData                 qcursor.cpp      624  0xb66a3360 
8  QCursorData::initialize                  qcursor.cpp      655  0xb66a3360 
9  QCursor::QCursor                         qcursor.cpp      470  0xb66a3360 
10 QWindowPrivate::QWindowPrivate           qwindow_p.h      107  0xb6bb3a78 
11 QQuickWindowPrivate::QQuickWindowPrivate qquickwindow.cpp 501  0xb6bb3a78 
12 QQuickViewPrivate::QQuickViewPrivate     qquickview.cpp   77   0xb6c306e0 
13 QQuickView::QQuickView                   qquickview.cpp   166  0xb6c30778 
14 main                                     main.cpp         38   0x131c8    

My question is what do I need to fix to get rid of the sigabt signal. 我的问题是我需要解决些什么才能摆脱信号的信号。
thanks for any help you can give. 谢谢你提供的所有帮助。

When I got the error again I noticed that it was actually happening when I declared the QQuickView object. 当我再次收到该错误时,我注意到在声明QQuickView对象时实际上正在发生该QQuickView I changed the main.cpp to use the QQmlApplicationEngine (instead of creating a quickview engine) to set the context and the error no longer occurs. 我将main.cpp更改为使用QQmlApplicationEngine (而不是创建QQmlApplicationEngine引擎)来设置上下文,并且错误不再发生。

FYI: The SIGABT error was given in a message box and that is all it said. 仅供参考:在消息框中给出了SIGABT错误,仅此而已。

I would like to give credit to @m7913d for the help in solving this issue. 我想感谢@ m7913d为解决此问题提供的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM