Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML Combobox not updating with dataChanged signal
Forum Updated to NodeBB v4.3 + New Features

QML Combobox not updating with dataChanged signal

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 5.2k Views
  • 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.
  • A Offline
    A Offline
    Andreas.S
    wrote on 28 Jan 2016, 15:44 last edited by Andreas.S
    #1

    Hi!

    I have a QML Combobox that does not update the current selected value when the model value is changed by another QML component.
    However, if I open the Combobox, I see the element with the updated value. Selecting the same element will not change the current selected to the correct value, but selecting another value, and then jumping back updates the current selected value correctly.

    The signal dataChanged is emitted each time the model data is changed.

    Is there another signal ComboBox listens to when it comes to updating the current selected value?

    I'm using Qt5.3, QtQuick 2.3 and QtQuick.Controls 1.2

    QML Combobox code:

     ComboBox {
            id: myComboBox
            height: 20
            width: 300
            textRole: "nameRole"
    
            model: myModel
        }
    

    myModel is defined in c++ like this:

    class MyModel: public QAbstractListModel
    
    

    Where the setData function is implemented like:

    bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if (!index.isValid() || index.row() >= dataList_.count())
            return false;
    
        switch(role) {
        case NameRole:
            if(dataList_.at(index.row())->->name() != value.toString())
            {
                dataList_.at(index.row())->setName(value.toString());
                emit dataChanged(index, index);
            }
    
    

    and roleNames:

    QHash<int, QByteArray> MyModel::roleNames() const
    {
        QHash<int, QByteArray> roles;
        roles[NameRole] = "nameRole";
    
    
    A 1 Reply Last reply 29 Jan 2016, 08:45
    1
    • A Andreas.S
      28 Jan 2016, 15:44

      Hi!

      I have a QML Combobox that does not update the current selected value when the model value is changed by another QML component.
      However, if I open the Combobox, I see the element with the updated value. Selecting the same element will not change the current selected to the correct value, but selecting another value, and then jumping back updates the current selected value correctly.

      The signal dataChanged is emitted each time the model data is changed.

      Is there another signal ComboBox listens to when it comes to updating the current selected value?

      I'm using Qt5.3, QtQuick 2.3 and QtQuick.Controls 1.2

      QML Combobox code:

       ComboBox {
              id: myComboBox
              height: 20
              width: 300
              textRole: "nameRole"
      
              model: myModel
          }
      

      myModel is defined in c++ like this:

      class MyModel: public QAbstractListModel
      
      

      Where the setData function is implemented like:

      bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role)
      {
          if (!index.isValid() || index.row() >= dataList_.count())
              return false;
      
          switch(role) {
          case NameRole:
              if(dataList_.at(index.row())->->name() != value.toString())
              {
                  dataList_.at(index.row())->setName(value.toString());
                  emit dataChanged(index, index);
              }
      
      

      and roleNames:

      QHash<int, QByteArray> MyModel::roleNames() const
      {
          QHash<int, QByteArray> roles;
          roles[NameRole] = "nameRole";
      
      
      A Offline
      A Offline
      Andreas.S
      wrote on 29 Jan 2016, 08:45 last edited by Andreas.S
      #2

      @Andreas.S

      Reproduced problem with QStringListModel. When changing element in model, ComboBox is not updated correctly.

      int main(int argc, char** argv)
      {
          QApplication app(argc, argv);
      
          QQuickView view;
      
          QStringListModel nameModel(QStringList() << "One" << "Two" << "Three");
      
          view.rootContext()->setContextProperty("_nameModel", &nameModel);
      
          QString loc = "qrc:///main.qml";
          view.setSource(QUrl(loc));
      
          view.show();
      
      
          return app.exec();
      }
      

      And QML:

      import QtQuick 2.3
      import QtQuick.Controls 1.2
      
      Item {
          id: root
          width: 560
          height: 360
      
          ComboBox {
              id: comboBox
              model: _nameModel
              textRole: "edit"
          }
      
          Component
          {
              id: editNameDelegate
      
              Rectangle
              {
                  id: editNameBox
                  visible: comboBox.currentIndex===index ? true: false
                  color: "transparent"
      
                  Rectangle {
                      id: changeNameBox
                      border.color: "black"
      
                      height: 30
                      width: 360
      
                      anchors.top: parent.top
                      anchors.topMargin: 2
                      anchors.left: parent.left
                      Text
                      {
                          text: "Change Name"
                          font.pixelSize: 18
                          anchors.left: parent.left
                          anchors.leftMargin: 8
                          anchors.verticalCenter: parent.verticalCenter
                      }
                      Rectangle
                      {
                          border.color: "black"
                          height: 25
                          width: 200
                          anchors.right: parent.right
                          anchors.rightMargin: 5
                          anchors.verticalCenter: parent.verticalCenter
      
                          TextInput
                          {
                              anchors.fill: parent
                              anchors.leftMargin: 3
                              id: changeNameInput
                              text: model.edit
                              Keys.onReturnPressed: {
                                  model.edit = text
                                  Qt.inputMethod.hide();
                              }
                          }
                      }
                  }
              }
          }
          ListView {
              anchors.top: comboBox.bottom
              height: 30
              width: 500
              id: editNameAList
              model: _nameModel
              delegate: editNameDelegate
              interactive: false
          }
      }
      
      1 Reply Last reply
      0
      • A Offline
        A Offline
        Andreas.S
        wrote on 1 Feb 2016, 08:15 last edited by
        #3

        Found a workaround:
        Connect a slot that updates the model completely using either beginInsertRows or ResetModel. This should be called each time data that the combobox displays is changed.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on 5 Apr 2024, 06:53 last edited by
          #4
          This post is deleted!
          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