Reputation: 17
How can I connect a QPushButton
and a QComboBox
?
I created a SLOT that accepts 2 parameters, a pointer to the QComboBox
and the index of the selected item:
void modificaExp::eliminaExp(QComboBox *combo,int value)
{
......
combo->removeItem(value);
....
}
the widgest are there:
QComboBox* combo=new QComboBox();
combo->addItem("ciao1");
combo->addItem("ciao44");
combo->addItem("ciao222");
combo->addItem("ciao555");
QPushButton* delButton=new QPushButton();
delButton->setText("delete");
connect(delButton, SIGNAL(clicked()), this, SLOT( eliminaExp(combo,combo->currentIndex() )));
so, when I click on delButton
the element stays there. I think there is a problem in the connect command, specifically I think than the slot is not called.
Upvotes: 0
Views: 782
Reputation: 1038
The slot should have the same type and equal or less number of arguments than the signal
Declare the QComboBox and the QPushButton objects in the header modificaexp.h
private:
QComboBox* combo;
QPushButton* delButton;
modificaexp.cpp
combo=new QComboBox();
combo->addItem("ciao1");
combo->addItem("ciao44");
combo->addItem("ciao222");
combo->addItem("ciao555");
delButton=new QPushButton();
delButton->setText("delete");
connect(delButton, SIGNAL(clicked()), this, SLOT( eliminaExp()));
Modify the slot
void modificaExp::eliminaExp()
{
combo->removeItem(combo->currentIndex());
}
Refer the Qt signal slot documentation
Upvotes: 0
Reputation: 1412
Are you sure you need this slot with two parameter?
Another simple way:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
connect(deleteButton, SIGNAL(clicked(bool)), this, SLOT(deleteSlot()));
}
void MainWindow::deleteSlot()
{
comboBox->removeItem(comboBox->currentIndex());
}
Upvotes: 1