Important: Please read the Qt Code of Conduct -

[Solved] Signals & slots, connect menu from QComboBox to slot

  • Hello folks, I created a small menu with QComboBox to select, between 3 variables.

    My question is, How can make the connection between the menus and a respective slot? (i did try but I don't get)

    any help will wellcomed
    Thanks in advance.

    /#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QToolButton>
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
            QComboBox *comboBox = new QComboBox(this);
            QComboBox *iconComboBox = new QComboBox;
            iconComboBox->addItem(QIcon(":/icons/engranaje.png"), tr("Automatic"));
            iconComboBox->addItem(QIcon(":/icons/s_mano.png"), tr("Manual"));
            iconComboBox->addItem(QIcon(":/icons/s_off.png"), tr("Off"));
            connect(comboBox->tr(),SIGNAL(triggered()),this,SLOT(slotTest())); //<-- how can i make this?
    void MainWindow::slotTest(){

  • The QComboBox provides signals such as currentIndexChanged() and currentTextChanged() that you can use for this purpose. The parameters passed are either the index of the element in the combobox or the item text.

    You would do something like:

    connect(comboBox, SIGNAL(currentTextChanged(QString)), this, SLOT(comboboxItemChanged(QString)));

    I hope that helps.

  • Thank you very much,
    It has worked!!!!

Log in to reply