-1

I would like to pass an object to another form after a event, in this case a pressed button I can not use QSignalMapper because it is not an int, string or a widget. Signals and slots are for me the only solution, but I do not know how to pass the vTest[i] variable.

std::vector<Test * > vTest;
... fill vTest ...
for(int i = 0; i < vTest.size(); i++)
 {
 QPushButton * pButton = new QPushButton(vTest[i]->getTestName());
 connect(pButton,SIGNAL(clicked(bool)),this,SLOT(slotGoNextPage(???)));       
}

here is the call of the function slot slotGoNextPage

void slotGoNextPage(){

    goNextPage(); }
...
 void goNextPage(){

pForm->var =  ?? // value which comes from vTest[i]
stackedWidget_page->setCurrentIndex(1);
}

And what should I do to pass more than one parameter?

RobertAalto
  • 1,235
  • 1
  • 9
  • 12

2 Answers2

1

You will need to type def your vtest type (to give it a name):

typedef std::vector<Test * > TestVect;

Then you need to register this type with Qt using:

qRegisterMetaType<TestVect>("TestVect");

Then you can use this in your connection

Also, if I recall, you need to have your slot/signal functions taking the same parameters (you can't have different parameters).

code_fodder
  • 15,263
  • 17
  • 90
  • 167
1

If you are using Qt5, you can use a lambda:

std::vector<Test * > vTest;
for(int i = 0; i < vTest.size(); i++) {
    QPushButton * pButton = new QPushButton(vTest[i]->getTestName());
    connect(pButton, &QPushButton::clicked, [=](){ this->goNextPage(vTest[i]); });      
}

Of course, this means that gonextPage() will have to take in a Test * parameter:

void goNextPage(Test * t) {
    pForm->var = t;
    //...
}

You don't need the slot.

dtech
  • 47,916
  • 17
  • 112
  • 190