
Originally Posted by
Rakma74
I successfully performed a connection between the comboBoxes, and the slot function, I'm only able to get the argument of the selected data, but not of the current ComboBox which has been clicked...
for (unsigned int row = 0; row < columns_number; row++)
connect(ComboBox[row], SIGNAL(currentIndexChanged(int)), this, SLOT(data_assign(int)));
Would you have any tip ??
I don't know how to identify an object other way than using a trick. There is one way to find out which object sent the signal. In your program, you have connected a couple of object signals into one slot. As the signal sends only index, you cannot really know which combo box sent the signal from signal/slot mechanism. This is tricky and I am not sure if it's proper technique but you may want to use QObject::sender() method that returns an address of a sender. Something like this:
void YourClassName::data_assign(int item) {
std::size_t i = 0;
while((i<rows) && (currentSender != comboBox[i])) {
i++;
}
// "i" the index of your array and points out the sender which is one of the combo box
}
void YourClassName::data_assign(int item) {
QComboBox* currentSender = static_cast<QComboBox*>(QObject::sender());
std::size_t i = 0;
while((i<rows) && (currentSender != comboBox[i])) {
i++;
}
// "i" the index of your array and points out the sender which is one of the combo box
}
To copy to clipboard, switch view to plain text mode
The trick is that QObject::sender() provides an address but it is of a QObject type. Without casting it into QComboBox, compiler wouldn't accept it. Casting this address to QComboBox solves this problem here. However, I personally don't like it. It is very easy to mess up. I wouldn't try to do anything else then just comparing the addresses. In general, it's better to avoid things like this or connect object signals only of the same type to the same slot. Then, you can be certain of the type of a sender.
Hope it helps.
PS> I am still C++ programmer not Qt C++ >.< . Instead of using static_cast<QComboBox*> Qt provides qobject_cast<> which I believe is saver:
QComboBox* currentSender = qobject_cast<QComboBox*>(QObject::sender());
To copy to clipboard, switch view to plain text mode
Bookmarks