PDA

View Full Version : Creating signals in subclasses



Cotlone
21st June 2010, 01:27
Hi everyone, I was hoping someone could help me out with this fairly simple (I hope) problem.

I have created a subclass of the QComboBox, I want to change the combo box index based on the value stored in its associated userData. I essentially want to create a signal call currentIndexChanged(QVariant) (or in my case I am using int anyway). How do I cause my subclassed QComboBox to emit this signal when the currentIndexChanged(int) signal is emitted?

I thought I should stick this connect in the header, but I seem to be having some problems...



ComboBoxCopy::ComboBoxCopy(QWidget *parent)
: QComboBox(parent)
{
connect(this,SIGNAL(currentIndexChanged(int)),this ,SIGNAL(currentIndexChanged(QVariant)));
}


or I'd be happy to rename the signal to prevent any possible confusing, at the moment I'm trying this.


connect(this,SIGNAL(currentIndexChanged(int)),this ,SIGNAL(currentIndexChangedData(int)));

I guess what I'm asking is, is this the right way to set this up?

I appreciate any help, thanks!
Michael

Edit: Ok, so I realised that the parameters passed between these signals are obviously not interchangeable (one being an index and the other being a data value). I have now reimplemented the setCurrentIndex(int) slot from the QComboBox as follows, adding the required signal emission


void ComboBoxCopy::setCurrentIndex(int index)
{
QComboBox::setCurrentIndex(index);
int userdata = itemData(index, Qt::UserRole).toInt();
emit currentIndexChangedData(userdata);
}


void ComboBoxCopy::setCurrentIndexFromData(int userdata)
{
int index = findData(userdata,Qt::UserRole,Qt::MatchExactly);
if(index != -1){
QComboBox::setCurrentIndex(index);
emit currentIndexChangedData(userdata);
}else{
//error!
}
}

I still value any opinions on this, despite my monologuing here :)
- Michael

Edit2: Above code was obviously not effective, as I thought it was. Damn my premature responses to my own post. This following code seems to make the most simple solution. I've just posted it here so, in the case that it helps someone else. That said there is a good chance I'll find a problem with this too...


ComboBoxCopy::ComboBoxCopy(QWidget *parent)
: QComboBox(parent)
{
connect(this,SIGNAL(currentIndexChanged(int)),this ,SLOT(emitDatafromIndex(int)));
}

void ComboBoxCopy::emitDatafromIndex(int index)
{
int userdata = itemData(index, Qt::UserRole).toInt();
emit currentIndexChangedData(userdata);
}

void ComboBoxCopy::setCurrentIndexFromData(int userdata)
{
int index = findData(userdata,Qt::UserRole,Qt::MatchExactly);
if(index != -1){
QComboBox::setCurrentIndex(index);;
}else{
//error!
}
}

franz
21st June 2010, 23:24
The last snippet is what I would use. However, I would call the signal something like void currentDataChanged(QVariant) (currentIndexChangedData() just sounds odd from a natural language point of view) and I would probably keep the QVariant in the signature as well.

Cotlone
22nd June 2010, 01:16
Cool, thanks for your input. I didn't like the name either, though I had previously thought about your suggestion and from a natural language point of view it seems to imply that the data has changed, when it is actually the index. I guess a good way to do it would be to just call it currentIndexChanged(QVariant), which would be distinguishable from the QComboBox's signal currentIndexChanged(int).