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. Signal for QStringList count changed?

Signal for QStringList count changed?

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 1.6k Views 4 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.
  • L Offline
    L Offline
    lansing
    wrote on last edited by
    #1

    I have a QStringList storing a bunch of names and QcomboBox to populate the list. I'll be adding new QString to the list and at the end of process, I want the QcomboBox to update with the list. But I couldn't find a signal for count change.

    connect(m_stringlist, &QStringList::countChanged??, this, &slotHandleCountChange );
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      QStringList has zero signals.
      Maybe you could use
      https://doc.qt.io/qt-5/qstringlistmodel.html

      Or simply define a new signal in the holding class and emit that to make combobox do what its needs to do.

      L 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        QStringList has zero signals.
        Maybe you could use
        https://doc.qt.io/qt-5/qstringlistmodel.html

        Or simply define a new signal in the holding class and emit that to make combobox do what its needs to do.

        L Offline
        L Offline
        lansing
        wrote on last edited by
        #3

        @mrjj

        Hi what would be the signal if I'm to use QStringListModel?

        mrjjM 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          No signal needed, update the content of the model and the changes will be propagated to the view.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • L lansing

            @mrjj

            Hi what would be the signal if I'm to use QStringListModel?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @lansing
            Hi
            Actually it's then a real model
            and would signal the comboBox by itself if
            the model is changed.
            So you would just change the model and it would be auto-updated.

            Heh. one sec too late :)

            1 Reply Last reply
            2
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              Just a mini sample since we work directly with a model

               // make the model
                  QStringListModel *mo = new QStringListModel;
                  // stuff something in it
                  mo->setStringList( QStringList() << "a" << "b" );
                  // assign the model to the combobox
                  ui->comboBox->setModel(mo);
                  // use helper function to add string to the end of the model
                  AddString(mo,"hi");
                  AddString(mo,"there");
                  AddString(mo,"im a model");
              ...
              
              void AddString(QAbstractItemModel *model, QString newString )
              {
                  if(model->insertRow(model->rowCount())) {
                         QModelIndex index = model->index(model->rowCount() - 1, 0);
                         model->setData(index, newString);
                     }
              }
              
              

              alt text

              L 1 Reply Last reply
              1
              • mrjjM mrjj

                Hi
                Just a mini sample since we work directly with a model

                 // make the model
                    QStringListModel *mo = new QStringListModel;
                    // stuff something in it
                    mo->setStringList( QStringList() << "a" << "b" );
                    // assign the model to the combobox
                    ui->comboBox->setModel(mo);
                    // use helper function to add string to the end of the model
                    AddString(mo,"hi");
                    AddString(mo,"there");
                    AddString(mo,"im a model");
                ...
                
                void AddString(QAbstractItemModel *model, QString newString )
                {
                    if(model->insertRow(model->rowCount())) {
                           QModelIndex index = model->index(model->rowCount() - 1, 0);
                           model->setData(index, newString);
                       }
                }
                
                

                alt text

                L Offline
                L Offline
                lansing
                wrote on last edited by
                #7

                @mrjj said in Signal for QStringList count changed?:

                void AddString(QAbstractItemModel *model, QString newString )

                Wow looks like it would be better for me to create a subclass of qstringlistmodel to put all these in because I'm probably going to need more functions for this such as removeString.

                mrjjM 1 Reply Last reply
                0
                • L lansing

                  @mrjj said in Signal for QStringList count changed?:

                  void AddString(QAbstractItemModel *model, QString newString )

                  Wow looks like it would be better for me to create a subclass of qstringlistmodel to put all these in because I'm probably going to need more functions for this such as removeString.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @lansing
                  yes thats not bad idea to wrap in a class to keep it contained.

                  L 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @lansing
                    yes thats not bad idea to wrap in a class to keep it contained.

                    L Offline
                    L Offline
                    lansing
                    wrote on last edited by
                    #9

                    @mrjj

                    I encountered a problem when creating the subclass. When I created a new item model class in Qt Creator, it asks me to reimplement all the basic functions...

                    mrjjM 1 Reply Last reply
                    0
                    • L lansing

                      @mrjj

                      I encountered a problem when creating the subclass. When I created a new item model class in Qt Creator, it asks me to reimplement all the basic functions...

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @lansing
                      Hi
                      In what way, don't you inherit qstringlistmodel ?

                      L 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @lansing
                        Hi
                        In what way, don't you inherit qstringlistmodel ?

                        L Offline
                        L Offline
                        lansing
                        wrote on last edited by
                        #11

                        @mrjj

                        Oh I was creating the class with the default one from the wizard, which was inheriting the abstactitemmodel. Manually declaring the classs solved it.

                        void GenericStringListModel::append(QString &a_string)
                        {
                            if (insertRow(rowCount())) {
                                QModelIndex index = this->index(rowCount() - 1, 0);
                                setData(index, a_string);
                            }
                        }
                        

                        I'm not sure if using this is correct because it's giving me error without it.

                        And is this correct for remove string from the list model?

                        void GenericStringListModel::removeOne(QString &a_string)
                        {
                            int index = stringList().indexOf(a_string);
                            removeRow(index);
                        }
                        
                        mrjjM JKSHJ 2 Replies Last reply
                        0
                        • L lansing

                          @mrjj

                          Oh I was creating the class with the default one from the wizard, which was inheriting the abstactitemmodel. Manually declaring the classs solved it.

                          void GenericStringListModel::append(QString &a_string)
                          {
                              if (insertRow(rowCount())) {
                                  QModelIndex index = this->index(rowCount() - 1, 0);
                                  setData(index, a_string);
                              }
                          }
                          

                          I'm not sure if using this is correct because it's giving me error without it.

                          And is this correct for remove string from the list model?

                          void GenericStringListModel::removeOne(QString &a_string)
                          {
                              int index = stringList().indexOf(a_string);
                              removeRow(index);
                          }
                          
                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #12

                          @lansing
                          hi
                          Hmm i wonder what it saw for index without "this". however if
                          it works with it. then no harm.

                          Looks good. The removeRow should emit the right signals.

                          1 Reply Last reply
                          0
                          • L lansing

                            @mrjj

                            Oh I was creating the class with the default one from the wizard, which was inheriting the abstactitemmodel. Manually declaring the classs solved it.

                            void GenericStringListModel::append(QString &a_string)
                            {
                                if (insertRow(rowCount())) {
                                    QModelIndex index = this->index(rowCount() - 1, 0);
                                    setData(index, a_string);
                                }
                            }
                            

                            I'm not sure if using this is correct because it's giving me error without it.

                            And is this correct for remove string from the list model?

                            void GenericStringListModel::removeOne(QString &a_string)
                            {
                                int index = stringList().indexOf(a_string);
                                removeRow(index);
                            }
                            
                            JKSHJ Offline
                            JKSHJ Offline
                            JKSH
                            Moderators
                            wrote on last edited by
                            #13

                            @lansing said in Signal for QStringList count changed?:

                                QModelIndex index = this->index(rowCount() - 1, 0);
                            

                            ...

                            I'm not sure if using this is correct because it's giving me error without it.

                            You defined a new local variable called index. That conflicts with name of the function index(). That's why you need this to tell the compiler to use the function instead of the variable.

                            If you gave your variable a different name, then you don't need this.

                            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                            mrjjM 1 Reply Last reply
                            5
                            • JKSHJ JKSH

                              @lansing said in Signal for QStringList count changed?:

                                  QModelIndex index = this->index(rowCount() - 1, 0);
                              

                              ...

                              I'm not sure if using this is correct because it's giving me error without it.

                              You defined a new local variable called index. That conflicts with name of the function index(). That's why you need this to tell the compiler to use the function instead of the variable.

                              If you gave your variable a different name, then you don't need this.

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @JKSH
                              doh. Good catch :)

                              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