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
){
connect(this,
SIGNAL(currentIndexChanged
(int)),
this,
SIGNAL(currentIndexChanged
(QVariant)));
}
ComboBoxCopy::ComboBoxCopy(QWidget *parent)
: QComboBox(parent)
{
connect(this,SIGNAL(currentIndexChanged(int)),this,SIGNAL(currentIndexChanged(QVariant)));
}
To copy to clipboard, switch view to plain text mode
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)));
connect(this,SIGNAL(currentIndexChanged(int)),this,SIGNAL(currentIndexChangedData(int)));
To copy to clipboard, switch view to plain text mode
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)
{
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){
emit currentIndexChangedData(userdata);
}else{
//error!
}
}
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!
}
}
To copy to clipboard, switch view to plain text mode
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
){
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){
}else{
//error!
}
}
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!
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks