Results 1 to 5 of 5

Thread: About QComboBox::currentIndexChanged

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default About QComboBox::currentIndexChanged

    Hi all,
    I use QStandardItemModel/QTableView/QItemDelegate in my app.
    QComboBox is one of the columns and it is made shown for all the time using QTableView:penPersistentEditor.

    i need:
    1. when user clicks the QCombox and change the current index, i notify the new change to the model while the current index stays unchaged until the model is updated.
    2. i want to get notified as soon as user changes the current index. while the problem is QItemDelegate::setModelData is never called until the QComboBox is focused out.

    i tried this:
    void
    MyItemDelegate::createEditor(..)
    {
    QComboBox * comboEditor = new QComboBox(parent);

    comboEditor->addItem("1");
    comboEditor->addItem("2");

    connect(comboEditor, SIGNAL(currentIndexChanged(int)), this, SLOT(emitDataCommit(int)));

    return comboEditor;
    }

    void
    MyItemDelegate::emitDataCommit(int)
    {
    emit commitData(qobject_cast<QWidget *>(sender()));
    }

    void
    MyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
    QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
    Q_ASSERT(comboBox);

    QString newText = comboBox->currentText();

    /// set it back to the previous until the model is updated using the new data.
    /// and the current index is automatically updated by the model when it is finished updating.
    if (newText == "1")
    {
    comboBox->setCurrentText("2");
    }
    else if (newText == "2")
    {
    comboBox->setCurrentText("1");
    }

    model->setData(index, newText);
    }


    BUT when the model accepts the new data and call setData() to update itself, the signal comboEditor::currentIndexChanged(int) is emitted for the second time by the model and MyItemDelegate::setModelData is called again.....

    is there anyway to prevent the model from emiting the signal or is there anyway to distinguish the two call to MyItemDelegate::setModelData?

  2. The following user says thank you to aresa for this useful post:


Similar Threads

  1. QComboBox BUG?
    By mak_user in forum Qt Programming
    Replies: 15
    Last Post: 18th September 2020, 07:00
  2. Help with QComboBox
    By the9gamer in forum Newbie
    Replies: 3
    Last Post: 8th August 2012, 07:36
  3. Set value to QComboBox
    By Archa4 in forum Newbie
    Replies: 2
    Last Post: 19th May 2011, 12:46
  4. Qcombobox
    By Project25 in forum Qt Programming
    Replies: 2
    Last Post: 16th December 2009, 19:29
  5. QComboBox
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 31st August 2007, 13:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.