QListView not showing previously inserted item in a model
-
Hello,
I am using a text box to insert commands into a file which also get appended to my model. I then want the contents of the file (which contains the contents of the model as well) to appear in a QListView as I insert each line. What I am noticing is that every time I click the button to insert a new line I typed, that line does not show up in the QListView until I insert the next line. And that keeps happening for every line. So for example, the first line I ever enter leaves the ListView blank until I enter the next line of text and click my input button. Does anyone know why this may be? I entered the code below:
@void mdiWidget::on_inputPushButton_clicked()
{
mdiTableModel->clear();QString mdicmd; if(!(mdiFile.open(QIODevice::ReadWrite))) { generalStatusMessage = "Error opening MDI file!"; emit updateGeneralStatusSignal(); } mdicmd = ui->mdiLineEdit->text(); QTextStream textStream(&mdiFile); if((!mdicmd.isEmpty()) & (mdicmd.at(0).toUpper().toAscii() == 'G' || mdicmd.at(0).toUpper().toAscii() == 'M' || mdicmd.at(0).toUpper().toAscii() == 'O' || mdicmd.at(0).toUpper().toAscii() == 'X' || mdicmd.at(0).toUpper().toAscii() == 'Y' || mdicmd.at(0).toUpper().toAscii() == 'Z')) { textStream << mdicmd + "\n"; while(!textStream.atEnd()) { QString line = textStream.readLine(); line = line.toUpper(); QList<QStandardItem *> items; QStringList fields = line.split("\n"); foreach (QString text, fields) {items.append(new QStandardItem((QString)text));} mdiTableModel->appendRow(items); } } else { generalStatusMessage = "Invalid MDI command!"; emit updateGeneralStatusSignal(); } ui->mdiLineEdit->clear(); ui->mdiLineEdit->setFocus(); mdiFile.close(); ui->mdiListView->setModel(mdiTableModel); ui->mdiListView->show(); ui->inputPushButton->setEnabled(false); ui->deletePushButton->setEnabled(false);
}@
Thanks!
-
Hi,
Didn't read your entire code in details, but I see some possible problems and crashes.
First of all, when new data is inserted in your model you need to notify the view about it. This is done via startInsertRows or something like it.
That tells the View not to read current data from the model.
In the button clicked you do not need to "reset" the model to the view! This only needs to be done ONES!!
The standard practice is:
@
on_button_clicked
{
if (newData)
{
beginInsertRows();
// Add stuff to your model safely here
endInsertRows();
// send signal to View to reload data and redraw!
update(); // or maybe reset() I forgotten that ;-)
}
}
@ -
Thanks, but I don't fully understand the beginInsertRows()/endInsertRows() portions. I have never done that. I always assumed that
@ui->mdiListView->setModel(mdiTableModel);@
was where I notified the View to be updated with the model...I am not sure how to go about appending a row using the two functions you provided. It seems you have to specify the range of rows to insert. Also, would "item" be the first parameter?
[quote author="Jeroentje@home" date="1382017316"]Hi,
Didn't read your entire code in details, but I see some possible problems and crashes.
First of all, when new data is inserted in your model you need to notify the view about it. This is done via startInsertRows or something like it.
That tells the View not to read current data from the model.
In the button clicked you do not need to "reset" the model to the view! This only needs to be done ONES!!
The standard practice is:
@
on_button_clicked
{
if (newData)
{
beginInsertRows();
// Add stuff to your model safely here
endInsertRows();
// send signal to View to reload data and redraw!
update(); // or maybe reset() I forgotten that ;-)
}
}
@[/quote] -
If the view already has a model and exactly this model, setModel will do nothing.
You should read the documentation of the models, beginInsertRow/endInsertRow is what you need.