PDA

View Full Version : MVC question



^NyAw^
18th May 2007, 16:38
Hi,

I'm trying to create a front-end GUI for my application. It consist on some lists coded in C++ for me. It have list objects that have other lists, ...

Now I want to create a TreeView to show the data. I'm reading the Qt doc about MVC. I can see that I could create use a delegate to edit the data of the tree, but I don't know how to change my internal data.

Another question, the method "createEditor" of the delegate could create a widget specific to the data that have to edit? Imagine that I have a treeItem that is an int, other treeItem that is a float, other that is a QString. Could it be possible?

My internat data structure is changing by user interaction and I want to show the current state. There are items that will be deleted, others that will be created and inserted in it's properly position.

Thanks,

patrik08
18th May 2007, 17:21
Hi,

I'm trying to create a front-end GUI for my application. It consist on some lists coded in C++ for me. It have list objects that have other lists, ...

Now I want to create a TreeView to show the data. I'm reading the Qt doc about MVC. I can see that I could create use a delegate to edit the data of the tree, but I don't know how to change my internal data.



You can read data from sql text xml CSV to edit you write a model

QStringListModel is used to store a simple list of QString items.
QStandardItemModel manages more complex tree structures of items, each of which can contain arbitrary data.
QDirModel provides information about files and directories in the local filing system.
QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel are used to access databases using model/view conventions.







Another question, the method "createEditor" of the delegate could create a widget specific to the data that have to edit? Imagine that I have a treeItem that is an int, other treeItem that is a float, other that is a QString. Could it be possible?


createEditor ....
You can write each class wo is subclass from QWidget edit or not edit you set a correct flag... button QLabel combobox ecc... to edit or display...

Qt::ItemFlags flags = QSqlQueryModel::flags(index);
if (index.column() == 1 || index.column() > 11) {
flags |= Qt::ItemIsSelectable;
..............



QWidget *BaseDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() == 11 || index.column() == 7 ) {
QDateTimeEdit *editor = new QDateTimeEdit(parent);
editor->setDisplayFormat("dd.MM.yyyy");
editor->setCalendarPopup(true);
return editor;
} else if (index.column() == 0) {
Base_Button *editora = new Base_Button(parent);
connect( editora , SIGNAL(OpenItem(int)), this , SLOT(Omodusa(int)));
/* other connect flat button .... */
return editora;
} else {
return QItemDelegate::createEditor(parent, option, index);
}
}







My internat data structure is changing by user interaction and I want to show the current state. There are items that will be deleted, others that will be created and inserted in it's properly position.
,

Set flag to edit or only display ...




bool SearchModel::setData(const QModelIndex &index, const QVariant &value, int role )
{
if (index.column() < 2 || index.column() > 11) {
return false;
}

QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
int id = data(primaryKeyIndex).toInt();
int lasteditingline = index.row();
clear();
bool ok;
QString saxi;
if (index.column() == 9) {
saxi = QString("update TABLEX set LANGUAGE = '%1' where ID = '%2'").arg(value.toString().toUpper()).arg(id);

update query && return true or false...




Begin to play on MVC examples/sql sqlite3 is running fast and easy ....

wysota
18th May 2007, 18:00
Now I want to create a TreeView to show the data. I'm reading the Qt doc about MVC. I can see that I could create use a delegate to edit the data of the tree, but I don't know how to change my internal data.
You should reimplement setData() method in your model and change the structures there.


Another question, the method "createEditor" of the delegate could create a widget specific to the data that have to edit? Imagine that I have a treeItem that is an int, other treeItem that is a float, other that is a QString. Could it be possible?
You don't have to touch the delegate. The default delegate already handles that.

I suggest to start with QStandardItemModel and store the data without any additional (custom) data structures. If you don't really need the custom structures for some other reason, the standard item model may be sufficient for you. It implements setData and makes items editable, so you don't have to write a single line of code. The only thing you have to do is to fill the model with data.

smacchia
18th May 2007, 19:45
If none of these models suite your purpose, you can create your own model, either derived form one of the Qt models, or the base class QAbstractItemModel.