QCombobox Signals And Slots

QCombobox has 8 signals.

void activated(int index)
void activated(const QString &text)
void currentIndexChanged(int index)
void currentIndexChanged(const QString &text)
void currentTextChanged(const QString &text)
void editTextChanged(const QString &text)
void highlighted(int index)
void highlighted(const QString &text)

If you want to get signals, you must connect these to slots. But if you want to get one of these, enougt to connect one. I connected everyone just create example.

Slots are functions must be defined as “slot ” like this:

private slots:
  void onComboboxActivated(int index);
  void onComboboxActivated(const QString &text);
  void onComboboxCurrentIndexChanged(int index);
  void onComboboxCurrentIndexChanged(const QString &text);
  void onComboboxCurrentTextChanged(const QString &text);
  void onComboboxEditTextChanged(const QString &text);
  void onComboboxHighlighted(int index);
  void onComboboxHighlighted(const QString &text);

Slots and signals must have same parameters. Otherwise, the connection will not occur. Not only for connection, slot function must have same parameters with signal. For example, this sample doesn’t work:

QObject::connect(ui.comboBox, SIGNAL(activated(int)), this, SLOT(onComboboxActivated()));

But it works:

QObject::connect(ui.comboBox, SIGNAL(activated(int)), this, SLOT(onComboboxActivated(int)));

Connections:

QObject::connect(ui.comboBox, SIGNAL(activated(int)), this, SLOT(onComboboxActivated(int)));
QObject::connect(ui.comboBox, SIGNAL(activated(const QString &)), this, SLOT(onComboboxActivated(const QString &)));
QObject::connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboboxCurrentIndexChanged(int)));
QObject::connect(ui.comboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(onComboboxCurrentIndexChanged(const QString &)));
QObject::connect(ui.comboBox, SIGNAL(currentTextChanged(const QString &)), this, SLOT(onComboboxCurrentTextChanged(const QString &)));
QObject::connect(ui.comboBox, SIGNAL(editTextChanged(const QString &)), this, SLOT(onComboboxEditTextChanged(const QString &)));
QObject::connect(ui.comboBox, SIGNAL(highlighted(int index)), this, SLOT(onComboboxHighlighted(int)));
QObject::connect(ui.comboBox, SIGNAL(highlighted(const QString &)), this, SLOT(onComboboxHighlighted(const QString &)));

Slot Functions:

void ComboboxExample::onComboboxActivated(int index)
{
	ui.label->setText(ui.comboBox->itemText(index));
}

void ComboboxExample::onComboboxActivated(const QString &text)
{
	ui.label->setText(text);
}

void ComboboxExample::onComboboxCurrentIndexChanged(int index)
{
	ui.label->setText(ui.comboBox->itemText(index));
}

void ComboboxExample::onComboboxCurrentIndexChanged(const QString &text)
{
	ui.label->setText(text);
}

void ComboboxExample::onComboboxCurrentTextChanged(const QString &text)
{
	ui.label->setText(text);
}

void ComboboxExample::onComboboxEditTextChanged(const QString &text)
{
	ui.label->setText(text);
}

void ComboboxExample::onComboboxHighlighted(int index)
{
	ui.label->setText(ui.comboBox->itemText(index));
}

void ComboboxExample::onComboboxHighlighted(const QString &text)
{
	ui.label->setText(text);
}

Then it works:

Thanks for your visit. If you find a mistake, or have a question,or want to add more information, leave a comment please.

Leave a Reply

Your email address will not be published. Required fields are marked *