QVector<QComboBox*> signal and slot problem
-
Hi All,
Basic Aim - Once the callback function(SLOT) is called i want to know which of the combobox inside my vector is being triggered.
@QVector<QComboBox* > test = new QComboBox();
for (int i = 0 ; i< abc.size(); i++)
{
...
test.at(i)->additem("abc");
...connect (test[i],SIGNAL(currentIndexChange(QString)),this,SLOT(comboboxChecked(QString))@
}So, basically I have a qvector of comboboxes(test) and i'm adding multiple comboboxes in my GUI, my connect statement also works fine but i'm only able to get the change of the index inside the combobox which the string of the changed index. What I also want to get is the id/index of combobox in which the value is being changed inside my callback function. (So, in short i want to get the counter value of the loop i.e "i" in this case inside my callback function)
Is there any way of doing this. Please help. Thanks in advance.
-
Hi kd188
Welcome to devnet.
Take a look to "QSignalMapper Class":https://qt-project.org/doc/qt-5/qsignalmapper.html -
You can add the "i" from the for loop as a dynamic property
@
comboBox->setProperty("i", i);
@To get the sender combobox from inside the slot:
@
if (QComboBox *c = qobject_cast<QComboBox *>(sender())) {
bool ok;
int i = c->getProperty("i").toInt(&ok);
if (ok) {
qDebug() << "I'm a combo and am feeling ok";
}
}
@That would do what you want, but if the slot has access to the QVector you don't need to set the property, just lookup the index using the pointer.
-
[quote author="Seamus Boyle" date="1393395066"]You can add the "i" from the for loop as a dynamic property
@
comboBox->setProperty("i", i);
@To get the sender combobox from inside the slot:
@
if (QComboBox *c = qobject_cast<QComboBox *>(sender())) {
bool ok;
int i = c->getProperty("i").toInt(&ok);
if (ok) {
qDebug() << "I'm a combo and am feeling ok";
}
}
@That would do what you want, but if the slot has access to the QVector you don't need to set the property, just lookup the index using the pointer.[/quote]
Hi Seamus Boyle,
Since my signal is currentIndexChange(QString). It's only sending me the QString in my callback function so even though my QVector is global, i would only be able to access just the QString.
My main purpose is the get the changed Index's String + the number of comboboxSo in my callback function what I want is
//where String points to the text inside combobox and id is the combobox reference
@void comboboxChecked(QString test, int id)@but currently my callback function is
@void comboboxChecked(QString test)@ -
[quote author="qxoz" date="1393388801"]Hi kd188
Welcome to devnet.
Take a look to "QSignalMapper Class":https://qt-project.org/doc/qt-5/qsignalmapper.html[/quote]Hi qxoz,
Thanks for replying. I have tried to use the signal mapper with my comboboxes like this
@MyClass::MyClass(QWidget pParent):QWidget(pParent),
m_window(new QDialog())
{
signalMap = new QSignalMapper(this);
QVector<QComboBox > test = new QComboBox();
createLayout();
}
void createLayout()
{
for (int i = 0 ; i< abc.size(); i++)
{
...
bool check;
check = connect (test[i],SIGNAL(currentIndexChange(QString)),signalMap,SLOT(map());
signalMap->setMapping(test[i],i);
...
}
}@in this case my check variable is false. So, I guess my first connect statement is not working.
How should I use signalMapper so that i can get the string changed inside my combobox as well as the id referencing to it?
In this case what i want to get inside my callback is the "i" variable inside my loop(to refer the index of the combobox) and the QString which is changed inside the combobox. -
The qobject_cast of sender() returns a QComboBox. It does not matter what signal you use.
-
In case not using QSignalMapper (as Seamus Boyle wrote)
@void comboboxChecked(QString)
{
if (QComboBox *c = qobject_cast<QComboBox *>(sender()))
{
//now you got combo box that emit signal. And you can get any data you want
... = c->currentIndex();
... = c->lineEdit->text();
}
}@in case using QSignalMapper
@
void createLayout()
{
bool check;
for (int i = 0 ; i< abc.size(); i++)
{
...
check = connect (test[i],SIGNAL(currentIndexChange(QString)),signalMap,SLOT(map());
signalMap->setMapping(test[i], i);
...
}
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(some_custom_slot(int)));
}@
then in slot
@void some_custom_slot(int id)
{
if (QComboBox *c = qobject_cast<QComboBox *>(signalMapper->mapping(id)))
{
//now you got combo box that emit signal. And you can get any data you want
... = c->currentIndex();
... = c->lineEdit->text();
}
}
@ -
Hi qxoz, Seamus
Thanks for the help. I got the point now. :) Thanks!