[Qt-interest] using QStandardItemModel

Alan Ezust alan.ezust at gmail.com
Wed Mar 9 20:11:13 CET 2011


A QStandardItemModel is editable by default, and you can navigate
through the tree of QStandardItems to get the data back.
However, your "data" QStringList will not be updated as a result of
editing the QStandardItem unless you override the "data" method of the
QStandardItemModel. Which is possible for you to do, but not
recommended.

If you want to have the user see and edit stuff from your "data"
QStringList, then you should extend from QAbstractListModel, and
override the "data" and "setData" methods, so that you are reading and
writing to/from the same data.

QStandardItem and QStandardItemModel are not recommended for "real"
apps because they require you to duplicate your data, just like the
Q*Widget and Q*WidgetItem classes do. With editable models, the
duplication of data requires you to write more synchronization code.

You can avoid that by extending from an abstract model that does not
duplicate data. No synchronization code - but then you must be careful
to emit dataChanged() signals from your setData() as appropriate so
other views can be alerted of changes.


On Thu, Mar 3, 2011 at 2:02 AM, Graham Labdon
<Graham.Labdon at avalonsciences.com> wrote:
> Hi
> I have just started to experiment with QStandardItemModel.
> So I set up my model and view and now cant see how the data in the model gets updated.
> My application displays the tree as expected and allows the user to edit the contents
> But how do I get the data back into the model
>
> .h file
> #ifndef MVC_H
> #define MVC_H
>
> #include <QtGui/QWidget>
> #include "ui_mvc.h"
>
> class QStandardItemModel;
> class QStandardItem;
>
> class MVC : public QWidget
> {
>    Q_OBJECT
>
> public:
>    MVC(QWidget *parent = 0);
>    ~MVC();
>
> private:
>    Ui::MVCClass ui;
>    QStandardItemModel *model;
>    QStringList data;
>
>   private slots:
>   void onButtonPressed();
>   void itemChanged(QStandardItem* item);
> };
>
> #endif // MVC_H
>
> .cpp
> #include "mvc.h"
> #include <QtGui>
> #include <QStandardItem>
>
> MVC::MVC(QWidget *parent)
>    : QWidget(parent)
> {
>        ui.setupUi(this);
>
>        connect(ui.pushButton,
>                        SIGNAL(pressed()),
>                        SLOT(onButtonPressed()));
>
>        QStringList header;
>        header << "Property" << "Value";
>
>         model = new QStandardItemModel();
>         model->setHorizontalHeaderLabels(header);
>         QStandardItem *parentItem = model->invisibleRootItem();
>         for (int i = 0; i < 4; ++i)
>         {
>                 data.append(QString("item %0").arg(i));
>             QStandardItem *item = new QStandardItem(data[i]);
>             item->setEditable(true);
>             parentItem->appendRow(item);
>             parentItem = item;
>         }
>         connect(model,
>                    SIGNAL(itemChanged(QStandardItem*)),
>                    SLOT(itemChanged(QStandardItem*)));
>        ui.treeView->setModel(model);
> }
>
> MVC::~MVC()
> {
>
> }
>
> void MVC::onButtonPressed()
> {
>        for (int i = 0; i < 4; ++i)
>        {
>                QString s = data[i];
>                qDebug() << "s : " << s << "\\n";
>        }
> }
>
> void MVC::itemChanged(QStandardItem* item)
> {
>
> }
>
>
> Graham
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>



More information about the Qt-interest-old mailing list