PDA

View Full Version : simple MVC question!



landonmkelsey
18th August 2008, 08:06
I have a simple MVC code where I update a model (QStringList) after the initial display.
I expected the view to change automatically (as in C#.NET).

I did the following to update the view! What other method of update have I missed.

I am reading about MVC but haven't gotten that far yet!


#include <QApplication>
#include <QSplitter>
#include <QTreeView>
#include <QListView>
#include <QListWidget>
#include <QDirModel>
#include <QStringList>
#include <QStringListModel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSplitter *splitter = new QSplitter;
QStringList qsl;
qsl.append(QString("abcd1234"));
QStringListModel *qStringListModel;
qStringListModel = new QStringListModel;
qStringListModel->setStringList(qsl);

QListView *qlw = new QListView(splitter);
// add a string
qsl.append(QString("acd134"));
qStringListModel->setStringList(qsl); // this displays the second string
qlw->setModel(qStringListModel);
splitter->show();
return app.exec();
}

wysota
18th August 2008, 08:20
The code seems correct. You should be seeing two items in your list - abcd1234 and acd134. Isn't it the case?

jpn
18th August 2008, 09:40
Probably he means that why is he required to do additional tricks after modifying the string list. If you want updates to happen automatically, you should use the model API (insertRow() + setData()) to change the list.

landonmkelsey
18th August 2008, 19:33
Thanks! That is it!!!

I'll read up on those calls and try it!

It is a large convenience to be able to connect a container to a view and have automatic view updates without an update().

Repeating myself, in C#.NET one need only update the container!

I always thought that was neat!

wysota
18th August 2008, 20:18
Repeating myself, in C#.NET one need only update the container!

It's the same here. The model is the container, not the string list. The latter is only an internal representation detail of the model.

landonmkelsey
18th August 2008, 20:33
Saw your post after I posted this!

Looking at the API's for QStringListModel (insertRows() setData()),

the code below seems simpler! I don't need a rowCount or model index!

I'll work out the example with those APIs and see what I learn!
The following works! All 3 strings appear!

All this using command line Qt4.3 under Fedora 9



#include <QApplication>
#include <QSplitter>
#include <QTreeView>
#include <QListView>
#include <QListWidget>
#include <QDirModel>
#include <QStringList>
#include <QStringListModel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSplitter *splitter = new QSplitter;
QStringList qsl;
qsl.append(QString("abcd1234"));
QStringListModel *qStringListModel;
qStringListModel = new QStringListModel;
qStringListModel->setStringList(qsl);

QListView *qlw = new QListView(splitter);

qsl.append(QString("abc123"));
qStringListModel->setStringList(qsl);
qlw->setModel(qStringListModel);

qsl.append(QString("ab12"));
qStringListModel->setStringList(qsl);
qlw->setModel(qStringListModel);

splitter->show();
return app.exec();
}


I tried to do the same thing with QListWidget but ran into a private function compiler error! QListWidget inherits from QListView! :confused:

Out of chaos comes understanding!:eek:

Research continues!

aamer4yu
18th August 2008, 20:59
You must be getting error while setting the model ...
QListWidget, QTreeWidget and QTableWidget are wrappers around the model/view , and maintain their own internal model/view. They work with QListWidgetItem, QTreeWidgetItem, and QTableWIdgetItem respectively.

landonmkelsey
18th August 2008, 21:05
Question is "insert rows from where?"

The parent?

What is the parent here? The QStringList must be the answer!

The following from the Qt4 docs!


bool QStringListModel::insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() ) [virtual]


Inserts count rows into the model beginning at the given row.

The parent index of the rows is optional and is only used for consistency with QAbstractItemModel. By default, a null index is specified, indicating that the rows are inserted in the top level of the model.

Reimplemented from QAbstractItemModel.

See also QAbstractItemModel::insertRows().

wysota
18th August 2008, 21:18
Research continues!

I suggest you take a look at QStandardItemModel.


#include <QtCore>
#include <QtGui>

class Inserter : public QObject {
public:
Inserter(QStandardItemModel *m) : QObject() {
model = m;
}
protected:
void timerEvent(QTimerEvent *e){
QString str = QDateTime::currentDateTime().toString();
model->appendRow(new QStandardItem(str));
}
private:
QStandardItemModel *model;
};

int main(int argc, char **argv){
QApplication app(argc, argv);
QStandardItemModel model;
model.setColumnCount(1);
QListView lv;
lv.setModel(&model);
lv.show();
Inserter ins(&model);
ins.startTimer(1000);
return app.exec();
}

landonmkelsey
18th August 2008, 23:02
Thanks! That is a perfect example!

I'll bet you have many pertinent examples. If you ever need a great sax example, I wrote the absolute best ever! Straightforward!

It combines:
(1) sax
(2) graphics
(3) container storage of xml data

jacek
18th August 2008, 23:27
I'll bet you have many pertinent examples. If you ever need a great sax example, I wrote the absolute best ever!
Maybe you could add it to our wiki?

landonmkelsey
19th August 2008, 00:21
I love the example you gave QStandardItemModel :D

but why is it missing from

http://doc.trolltech.com/4.0/qstandarditemmodel-members.html

Maybe it is also missing from QStringListModel!

jacek
19th August 2008, 00:30
Where is the wiki you spoke of? On this site?
Yes, Qt Centre has a wiki: http://wiki.qtcentre.org

wysota
19th August 2008, 01:46
I love the example you gave QStandardItemModel :D

but why is it missing from

http://doc.trolltech.com/4.0/qstandarditemmodel-members.html

Maybe it is also missing from QStringListModel!

You might want to try something less... ancient...

http://doc.trolltech.com/latest/qstandarditemmodel-members.html

landonmkelsey
19th August 2008, 13:25
How did I get the ancient version? I surfed correctly on trolltech.com! :crying:

Trolltech->Qt4->docs

This "less ancient" version will be a blessing!

Just noticed your link is for 4.4! I have 4.3!

Thanks for a great link! I will use it!

Maybe a later Fedora update will deliver 4.4!