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?
Qt 6.11 is out! See what's new in the release blog

Signal for QStringList count changed?

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 2.5k 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 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