Qt Signal, passing parameters to slot.
-
Here's the situation, I have a database which I am reading from, and a struct which I created that will read the data from the DB and copied into my struct. A QList<struct> is used, and this QList stores a list of structs(which is each record in the DB). Inside this DB, there is a field which stores a pointer that points to a variable which I want to connect to, and when the value changed, I want to pass the struct to my slot. Is it possible? My code structure is as shown below:
/* -- struct = each record in the DB -- */
typedef struct{ //my struct here }condition;
/* -- QList stores struct, QList stores the entire DB records -- */
QList<condition> conditionList;
/* -- check if var with within lower and upper limit, if so return true -- */
bool compareLimit(condition cond, int var){ if (var >= cond.lowerLimit && var <= cond.upperLimit) return true; else return false; } void storeToStruct(){ for (int x=0; x<DB.length(); x++){ conditionList.append(DB.record(x); connect(conditionList.at(x).var, SIGNAL(valueChanged()), this, SLOT(compareLimit())); } }
So what is happening here is every time a record is being read from the database, I want to run a connect statement which will connect a 'var' to 'valueChanged()', and call the generic slot function 'compare'. Problem is I have no idea how do I pass the particular record into the slot function.
Will anyone please give me some suggestions or advice? Any suggestions/advice is greatly appreciated. This is a dead-end for me T_T
-
What is var? If it is not derived from QObject then it cannot have any signals.
Your struct needs to inherit from QObject and you have to add Q_OBJECT makro at the beginning of your struct. Only classes/structs derived from QObject can have signals. Then you add valueChanged() signal to your struct. In compareLimit() slot you can call sender() to get the object which emited the signal (an instance of your struct in this case).
connect(conditionList.at(x), SIGNAL(valueChanged()), this, SLOT(compareLimit()));You should read http://doc.qt.io/qt-5.5/signalsandslots.html
-
This is usually accomplished by using a
QSignalMapper
(http://doc.qt.io/qt-5/qsignalmapper.html). -
Okay, sorry, I didn't point this out. Let's just say 'var' is a custom type created by someone else and it is able to have signal. It has been tested and proven to work.
What I am trying to achieve now is when I'm in the for-loop, a connect statement is called, and compareLimit() will be called when value is changed. Problem is compareLimit() doesn't know which upper and lower limit to compare 'var' against, because there is a QList of struct.
I want conditionList.at(x).var to be compared against conditionList.at(x).lowerLimit and conditionList.at(x).upperLimit.
-
As long as var inherits QObject you can do as followed.
QSignalMapper *sigMap = new QSignalMapper(); connect(sigMap,SIGNAL(mapped(QObject *)),this,SLOT(compareLimit(QObject*))); void storeRoStruct() { for (int x=0; x<DB.length(); x++){ conditionList.append(DB.record(x); connect(conditionList.at(x).var, SIGNAL(valueChanged()), sigMap, SLOT(map())); sigMap->setMapping(conditionList.at(x).var, conditionList.at(x).var); } }
And in the Slot make a dynamic_cast to var.