Connecting Many signals to one slot
Added by Gavin Cobb almost 4 years ago
Good evening. To specifically state what I am trying to accomplish, I would like to create a dynamic table of data that when a table cell is pressed, a slot will increment some variable.
I have a table of strings with corresponding table cells. I want to use these table cells with a mouse event (.clicked()) as a signal and a lambda expression as slot. However, I want all of the tablecell mouse event signals to correspond to 1 slot. Also, the table is dynamic so I would like for the signal and slot to be connected when the tablecell is created.
What I have so far is a lambda expression that is a slot that adds the task from a lineedit to the grid when the add task button is pressed. Easy. Next I need to bind each tablecell to a slot that increments a variable when pressed. I have attempted many ways of binding many signals to 1 slot with boost bind and std::bind to no avail. I've used different methods and a few times I have come up with being able to connect only the last tablecell to be created at the time to the slot. I believe my best attempt is when I tried to make a vector of function objects to act as many slots corresponding to many signals dynamically as follows:
//TCells is a vectors of tablecell pointers
//Task adder is the slot that adds the task to the table
//addbutton is the button connected to a mouse event as a signal
static std::vector<std::function<void()>> functors;
auto TaskAdder = [=]{
this~~taskText = this>taskEdit->displayText();taskTextStr = this
this>taskText.toUTF8();AddTask(Sboard
Sboard->AddTasker(taskTextStr);
if(Sboard->flag == 1){
this>GridStrings);setText(std::to_string(this
Sboard->flag = 0;
}
functors.push_back([this] { MovesText>numMoves)); });TCells[this
this>size - 2]clicked().connect(std::bind(functors[this~~>toDoCount]));
this->toDoCount;
};
addButton->clicked().connect(std::bind(TaskAdder));
This allows me to only increment the numMoves when the last tablecell is clicked. Not other table cells.
A similar attempt that had the same outcome that I found in the forums is:
auto TasktoProgress = [=]{
MovesText~~setText(std::to_string(this~~>numMoves));
};
auto TaskAdder = [=]{
this~~taskText = this>taskEdit->displayText();taskTextStr = this
this>taskText.toUTF8();AddTask(Sboard~~>GridStrings);
Sboard->AddTasker(taskTextStr);
if(Sboard->flag == 1){
this
Sboard->flag = 0;
}
this~~TCells[this~~>size - 2]->clicked().connect(std::bind(TasktoProgress));
};
addButton->clicked().connect(std::bind(TaskAdder));
Please help. I can't find any information online that helps me solve the problem of many signals to 1 slot. Thank you for reading.