Update a Combobox after opening Qdialog

Asked

Viewed 63 times

0

How do I update a Combobox after opening a Qdialog?

The code below does not update if you have an update in db. I have to close the application and open to show the new data:

Dialog::Dialog(){
  carregar_comboBox();
}

void Dialog::carrega_comboBox(){
    qry..
     while..
     ui->comboBox->addItem(qry.value(0).toString());
} 
  • You must then call the carrega_comboBox() whenever you change something in DB. Or use a model for compobox.

  • @William Bernal has some way of calling the carregar_comboBox() when I open the QDialog? It’s better than binding in other class.

  • Implement your function show() { carregar_comboBox(); QDialog::show(); }.

  • Thanks. I got it working :)

  • @Guilhermebernal gives an answer so we can vote.

1 answer

1


From what I understand you need the function carregar_comboBox() always be called before the dialog is shown on the screen. A simple solution is to re-implement the function show() overwriting the QDialog::show():

class SeuDialog : public QDialog {

    \* ... *\

public:

     // Sempre que alguém chamar essa função para mostrar a dialog...
     void show() {
         carregar_comboBox(); // Primeiro carregue sua ComboBox
         QDialog::show();     // Somente depois mostre de fato a dialog
     }
}

It would also be interesting to implement showMaximized() similarly so that the behavior is consistent.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.