PDA

View Full Version : QDataWidgetMapper and QCombobox



miraks
6th August 2008, 21:58
I am using QDataWidgetMapper successfully with QLineEdit and other widgets but it doesn't work with QCombobox.

If I replace the QCombobox by a QLineEdit, it works: the QLineEdit is updated when I select a line in my table view.
If I come back to the QCombobox, it doesn't work: the QCombobox is not updated when I select a line in my table view.

Do you know why ?
Is it normal or is it a bug ?
Do you know a simple workaround ?

Thank you for your help

wysota
6th August 2008, 23:21
It's normal. You have to provide a custom delegate for the mapper so that it knows how to convert data between the model and the editor (combobox in this case). As far as I remember there is an article on Qt Quarterly that describes the solution.

miraks
7th August 2008, 22:11
It's normal. You have to provide a custom delegate for the mapper so that it knows how to convert data between the model and the editor (combobox in this case). As far as I remember there is an article on Qt Quarterly that describes the solution.

Hi wysota,

With your answer, I found the following page http://doc.trolltech.com/qq/qq21-datawidgetmapper.html and understood that it doesn't work with QComboBox because of QComboBox doesn't have a user property.

I didn't use the delegate solution proposed on the page, I preferred use the following one:
Creation of a new Combo box class with a user property:

class __attribute__ ((visibility("default"))) SKGComboBox : public KComboBox
{
Q_OBJECT
Q_PROPERTY( QString text READ text WRITE setText USER true)

public:
/**
* Default Constructor
*/
SKGComboBox(QWidget *parent = 0);

/**
* Default Destructor
*/
virtual ~SKGComboBox();

/**
* Get the text for the combo
* @return the text
*/
virtual QString text() const;


/**
* Set the text for the combo
* @param iText the text
*/
virtual void setText (const QString& iText);


};


SKGComboBox::SKGComboBox(QWidget *parent)
: KComboBox(parent)
{
}

SKGComboBox::~SKGComboBox()
{
}

QString SKGComboBox::text() const
{
return currentText();
}

void SKGComboBox::setText (const QString& iText)
{
int pos=findText(iText);
if (pos==-1)
{
pos=0;
insertItem(pos, iText);
}
setCurrentIndex(pos);
}

And it works now like for QLineEdit.

Thank you again.

wysota
7th August 2008, 22:19
The problem with your solution is that you have to set the list of available choices manually, but that's ok if you're satisfied with it.

I was referring to this article, by the way:
http://doc.trolltech.com/qq/qq21-datawidgetmapper.html#usingdelegatestoofferchoices

kaje44
6th December 2008, 18:53
I had same problem with http://doc.trolltech.com/qq/qq21-datawidgetmapper.html,
but your solution with class SKGComboBox WORK...super..thanks