0
votes

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.

2
connect does not work this way. If you're using Qt4, you'll have to pass combobox to a slot somehow (make it a class member, or via setProperty). If you're using Qt5, you can switch to a 'new' version of connect and write a lambda. - Amartel
thanks, you're right!! you save my project :D - ale666

2 Answers

1
votes

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());
}
0
votes
  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