ComboBox model change does not update graphical appearance every time
-
Hello, I am quite new to Qt and Qt Quick and I am facing a problem with the update of an item when one of its properties changes.
Basically I have a Combo Box which lists Bluetooth devices as they are discovered and lets the user select one of the devices listed for pairing. To populate the combo box item I use a ListModel to which I append the newly discovered Bluetooth devices. This is the slot which adds new items to the combo box (it is connected to a C++ signal):
function slot_deviceDiscovered(deviceName){ bluetoothPairing.comboInput.model.append({text: deviceName}) if(bluetoothPairing.comboInput.currentIndex === -1) { bluetoothPairing.comboInput.currentIndex = 0 } pairingButton.enable() }
Now when a Bluetooth discovery process is triggered, the devices which are already paired are discovered right away and the signal is emitted really quickly (I find this to be a weird behavior but it's not the problem) which triggers the execution of the slot. However, these devices which are discovered almost instantly don't show up in the combo box although their info was correctly added to the model. Then, as more devices get eventually discovered, their info also get appended to the model. But for them, the combo box also updates itself properly and I can select them in the list.
I'm having a hard time finding the reason why the combo box won't update for the initial devices but will for the subsequent ones.
Any insight?
-
@Payeurp
i guess it's something that happens in the code you've not posted.
Do you change/set the model in your code? It may be that the slot gets called before you replace the model?
Or you do the connection of the slot too early? -
@raven-worx Thanks for your answer. There is no other place in the code where I update/modify the model. Also the combo box is already created when the slot is connected and called, if this is what you meant. Initially I declare the combo box as such with an empty model:
ComboBox { id: comboInput visible: false anchors.fill: parent model: ListModel { id: model } }
And this is the connection in C++ (plus the code to get the settings view inside which the combo box exists):
//Connect signals emitted and received by Settings Form m_pQmlRoot = qmlEngine.rootObjects()[0]; item = qvariant_cast<QObject*>(m_pQmlRoot->property("settingsPage")); QObject::connect(bluetoothManager, SIGNAL(sig_DeviceDiscovered(QVariant)), item, SLOT(slot_deviceDiscovered(QVariant)));