PDA

View Full Version : QComboBox problem



^NyAw^
31st March 2008, 09:49
Hi,

I have this code on "on_TextComboBox_textChanged(const QString &qText)" slot


ui.TextComboBox->setCompleter(0);
ui.TextComboBox->setEditable(false); //To not let the combo emit "textChanged"
ui.TextComboBox->clear();
//Fill the combo with the list elements
if (!qLList.isEmpty())
for (int i = 0; i < qLList.size(); ++i)
ui.TextComboBox->addItem(QObject::tr(qLList.at(i).toAscii().data()) );

//Adding the text written by hand on the combo
ui.TextComboBox->addItem(qText);
if (m_pCParameter->isEditable())
ui.TextComboBox->setEditable(true);


It crashes when exits the slot on "QCompleter::completionMode()" function.

What I want is a Combo that contains some items and the combo also have to be editable. So, when the user starts writting I want to insert the text as an item. For doing this I clear the items, add the items and finally add the text written as item. I don't want the completer so I try to disable it calling "ui.TextComboBox->setCompleter(0);".

Thanks,

wysota
31st March 2008, 10:02
How does that differ from a simple editable combobox? Your current solution won't work as you are modifying the state of the combo from within a slot connected to a signal emited as a result of a change of the combobox itself causing other slots connected to the same signal to malfunction.

^NyAw^
31st March 2008, 10:13
Hi,

Sorry, I forget to tell you that I'm modifying internal data. I have a own class that is getting the text written on the combo.



m_pCParameter->setText(qText);

wysota
31st March 2008, 11:13
What is m_pCParameter and how is it related to ui.TextComboBox?

^NyAw^
31st March 2008, 11:43
Hi,

"m_pCParameter" is a pointer to own class. This class contains a QList that is a list of possibilities that is used to fill the combo. Then I let the user choose one of this elements or to write a different text.

The possibilities are saved untranslated on the QList possibilitites and translated when are inserted to the combo, but when the user selects an item, the internal object needs to save the untranslated text.
So in "on_TextComboBox_textChanged(const QString &qText)" slot I use this code:





ParametreTextWidget::on_TextComboBox_textChanged(c onst QString &qText)
{
int iIndex = ui.TextComboBox->findText(qText,Qt::MatchFixedString | Qt::MatchCaseSensitive);
if (iIndex != -1)
{
QStringList qList = m_pCParameter->getList();
if (!qList.empty())
{
QString qOriginalText = qList.at(iIndex);
m_pCParameter->setText(qOriginalText);
}
else
{
m_pCParameter->setText(qText);
}
}

else
{
ui.TextComboBox->setCompleter(0);
ui.TextComboBox->setEditable(false); //To not let the combo emit "textChanged"
ui.TextComboBox->clear(); //Fill the combo with the list elements
if (!qLList.isEmpty())
for (int i = 0; i < qLList.size(); ++i)
ui.TextComboBox->addItem(QObject (http://doc.trolltech.com/latest/QObject.html)::tr(qLList.at(i).toAscii().data())); //Adding the text written by hand on the combo
ui.TextComboBox->addItem(qText);
if (m_pCParameter->isEditable())
ui.TextComboBox->setEditable(true);


What I try to do if the item is handwritten(not exist on the possibility list) is to insert it as a item and save it to internal object.

Thanks,