How to get previous selection from QComboBox?
-
Hi,
My QComboBox along with QLineEdit contains different datatypes (3 fields of int and 3 fields of string) and I'm already doing some validation based on what the user selects as type. For example, when an integer type is selected from the QComboBox then I will not let the user type regular text in the QLineEdit object and vice versa.Whenever the combo selection changes, I'm simply clearing the QLineEdit which I don't want.
_typeBox = new QComboBox(this); _typeBox->insertItem(static_cast<int>(ItemType::e_CID), tr("Cellphone ID")); _typeBox->insertItem(static_cast<int>(ItemType::e_EID), tr("Emergeny ID")); _typeBox->insertItem(static_cast<int>(ItemType::e_FName), tr("First Name")); _typeBox->insertItem(static_cast<int>(ItemType::e_LName), tr("Last Name")); _typeBox->insertItem(static_cast<int>(ItemType::e_PName), tr("Property Name")); _typeBox->insertItem(static_cast<int>(ItemType::e_PValue), tr("Property Value")); connect(_typeBox, SIGNAL(activated(int)), this, SLOT(typeIndexChanged(int))); void typeIndexChanged(int index) { _lineEdit->clear(); }
For example, if the current selection is CID and QLineEdit contains a valid integer and the user now changes the selection to EID, I shouldn't clear the contents of QLineEdit.
Similarly, if the current selection is EID and the user changes the selection to FName then the QLineEdit should be cleared out because the existing integer is no longer valid now. -
if i dont misunderstood your question , I would define a variable that holds the previous index information.
private: int _lastIndex=0 //-------- void typeIndexChanged(int index) { if(_lastIndex ==0) { _lastIndex = index; } else if( index != _lastIndex) { _lineEdit->clear(); _lastIndex = index; } }
this should work
-
@vijaychsk said in How to get previous selection from QComboBox?:
For example, when an integer type is selected from the QComboBox then I will not let the user type regular text in the QLineEdit object and vice versa.
@Nevez has shown what you would indeed need to do if you need to know what the previously selected index the user is changing from was. However, although you talk about that in your final paragraph, if you take the sentence at the start I have quoted I am not convinced you really want/need that. That sentence implies all you need to do is check whether the text you presently have is or is not still valid for the newly selected combo index, regardless of the old index.
Additionally, you may or may not need something like that depending on which signal you choose to connect to:
-
QComboBox::activated(int index): I am not clear from the docs whether this is emitted before the current index is changed (in which case you can still look at
QComboBox::currentIndex()
) or after the current index has been changed (in which case you will need the_lastIndex
member variable). -
QComboBox::currentIndexChanged(int index): This is definitely emitted after the current index has been changed (so you would definitely need the
_lastIndex
member variable).
To clear out, or not clear out, the
QLineEdit
depending on the newly selected combo index you will want something like:void typeIndexChanged(int index) { // `index` must be the *new* current index if (!lineEditContentIsValidForComboIndex(static_cast<ItemType>(index))) _lineEdit->clear(); } bool lineEditContentIsValidForComboIndex(ItemType itemType) { switch (itemType) { case ItemType::e_CID: case ItemType::e_EID: case ItemType::e_PValue: return textIsNumber(_lineEdit->text()); case ItemType::e_FName: case ItemType::e_LName: return textIsLetters(_lineEdit->text()); case ItemType::e_PName: default: return true; } }
You may be able to use your "I'm already doing some validation based on what the user selects as type" functions/validators here for
textIsNumber(_lineEdit->text())
etc.If you really do want to take the previously selected index into account, pass the
_lastIndex
as a parameter to this function too.That is what you asked for. However, I am not at all sure it is a good idea. Just because I have previously entered a number which is valid for
Cellphone ID
, why would I want to preserve that if I change toEmergeny ID
? Seems confusing to me, but up to you. -