Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Editable QComboBox with empty edit text: Avoid automatic text change when model is modified
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 7.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wupwup
    wrote on 12 Dec 2011, 12:07 last edited by
    #1

    Hi!
    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?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      broadpeak
      wrote on 12 Dec 2011, 15:31 last edited by
      #2

      Can't help?:
      QComboBox::InsertPolicy(QComboBox::NoInsert);

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wupwup
        wrote on 12 Dec 2011, 20:13 last edited by
        #3

        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!

        1 Reply Last reply
        0
        • E Offline
          E Offline
          EmoBemo
          wrote on 27 May 2013, 10:38 last edited by
          #4

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

          @MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);

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

          m_pModel = new QStandardItemModel(this);
          m_pModel->insertColumn(0);

          ui->comboBox->setModel(m_pModel);

          ui->comboBox->setCurrentIndex(-1);
          }

          MainWindow::~MainWindow()
          {
          delete ui;
          }

          void MainWindow::on_pushButton_clicked()
          {
          int nRows = m_pModel->rowCount();
          m_pModel->insertRow(nRows);

          m_pModel->setData(m_pModel->index(nRows, 0), QString("%1").arg(qrand()0));
          }

          void MainWindow::on_pushButton_2_clicked()
          {
          ui->comboBox->setCurrentIndex(-1);
          }

          void MainWindow::displayCurrentIndex(int n)
          {
          ui->label->setText(QString::number(n));
          }@

          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?

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved