QML model inside model is undefined

1.8k Views Asked by At

I have a QAbstractListModel called SerialPortList which represents a list of SerialPorts. Each SerialPort has a QList<int> supportedBaudRates property. The SerialPortList model's display role is simply QVariant::fromValue(mSerialPorts[i]), i.e. a SerialPort instance.

I display my list model in a Repeater like this (highly simplified):

SerialPortList {
    id: ports
}

Column {
    Repeater {
        id: portList
        // ...
        model: ports
        Text { text: model.display.portName }
        ComboBox {
            model: model.display.supportedBaudRates
            // ...
        }
    }
}

That is, a list of the serial ports name, and then a combo box with its supported baud rates.

The weird thing is, although the text is displayed fine, apparently for the combo box model.display is undefined. If I replace the combo box with this:

            Text {
                text: model.display.supportedBaudRates.join(" ")
            }

then it produces the result you'd expect!

What is going on here? I can only assume that model is redefined inside the ComboBox so it thinks it is referring to itself, but I've tried all kinds of id references like portList.model.display.supportedBaudRates that I can think of and none of them work.

Edit

Kakadu pointed out that you can't use a QList<int> as a model. Kind of annoying, but I've changed it to QStringList which you apparently can use. Sadly the error remains the same.

Edit 2

I changed the model: in the ComboBox to model: { console.log("model: " + model); } and it prints 1, which makes me sure that model refers to itself.

1

There are 1 best solutions below

0
On

Oh I finally got it to work! It's simply a namespace problem - model is something of a magic variable so you can't use id qualifiers on it.

Fortunately model. is implicitly prepended to any variable accesses in the delegate, so I changed my code to this and it worked:

    ComboBox {
        model: display.supportedBaudRatesAsStrings
        // ...
    }

(The ...AsStrings property is a QStringList duplicate of the original QList<int> property.)