Comma | Qt Qml | Qt 6.11.2
Description: [comma] Do not use comma expressions.

- Qt 6.11
- Qt Qml
- Qt Qml Tooling
- qmllint
- QML Lint Warning and Errors
- Comma
- Qt 6.11.2 Reference Documentation

Attached property reuse ◦ Confusing minuses

# Comma

This warning category is spelled `[comma]` by qmllint.

## Do not use comma expressions

### What happened?

A JavaScript comma expression was used outside of a for loop.

### Why is this bad?

Comma expressions reduce readability of the code and obscure side-effects.

### Example

     import QtQuick

     Item {
         Component.onCompleted: init(config, true), enableLogging(categories), run(1000) // millis
     }

To fix this warning, refactor the code to use distinct statements for each operation. This way, each side effect is explicit instead of happening as part of another unrelated operation:

     import QtQuick

     Item {
         Component.onCompleted: {
             init(config, true)
             enableLogging(categories)
             run(1000) // millis
         }
     }

In addition, there are some special considerations for cases where comma expressions appear because a variable is intentionally being captured for a binding, as in the following code, where `previewOfFirstPage` is a function defined in C++ which internally depends on `Config.fontSize`:

     Text {
        // This causes the function to re-run when fontSize changes
        text: Config.fontSize, documentProvider.previewOfFirstPage()
     }

If you encounter this situation, consider one of the following approaches:

- If a function such as `previewOfFirstPage` depends on a property, prefer making this dependency explicit by passing the value as an argument.

       Text {
          text: documentProvider.previewOfFirstPage(Config.fontSize)
       }

- If changing the function signature is undesirable for API reasons, consider replacing the function with a Q_PROPERTY instead, so that change notifications for it can be emitted when the dependency is modified in C++:

       void Config::setFontSize(int fontSize) {
          if (m_fontSize == fontSize)
             return;
          m_fontSize = fontSize;
          emit fontSizeChanged();
          emit previewOfFirstPageChanged();
       }

       Text {
          text: documentProvider.previewOfFirstPage
       }

- If modifying the C++ implementation or adding QML dependencies is not possible, use a `qmllint` directive to silence the warning. Include a comment explaining that the intention is to capture the variable in the binding.

       Text {
          // This causes the function to re-run when fontSize changes
          text: Config.fontSize, documentProvider.previewOfFirstPage() // qmllint disable comma
       }

Attached property reuse ◦ Confusing minuses