[Qt-interest] using QStandardItemModel

Graham Labdon Graham.Labdon at avalonsciences.com
Thu Mar 3 11:02:42 CET 2011


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



More information about the Qt-interest-old mailing list