How to add items to listView?
-
I am trying to make a chat application wherein I want user to type in a
textEdit
box and when someone pressessend
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 standardtextEdit
so that chat box can update new messages in new line.Thank you for your attention.
-
@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
:) -
-
@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.
-
@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); -
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
-
@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 athttp://doc.qt.io/qt-5/qstandarditemmodel.html
and
http://doc.qt.io/qt-5/qstandarditemmodel.html#appendRowjust for notes:
http://doc.qt.io/qt-5/qtnetwork-network-chat-example.html