Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Show Camera inside of a Widget-App



  • I have a QMainWindow Class where a function is called:

    void MyTest::OnOpenCamera()
    {
        this->setVisible(false);
        auto cameraApp = new CameraApp;
        cameraApp->Show(this);
    }
    

    In class CameraApp the following code is implemented:

    CameraApp::CameraApp()
    {
        QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
        QZXing::registerQMLTypes();
        NativeHelpers::registerApplicationInstance(this);
    
        this->LoadQml();
    }
    
    CameraApp::~CameraApp()
    {
    }
    
    void CameraApp::LoadQml()
    {
        this->engine.load(QUrl(QStringLiteral("qrc:/qml/camera.qml")));
    }
    
    void CameraApp::Show(QWidget* parent)
    {
        QObject* topLevel = this->engine.rootObjects().value(0);
        QQuickWindow* window = qobject_cast<QQuickWindow*>(topLevel);
        window->show();
    }
    

    Now the camera window is showing indeed, but it's not transparent.

    What can I do, to see a normally camera screen inside of my app?
    Thank you!


  • Lifetime Qt Champion

    Hi,

    Might be a silly question but why not use QCameraViewfinder ?



  • The example is an QZXingLive-example. There is no QCameraViewfinder in use.

    Just in main.cpp

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
        QGuiApplication app(argc, argv);
    
        QZXing::registerQMLTypes();
    
        Application customApp;
        customApp.checkPermissions();
    
        return app.exec();
    }
    

    and then in application.cpp

    Application::Application()
    {
        //both signals will be connected to the same function for
        //simplicity
    
        connect(this, &Application::onPermissionsGranted,
                this, &Application::initializeQML);
    
        connect(this, &Application::onPermissionsDenied,
                this, &Application::initializeQML);
    
        NativeHelpers::registerApplicationInstance(this);
    }
    
    void Application::initializeQML()
    {
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    }
    
    void Application::checkPermissions()
    {
    #if defined(Q_OS_ANDROID)
        //intentionally called in the C++ thread since it is blocking and will continue after the check
        qDebug() << "About to request permissions";
    
            QAndroidJniObject::callStaticMethod<void>("org/ftylitak/qzxing/Utilities",
                                            "requestQZXingPermissions",
                                            "(Landroid/app/Activity;)V",
                                            QtAndroid::androidActivity().object());
        qDebug() << "Permissions granted";
    #else
        emit onPermissionsGranted();
    #endif //Q_OS_ANDROID
    }
    

    Now I wonder, how can I implement this as a library implementation for my app.


  • Lifetime Qt Champion

    @peter-70 said in Show Camera inside of a Widget-App:

    Now the camera window is showing indeed, but it's not transparent.

    What is not transparent?
    It looks like you want to use a barcode scanner, right?



  • Yes, exactly!

    When I try to run the code in my app, the qml content is visible, but it has a white background. So I can't see the camera input signal/image


  • Lifetime Qt Champion

    @peter-70 Does checkPermissions() succeed?



  • Yes! The engine.load is called. The qml file can be found. The content of the qml file is drawn, but the background of it, is white and not transparent.

    But this is only when I try to run the app, as a lib, from my widget app. If the scanner app runs as standalone app, everything is ok.


Log in to reply