Important: Please read the Qt Code of Conduct -

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

  • I have taken a combo Box for zooming the image on clicked and combobox have 4 parameter (100%,200%,400%,800%). So, I want to change the image size on click of item inside the combobox.
    I am try this signal and slot code:-

     connect(ui->mcomboBox, SIGNAL(activated(int)), this, SLOT(Zoom100(int)));

    this one working but any item I select item it will forward to same function

    I used another signal and slot but it will also not work

     connect(this->ui->mcomboBox, SIGNAL(currentIndexChanged(const Qstring&)),
    	this, SLOT(zoom100(const Qstring&)));
    void mainWindow::Zoom100(int index)
    void mainWindow::Zoom200(int index)
    void mainWindow::Zoom400(int index)
    void mainWindow::Zoom800(int index)

    I am trying to get every item index value.
    Thank you in advance.

  • You're using the text in the items of the combo box to zoom in or zoom out, so what you need is not the index rather than the text.

    You've to connect the changed text and create a slot that gets a QString. Here there is an example, not elegant but can help you:

    connect(ui->mcomboBox, SIGNAL(currentTextChanged(QString)), this, SLOT(Zoom(QString)));
    void mainWindow::Zoom(const QString &text)
        int theZoomValue = text.split("%").constFirst().toInt();

    If what you want is indeed the index of the item selected in the combobox, you need the following connect:

    connect(ui->mcomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(Zoom(int)));

    Remember that the types of the params must match.

  • @francescmm Can i get the index of every item on click. If i will get the index then i will used switch case to solve this problem
    I will try this one but its not working

     connect(ui->mcomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(Zoom(int)));

  • @amarism I've just edited the topic for that! I misunderstood your question =)

    You also can user the signal QComboBox::activated(int)

  • I know it sounds silly, but did you remember to declare the method "void Zoom(int value)" in the public/private slot part of the header file?

    Can you paste your whole code so I can see what's happening easily?

  • @francescmm yup i will declare the function in .h file

    private slots:
            void Zoom100(int index);

  • Do it this way!

    // Remember the word 'slots'
    private slots:
       void Zoom100 (int index);

  • @francescmm yes i will taken a slot inside the private part

  • @amarism said in How can I call a particular function when item in combobox changed?:

    connect(ui->mcomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(Zoom(int)));

    now its working thanx

Log in to reply