PDA

View Full Version : QStringListModel doesn't update when QStringList is updated



nymar
7th March 2013, 12:08
Hi,
I would like help about these points
1/ I want to use the append in my method so i can add "Belgique" for ex. to my list but when i do like this nothing happens when i push the button Add....this my code and window 8800

#include"scheduletable.h"

#include "ui_scheduletable.h"
#include<QStringListModel>
ScheduleTable::ScheduleTable(QWidget*parent):
QWidget(parent),
ui(newUi::ScheduleTable)
{
ui->setupUi(this);
QStringListlistePays;
listePays<<"France";
QStringListModel*modele=newQStringListModel(listeP ays);
ui->listView->setModel(modele);
connect(ui->addFrame,SIGNAL(clicked()),this,SLOT(add_Frame())) ;
}
voidScheduleTable::add_Frame()
{
listePays.append("Belgique");

}
ScheduleTable::~ScheduleTable()
{
deleteui;
}

alizadeh91
7th March 2013, 12:33
you can use model->setData() method

Or you can use model->setStringList(listePays) after listePays.append() method

wysota
7th March 2013, 12:39
QStringListModel stores a copy of the string list you pass it. Thus if you add a new entry to the original string list, the model does not get updated. Since QStringListModel is read-only, the only solution is to set a new string list on it again but then you'll lose things such as selection and scrolling in the view because the model will be reset. A more proper solution would be to use QStandardItemModel and add a new entry directly to the model with QStandardItemModel::appendRow().

nymar
7th March 2013, 16:20
Thx for helping guyz the solution is

void ScheduleTable::add_Frame()
{
listePays.append("Belgique");
modele->insertRows(0, 1);
modele->setData(modele->index(0), "Belgique");
}


Added after 59 minutes:

but i have a question how can i put the new item at the end of the list ? i couldn't use currentIndex()

alizadeh91
7th March 2013, 17:20
Plz see wysota post. For understanding concept of model/view in Qt see this: http://qt-project.org/doc/qt-4.8/model-view-programming.html -> this is very helpful and complete