ale666
ale666

Reputation: 17

connect QPushButton and QComboBox

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

Answers (2)

techneaz
techneaz

Reputation: 1038

  1. The slot should have the same type and equal or less number of arguments than the signal

  2. Declare the QComboBox and the QPushButton objects in the header modificaexp.h

     private:
     QComboBox* combo;
     QPushButton* delButton;
    
  3. 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()));
    
  4. Modify the slot

     void modificaExp::eliminaExp()
     {           
        combo->removeItem(combo->currentIndex());        
     }        
    
  5. Refer the Qt signal slot documentation

Upvotes: 0

t3ft3l--i
t3ft3l--i

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

Related Questions