Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a function when item in combobox changed?

Tags:

combobox

qt

qt4

connect(ui->ComboBox,SIGNAL(currentIndexChanged()),this,SLOT(switchcall()));

in qt, combobox items i have none,server,client.when i select one of this it should call switchcall function.in this function i want to perform task depending upon choice in combobox.how to do it??

like image 801
Amar Avatar asked Dec 13 '12 08:12

Amar


3 Answers

You haven't put the args in the SIGNAL/SLOT statements.

connect(ui->ComboBox,SIGNAL(currentIndexChanged(const QString&)),
        this,SLOT(switchcall(const QString&)));

Alternatively you can use the item index, using the overloaded signal.

like image 150
cmannett85 Avatar answered Nov 14 '22 17:11

cmannett85


Use autoconnect:

void on_ComboBox_currentIndexChanged(int index);

Autoconnect template:

on_<control_name>_<signal_name>(<signal params>)
like image 29
Nikolay Avatar answered Nov 14 '22 16:11

Nikolay


To get the index from QComboBox change event of QComboBox item use:

connect(ui->comboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(indexChanged(int)));

in mainwindow.h:

private slots:
    void indexChanged(int index);

in mainwindow.cpp:

void MainWindow::indexChanged(int index)
{
    // Do something here on ComboBox index change
}
like image 5
FutureJJ Avatar answered Nov 14 '22 17:11

FutureJJ