简体   繁体   中英

Q_INVOKABLE variadic function

In extension to this question: Pass variadic parameters further

I am trying to make the wrapper method Q_INVOKABLE static :

template <typename... Args>
Q_INVOKABLE static void wrapper(const LogLevel ll, QString&& msg, Args&&... args)
{
    CLogger::instance().trace(ll, std::move(msg), std::forward<Args>(args)...);
}

But MOC will not compile this code: error: C2061: syntax error: identifier 'Args' Is there any way for accomplishing variadic functions being esported to the QML part?

Basing on @RossRogers comment, I created the following method:

header:

Q_INVOKABLE static void log(LogLevel l,
                            QString m,
                            QVariant v1 = QVariant(),
                            QVariant v2 = QVariant(),
                            QVariant v3 = QVariant());

source:

 * @brief   A QML enabled wrapper function for the logger trace method. It
 *          allows to use the trace (printf style) in QML with up to 3
 *          parsable arguments.
 * @param   l: trace log level.
 * @param   m: message to be printed.
 * @param   v1: oprional parsable param 1.
 * @param   v2: oprional parsable param 2.
 * @param   v3: oprional parsable param 3.
 */
void CSystem::log(LogLevel l, QString m, QVariant v1, QVariant v2, QVariant v3)
{
    CLogger::instance().trace(l, std::move(m),
            std::move(v1), std::move(v2), std::move(v3));
}

It is a very dirty, and most likelly now very efficient workaround that allows for parsing up to 3 values. Inside the trace method I ommit the invalid QVariant objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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