Solved Pass two arguments to a slot
-
Same .cpp file.
void ClassOne::execute() { QVector<QString> myArray; DialogClass dlg; if(QDialog::Accepted == dlg.exec()) { for(int i = 0; i < 10 ; i++){ other = getInfo(); myArray.push_back(other); } connect(dlg.ComboBox, SIGNAL(currentIndexChanged(int)), &dlg, mySlot(int))); dlg.comboBox(anotherArray); // This fill the combobox } } void DialogClass::mySlot(int index){ //I Need the myArray here. }
-
As I said the easiest way is to use a vector but keep in mind it is not a direct solution.
First you have to send the currentIndexChanged(int) to a slot that accepts (int) as-well, like this:
connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(comboBoxCourseLoadValues(int)));
But the Slot is capable of remembering the value of emitted signal until it receives another call.
As soon as the slot receives two INTs(what you are looking for) you can do what ever you want with it or emit another signal from there to another slot which accepts (int,int).
Here is the code:void MainWindow::comboBoxCourseLoadValues(int p) { static QVector<int> vec; vec.push_front(p); if(vec.size()>1){ /*Here you have got 2 INTs.*/ /*And you can do what ever you want with them. Eg:Send a new Signal(int,int)*/ /*but make sure to not send the address of them somewhere else AT ALL*/ /*becuase we are going to clear the vector Now*/ qDebug()<<vec.at(0)<<vec.at(1); //for viewing the out put result vec.clear(); }
}
-
I did not find the class definition for DialogClass above. However, it may look similar like:
class Dialog : public QObject { ... public: QComboBox ComboBox; ... public slots: void mySlot(int index); ... };
What hinders you to add the second array?
class Dialog : public QObject { ... QVector<QString> mArray; public: QComboBox ComboBox; ... void setSecondArray ( const QVector<QString> & array ) { mArray = array; } public slots: void mySlot(int index); ... }; void DialogClass::mySlot(int index){ do_something_with = mArray.at(index); //I Need the myArray here. }
-
@Nouriemm
Ohh Thanks for this explanation, but in this case I need a parameter of another type:connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(comboBoxCourseLoadValues(int, QVector)));
But I like your explanation, I learned too much about signals.
-
The @mrjj give me a solution with pointers in another topic:
https://forum.qt.io/topic/62427/get-a-array-inside-slotBut what you say is very useful.
QVector<QString> mArray; ///is in another class definition
Why the code below is in the class definition? When it's called? How I can do learn more about it?
void setSecondArray ( const QVector<QString> & array ) { mArray = array; }
-
@Helson said:
Why the code below is in the class definition? When it's called? How I can do learn more about it?
void setSecondArray ( const QVector<QString> & array )
{
mArray = array;
}That is basically inline code. As you might have noted is the array in my definition in the private section. Therefore, is somewhat secured, because not diretly accessible. With the inline definition the code section will be introduced directly whereever you use. It spares the overhead of calling a member function.
It has ups and downs and should be used only for short sections of code. Here is a link to an articleI seemed to remember that you noted a deadline. Therefore "kis"="keep it simple".
I looked at @mrjj code you linked. As far as I saw you cannot use it. You seem to use the original signals of QComboBox and that gives you an index for the current line. To make out of int index a complete array with other information would be magic. -
@Helson
Hello,
I'm a bit slow to respond these days, and have little to contribute to @koahnig's excellent points. I would advise against @Nouriemm's suggestion though only because of the unneeded use of static variables. While here it's not such a problem (since GUI objects are not reentrant anyway), in many cases using static variables like this:void MainWindow::comboBoxCourseLoadValues(int p) { static QVector<int> vec; ... }
can get you a lot of headaches ... especially when order of construction/destruction is important.
Kind regards.
-
I could learn a bit of QT and C++ throught the tips and advice.
I hope one day contribute to the forum too.Thanks for the help all!
-
@kshegunov
Hey there,
I know static variables are really fearful for the vast majority of programmers.
But in my case it is not really as bad as it looks like and I don't think it makes any trouble in this case again.
Plus All I was trying to do was to demonstrate my solution in a small piece of code which of-course can be implemented in hundreds of other ways and this is the part which I call it the art of programming.
Cheers All -
@Nouriemm
Hello,
I hope you're not taking it personally, I certainly wasn't trying to undermine your effort. As you can see I've noted that in this particular case your suggested solution is as good as any, I was only pointing out potential pitfalls that could occur in more involved cases. While static variables are in no way fearsome, a good practice is to avoid them if possible, and not because you're scared or don't know how to handle them, but because of their nasty side effects that one should always be keeping in mind. Opaque pointers are also usable, but I'm guessing you're not passing your objects around throughvoid *
just because you can, are you?Statics, globals and singletons (which are basically the same thing) have the nasty side effect of promoting interdependence between components and you have to take special measures when order of construction/destruction is important. On top of that, they, because such is their nature, are breaking reentrancy and you have to take special steps to work around it. Consider the following very simple example:
class A { A() {} void acquireGlobalResource() {} }; A * a = new A(); class B { B() { a->acquireGlobalResource(); } }; B b; int main() { // ... }
Now, the question is, can you guarantee that
a
will be initialized beforeb
, so when the constructor of the class B runs you get a valid reference?Kind regards.
-
@kshegunov
First let me clear that I am not the advocate of static variables. Honestly, I try to avoid them as much as it is possible. All I was trying to do, again, was to demonstrate a solution not an absolute answer. In my opinion the best way to demonstrate a variable that keeps it's state is a static variable.
One more thing(Trust me it is not personal), There is a big difference between the static variable in the given example by you and my static variable. Your static variable has external-linkage but my static variable has internal linkage inside its scope.
The only and only limitation that I accept for my solution is serialized access(mutex) of the function that holds the static variable in multi-threading(which in this case is not a biggie, is it? ).