0

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.

m.s.
  • 16,063
  • 7
  • 53
  • 88
ale666
  • 17
  • 1
  • `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 Jul 23 '15 at 11:58
  • thanks, you're right!! you save my project :D – ale666 Jul 23 '15 at 12:05

2 Answers2

1

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());
}
t3ft3l--i
  • 1,372
  • 1
  • 14
  • 21
0
  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

techneaz
  • 998
  • 6
  • 9