Matteo Barbieri
Matteo Barbieri

Reputation: 1

Get the size of a QStringList in QML from a property passed from C++ backend

Surely is a silly question but i can't get out of it...

Is there any method in the QML side to get the size of a QStringList passed as a property from the c++ backend? I can use the property to fill the model of a combobox but i can't find a straight way to get the size (of course i can expose an invokable method from the cpp but it's not what i want)

example:

test.h

class Test : public Workflow
{
  Q_OBJECT
  Q_PROPERTY(QStringList availableCameras MEMBER m_availableCameras NOTIFY availableDevicesChanged)
[...]

private:
qStringList m_availableCameras

test.qml

    GroupBox{
      anchors.fill: parent
      title: "Camera Panel"
      property string selectedCamera: ""
    
      function showImage(){
        if(test.availableCameras.size() === 1) // NOT WORKING
        {
          return configurator.lastSingleImage
        }
        if(selectedCamera === test.rightCamSerialConf)
        {
          return configurator.lastRightImage
        }
        if(selectedCamera === test.leftCamSerialConf)
        {
          return configurator.lastLeftImage
        }
    
      }
      ComboBox{
                  model: test.availableCameras // WORKING
                  editable: false
                  onEditTextChanged: selectedCamera = editText
                } 
   [...]
   }

Upvotes: 0

Views: 1631

Answers (1)

GrecKo
GrecKo

Reputation: 7170

A QStringList acts as a JS array of strings in QML. In general you can use Array's functions on it : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

And when you can't, you can explicitely make it an actual array with Array.from

In your case you want length:

if(test.availableCameras.length === 1)

Upvotes: 1

Related Questions