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. How to add items to listView?
Forum Updated to NodeBB v4.3 + New Features

How to add items to listView?

Scheduled Pinned Locked Moved Solved General and Desktop
qt5listviewqlistview
7 Posts 2 Posters 21.8k Views 1 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.
  • S Offline
    S Offline
    Shawny
    wrote on 29 Mar 2016, 13:13 last edited by
    #1

    I am trying to make a chat application wherein I want user to type in a textEdit box and when someone presses send the text should be retrieved and displayed in listView. So far I am only able to write this much code as shown below which should get invoked when user presses send button:

    void MainWindow::on_pushButton_clicked()
    {
    
        model = new QStringListModel(this);
        QStringList list;
        QString text = ui->textEdit->toPlainText();
        list << text;
        model->setStringList(list);
        ui->listView->setModel(model);
    
    
    }
    
    

    But it shows below mentioned errors:

    /home/neo/Qt/chatty/mainwindow.cpp:-1: In member function 'void MainWindow::on_pushButton_clicked()':
    /home/neo/Qt/chatty/mainwindow.cpp:22: error: 'model' was not declared in this scope
         model = new QStringListModel(this);
         ^
    /home/neo/Qt/chatty/mainwindow.cpp:22: error: expected type-specifier before 'QStringListModel'
         model = new QStringListModel(this);
                     ^
    /home/neo/Qt/chatty/mainwindow.cpp:22: error: expected ';' before 'QStringListModel'
    

    Can anybody suggest me cleaner way to how I can add items in listView of Qt?

    P.S. Since I am making a chat application I thought to used listView instead of standard textEdit so that chat box can update new messages in new line.

    Thank you for your attention.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 29 Mar 2016, 13:46 last edited by
      #2

      @Shawny said:
      Hi
      lets see

      • /home/neo/Qt/chatty/mainwindow.cpp:22: error: 'model' was not declared in this scope
        model = new QStringListModel(this);
        It dont know "model" . where did u define this?
        unless you included
        QStringListModel *model; in the mainwindow class , then
        the code is trying to assign a new QStringListModel to unknown variable "model".

      • /home/neo/Qt/chatty/mainwindow.cpp:22: error: expected type-specifier before 'QStringListModel'
        Did u include "QStringListModel" ?

      Also, you should really read
      http://doc.qt.io/qt-5/model-view-programming.html
      :)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Shawny
        wrote on 29 Mar 2016, 13:54 last edited by Shawny
        #3

        @mrjj Thank you for your inputs. That was too silly of me to forget adding the include. Thanks for reminding me.

        But now the error that is remaining is 'model' was not declared in this scope. I have declared model in same function then why am I getting this error? I am also going through the documentation that you linked here.

        M 1 Reply Last reply 29 Mar 2016, 14:04
        0
        • S Shawny
          29 Mar 2016, 13:54

          @mrjj Thank you for your inputs. That was too silly of me to forget adding the include. Thanks for reminding me.

          But now the error that is remaining is 'model' was not declared in this scope. I have declared model in same function then why am I getting this error? I am also going through the documentation that you linked here.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 29 Mar 2016, 14:04 last edited by mrjj
          #4

          @Shawny
          well no worries. i sometimes also forget the includes :)

          you dont declare it there, you "just" new it
          model = new QStringListModel(this);

          if you also declared it
          QStringListModel* model = new QStringListModel(this);

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Shawny
            wrote on 29 Mar 2016, 14:09 last edited by
            #5

            Sorry for this noobish mistake. I was trying to declare it before and then using model. Its working perfectly now.

            The only problem is my listView is not updating new sent messages on different row. I think i will also need to increment row as i send text in text area.

            But yes my main problem of updating listView is solved.

            thank you very much @mrjj

            M 1 Reply Last reply 29 Mar 2016, 14:16
            0
            • S Shawny
              29 Mar 2016, 14:09

              Sorry for this noobish mistake. I was trying to declare it before and then using model. Its working perfectly now.

              The only problem is my listView is not updating new sent messages on different row. I think i will also need to increment row as i send text in text area.

              But yes my main problem of updating listView is solved.

              thank you very much @mrjj

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 29 Mar 2016, 14:16 last edited by mrjj
              #6

              @Shawny
              You are welcome :)
              no worries. its always easier for others to spot what is wrong than the person that wrote the code.
              Even for very experienced programmers.

              The QStringListModel makes copy of the list u give it. so to update you would append to the same stringlist and set it again.
              this however might not always be as wanted so if u feel u need more, please look at

              http://doc.qt.io/qt-5/qstandarditemmodel.html
              and
              http://doc.qt.io/qt-5/qstandarditemmodel.html#appendRow

              just for notes:
              http://doc.qt.io/qt-5/qtnetwork-network-chat-example.html

              1 Reply Last reply
              2
              • S Offline
                S Offline
                Shawny
                wrote on 29 Mar 2016, 14:45 last edited by
                #7

                Wow. These are some useful references. Thanks a lot. I mark this thread solved.

                1 Reply Last reply
                0

                1/7

                29 Mar 2016, 13:13

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved