Qt: throwing an exception from C ++ to the QML engine

When calling the Q_INVOKABLE method written in C++ from a JavaScript function in QML , how did you choose the exception? This method belongs to an object of type < MyApp > registered with the qmlRegisterType() call qmlRegisterType()

For example, I have the following QML code:

 TextField { id: fld_recipient onEditingFinished: { try { var identity=myapp.identities.current_Identity; var company=identity.companies.current_Company; myapp.lookup_email(identity.identity_id,company.company_id,fld_recipient.text) } catch(e) { console.log(e); } } } 

Here, the MyApp :: lookup_email () method is sent to the server and looks for the corresponding email addresses. This process can be stopped by a lot of errors, and I want the catch () operator to display this error.

How is this done on the C ++ side? A view like this:

 void MyApp::lookup_email(int identity_id,int company_id,QString email) { .... error_code=server->lookup_email(identity_id,company_id,email); if (error_code) { /// throw an exception to QML engine, ????? <= what goes here ? } } 
+5
source share
1 answer

Qml should not include any business logic, but simply show any results. In case your application starts in an exception state, catch the exception in the business logic and present the result / new to the user.

If you assume that your (UI) code is being called with incomplete data, you should simply ignore it. Or use debug level statements.

+2
source

Source: https://habr.com/ru/post/1262092/


All Articles