PDA

View Full Version : connecting qcombobox with qdatawidgetmapper



sliptonic
1st February 2017, 00:10
I'm building an python MVC application with a qAbstractDataModel. I'm using a qDataWidgetMapper to populate fields in edit panel. That all works well and now I'm trying understand how to connect qcombobox to present options to the user.

I expect that this is a very common use-case but I can't find any introductory examples of how to set it up. I've searched extensively on Google and Stackoverflow and I find things that make me think I need to build a custom qItemDelegate but I can't find a tutorial or example to get started.

I'm sorry if I don't even know enough to ask the right question but please point me in the right direction.

FWIW, my code is intended for an open source project but I've built a stand-alone version of the tool here: https://github.com/sliptonic/toollibrarytests

anda_skoa
1st February 2017, 08:48
It depends on what your model has as the data for the cell which is represented by the combobox.

If the data for this cell is an zero-based integer, then you can most likely map that directly into the combobox' currentIndex.

Lets assume you have three values: "A", "B", "C", and the model associates the numbers 0, 1 and 2 respectively.



QComboBox *combo = new QComboBox;
combo->addItem("A");
combo->addItem("B");
combo->addItem("C");

dataWidgetMapper->addMapping(combo, columnInTheModel, "currentIndex");


Cheers,
_

sliptonic
1st February 2017, 15:49
Thanks for the reply. Unfortunately my data isn't normalized like that. The model is storing the actual string.
That's why I started looking at delegates.

I have a function that converts from strings to indices and back.

anda_skoa
2nd February 2017, 09:31
Ah, I see.

If the strings are predefined, then you could map to the combobox "currentText" property.

If the strings change during runtime, then you'll indeed likely need a delegate.

Cheers,
_

sliptonic
2nd February 2017, 18:06
I've tried mapping to currentText but can't get it to work.

Regardless, for translation and future proof, I'd prefer to load the choices at run time so I need the delegate. Which brings me back to my first post that I'm having trouble finding any Python examples of doing that.