Important: Please read the Qt Code of Conduct -

Qt4 clear() issue : Am I using it right?

  • Hi I am new to qt4 and I'm trying developing a framwork which includes certain topic(labels) to be added to the combo box. Adding topics to a combo box was a success but at ceratin point I need to Refresh the GUI and load the topics to their default state.

    The code itself is called when a the following code is executed:

    @ui_.refresh_topics_push_button->setIcon(QIcon::fromTheme("view-refresh")); connect(ui_.refresh_topics_push_button, SIGNAL(pressed()), this, SLOT(updateTopicList()));@

    Through the function updateTopicList() ui_.topic->clear() is called. But on excecution of this code the control jumps to another function and executes it.

    I also use the following snippet which contains the function which is accedentally executed. (onTopicChanged())
    @connect(ui_.topics_combo_box, SIGNAL(currentIndexChanged(int)), this, SLOT(onTopicChanged(int)));@

    Any help is appreciated

  • Qt Champions 2017

    This is the correct behaviour. It is not accidental. Since combobox list is cleared, index changed. Hence slot topicChanged is called. You can do the following in your slot(onTopicChanged..)

    void onTopicChanged(int a) {
    if ( a== -1) return i.e don't do anything.


  • No changes in the output. I have called the clear() twice in the updateList() is that the reason your solution is not working?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What do you mean by not working ? After calling clear are you repopulating the comboboxes ?

  • Qt Champions 2017

    What is not working for you ?

    1. Is it the issue of combo box not cleared
    2. Is the issue of curentIndex slot is getting called ?

    I have done simple program for you. Check it.

    @ QComboBox box;
    QPushButton buton("Clearme");;

    void MainWindow::topicChanage(int va){
    qDebug() << " Topic changed" << va << endl;
    if (va == -1){
    qDebug() << "List is cleared. Dont do anything" <<endl;


    Now if you give exact issue, we can help you.

  • Dheerendra- your suggestion worked. I had included a blank append in one of the topic @topic.append("") @ . Hence, the index containing the actual topic started when index=1. Now its working perfectly. Thanks again

  • @SGaist- Thanks for the greetings. I got my problem solved

Log in to reply