Results 1 to 5 of 5

Thread: About QComboBox::currentIndexChanged

  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:


  3. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: About QComboBox::currentIndexChanged

    You could try blocking the combobox signals in setModelData until you've returned from model->setData()

    See QObject::blockSignals()

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:


  5. #3
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: About QComboBox::currentIndexChanged

    Quote Originally Posted by anda_skoa View Post
    You could try blocking the combobox signals in setModelData until you've returned from model->setData()

    See QObject::blockSignals()

    Cheers,
    _
    Thank you, but i override setData in QStandardItemModel, and it
    Only send a command to the server via the web and returns afterwards.
    So it is not until the server notifies me of the result that i call
    Qstandarditemmodel::setData to update the model.

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


  7. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: About QComboBox::currentIndexChanged

    Ah, I see. Since you were using QStandardItemModel I assumed it had all data itself. Something like that is usually done via a custom model.

    Anyway, block the signal when "resetting" the comboBox. Right now you commit twice, once when the user changes the value and once when your "reset".
    And maybe when setEditorData() changes the content. After all there is no point in notifying the model about data it provided itself.

    Cheers,
    _

  8. The following 2 users say thank you to anda_skoa for this useful post:

    aresa (4th December 2013)

  9. #5
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: About QComboBox::currentIndexChanged

    I googled for hours and found a nice signal called 'activated' ,
    This signal is only emitted when the current index is set by user click.
    Not pragmatically. So by connecting this with 'emitDataChanged' the second
    Call to QItemDelegate::setModelData is effectively avoided.
    This is almost what i want except that the 'actived' signal is also
    Emitted even if the current index is not changed by user click.
    Anyway this is easy to handle.

    Thanks for your replies, happy programming with you.

  10. 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: 14
    Last Post: 31st October 2015, 18:20
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.