PDA

View Full Version : How to add a parameter to a slot



Potch
4th May 2008, 12:10
Hello,

This is the first time I use Qt and I need an advice to perform the following task.
With this loop, I display a combobox on each column of the first row of my QTableWidget.


for (int col=0 ; col <= colonnes ; ++col)
{
QComboBox *moncombo = new QComboBox;
moncombo->addItems(PL_variables);
TableW->setCellWidget(0, col, moncombo);
connect(moncombo, SIGNAL(activated(int)),this, SLOT(associate(int)));
}

With connect(moncombo, SIGNAL(activated(int)),this, SLOT(associate(int))); I can transmet the index of the combo to the slot.

My problem is that I also have to transmet the number of the column on which the signal had been sent.
I guess I should to use the horizontalHeader function or something like that, but I don't really know how to do this.

Thanks

marcel
4th May 2008, 12:31
You could use QSignalMapper for this.

Potch
4th May 2008, 18:09
Thanks for your quick answer Marcel. I didn"t know this class yet, I read the documentation right now...

wysota
4th May 2008, 19:45
It would probably be wiser to use a custom delegate instead of cell widgets. It'll get awfully slow if you have more than 10 of them.

Potch
5th May 2008, 23:09
Thank you for your advise Wysota. I'm currently learning Qt and custom delegates is the next chapter for me :o Normaly, they are only 8 of them, so it's not to slow to execute.

I could finaly manage to retrieve the number of column as follow :

for (int col=0 ; col <= colonnes ; ++col)
{
QComboBox *moncombo = new QComboBox;
moncombo->addItems(PL_variables);
moncombo->setObjectName(QString::number(col)); // the name of my combo is the number of my column
TableW->setCellWidget(0, col, moncombo);
connect(moncombo, SIGNAL(activated(int)),this, SLOT(associate(int)));
}

The slot

void MainWindow::associate(int index)
{...
int c = sender()->objectName().toInt();
...}

Regarding to the documentation, it's not a very correct way to do this... That's why I try to implement another solution using QSignalMapper.

Thanks for your advices...