December 12, 2011

wupwup wupwup
Lab Rat
1 posts

Editable QComboBox with empty edit text: Avoid automatic text change when model is modified


I’m trying to use the same model for multiple combo boxes. The user may select an existing item from the model, or enter a new one which will then be added to the model, thus being added to all combo boxes using that model.
The user may also leave the text field empty, and that seems to cause two problems:

1. When assigning an existing (non-empty) model to a new combo box, the edit text of that box is automatically filled with the first item of the model, instead of just remaining empty. While that’s a minor problem I can work around by reinitializing the edit text after setting the model, the second problem is causing me severe headaches:

2. Having assigned the same model to multiple combo boxes, empty edit texts of one box get automatically filled with model content when all items are removed and then a new item is added to the model within another combo box.

Is there any way to totally disable this auto-fill behaviour for a an empty line edit of an editable combo box?

3 replies

December 12, 2011

broadpeak broadpeak
Lab Rat
309 posts

Can’t help?:

December 12, 2011

wupwup wupwup
Lab Rat
1 posts

InsertPolicy is already set, but that’s for the “opposite direction” anyway.
Never mind, the combo box seemed too clumsy for the task after all. I’m now using a line edit and a separate button opening a selection dialog with further editing features.

Thanks anyway!

May 27, 2013

EmoBemo EmoBemo
Lab Rat
7 posts

The same issue also bugs me. Here is a sample code that reproduces the issue:

  1. MainWindow::MainWindow(QWidget *parent) :
  2.     QMainWindow(parent),
  3.     ui(new Ui::MainWindow)
  4. {
  5.  ui->setupUi(this);
  7.  connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(displayCurrentIndex(int)));
  9.  m_pModel = new QStandardItemModel(this);
  10.  m_pModel->insertColumn(0);
  12.  ui->comboBox->setModel(m_pModel);
  14.  ui->comboBox->setCurrentIndex(-1);
  15. }
  17. MainWindow::~MainWindow()
  18. {
  19.     delete ui;
  20. }
  22. void MainWindow::on_pushButton_clicked()
  23. {
  24.  int nRows = m_pModel->rowCount();
  25.  m_pModel->insertRow(nRows);
  27.  m_pModel->setData(m_pModel->index(nRows, 0), QString("%1").arg(qrand()0));
  28. }
  30. void MainWindow::on_pushButton_2_clicked()
  31. {
  32.  ui->comboBox->setCurrentIndex(-1);
  33. }
  35. void MainWindow::displayCurrentIndex(int n)
  36. {
  37.  ui->label->setText(QString::number(n));
  38. }

The first time the pushButton is clicked the first row of m_pModel is inserted. That automatically changes the current index of the combo box to 0. Why? On every other pushButton press the index does not change. Only on the first click. Is this a bug? Is there any workaround?

  ‹‹ error while loading QtNetwork4.dll      QSystemTrayIcon and Context menu ››

You must log in to post a reply. Not a member yet? Register here!