PDA

View Full Version : Detailed info in QTreeView Model



Pawello
20th August 2012, 11:05
Hi, I have a problem with creating a program based on
Basic Sort/Filter Model Example (http://qt-project.org/doc/qt-5.0/itemviews-basicsortfiltermodel.html) , but including also detailed view of each record like Master Detail Example (http://doc.qt.nokia.com/4.7-snapshot/sql-masterdetail.html). I can make normal Model from first example (I read data from txt file as QStringList separated by "_"), but I don't know how to include to each line in table additional info (e.g. in a form of QStringList) which will appear after activating record.

Here you have my code. I shortened it so there could be some logic gaps but I think this would be enough to show general idea of my program

eglowny.h


#ifndef EGLOWNY_H
#define EGLOWNY_H
#include <QMainWindow>
#include <QAbstractItemModel>
#include <QTreeView>


extern QAbstractItemModel *asd(QWidget *parent, QString file);

class Eglowny : public QMainWindow
{
Q_OBJECT

public:
explicit Eglowny(QWidget *parent = 0);

private:
void wnetrzeokna();
QGridLayout *uklad;
QWidget *wyg;
QTreeView *lista;
SortFilterProxyModel* filtr;

private slots:
void zrodlo(QAbstractItemModel *model);
};
#endif // EGLOWNY_H

eglowny.cpp


#include "eglowny.h"
Eglowny::Eglowny(QWidget *parent) :
QMainWindow(parent)
{
wnetrzeokna();
setWindowTitle("Kolekcjoner");
resize(800, 600);
}
void Eglowny::wnetrzeokna()
{
filtr = new QSortFilterProxyModel;
filtr->setDynamicSortFilter(true);
lista = new QTreeView;
lista->setRootIsDecorated(false);
lista->setAlternatingRowColors(true);
lista->setModel(filtr);
lista->setSortingEnabled(true);

uklad = new QGridLayout;
uklad->addWidget(lista, 0, 0, 1, 3);

wyg = new QWidget;
wyg->setLayout(uklad);
lista->sortByColumn(1,Qt::AscendingOrder);

setCentralWidget(wyg);
}

void Eglowny::save ()
{
QString file = QFileDialog::getSaveFileName(this, tr("Save"));
if (file.isEmpty())
return;
saveb (file);
}

void Eglowny::upload()
{
QString file
= QFileDialog::getOpenFileName(this, tr("Upload "));
if (file.isEmpty())
return;
zrodlo(asd(wyg, file));
}

void Eglowny::zrodlo(QAbstractItemModel *model)
{
filtr->setSourceModel(model);
}baza.h

#ifndef BAZA_H
#define BAZA_H
#include <QApplication>
#include <QList>
#include <QFile>
#include <QTextStream>
#include <QStringList>
#include <QMessageBox>
#include <QSet>
#include <QAbstractItemModel>
#include <QStandardItemModel>

QList<QString> ListaPodstawowychAtrybutow;
QAbstractItemModel *asd(QWidget *parent, QString file);
void saveb(QString &file);
void wypiszpodstawoweatrybuty();
QStandardItemModel *model;
void dodajwpis(QAbstractItemModel *model, QString wpist, QString wpisn, QString wpisz, QString wpisk);
#endif // BAZA_H
baza.cpp


#include "baza.h"
void saveb(QString &file)
{
QFile plikzdanymi(file);
if (!plikzdanymi.open(QFile::WriteOnly| QFile::Text))
{
////
return;
};
QTextStream plikdocelowydane (&plikzdanymi);
QString wpislinia;
while (model->rowCount()>0)
{
QList<QStandardItem*>asd= model->takeRow(0);
foreach(QStandardItem* lista,asd)
{
wpislinia.append("_"+lista->text());
}
plikdocelowydane<<wpislinia;
endl(plikdocelowydane);
asd.clear();
wpislinia.clear();
}
plikzdanymi.close();
}

QAbstractItemModel *asd(QWidget *parent, QString file)
{
QFile plik(file);
if (!plik.open(QFile::ReadOnly| QFile::Text))
{
/////
return NULL;
};
typ.clear();
atrybut.clear();
model = new QStandardItemModel(0, 4, parent);
model->setHeaderData(0, Qt::Horizontal, "0");
wypiszpodstawoweatrybuty();
int i=1;
foreach(QString podstawowyatrybut, ListaPodstawowychAtrybutow)
{
model->setHeaderData(i, Qt::Horizontal, podstawowyatrybut);
i++;
};
QFile plikzdanymi(file);
if (!plikzdanymi.open(QFile::ReadOnly| QFile::Text))
{
////////////

return;
};
QTextStream plikdane (&plikzdanymi);
QString wpislinia= plikdane.readLine();
while (!wpislinia.isNull())
{
QStringList wpislista = wpislinia.split("_");
dodajwpis(model,wpislista[0],wpislista[1],wpislista[2],wpislista[3]);
wpislinia = plikdane.readLine();
}
;
plikzdanymi.close();
return model;
}
void dodajwpis(QAbstractItemModel *model, QString wpist, QString wpisn, QString wpisz, QString wpisk)
{
model->insertRow(0);
model->setData(model->index(0,0),wpist);
model->setData(model->index(0,1),wpisn);
model->setData(model->index(0,2),wpisz);
model->setData(model->index(0,3),wpisk);
}

void wypiszpodstawoweatrybuty()
{
ListaPodstawowychAtrybutow.clear();
ListaPodstawowychAtrybutow <<"1"<<"2"<<"3";
}

StrikeByte
20th August 2012, 12:50
If i can remember correct you can use setData on the QStandardItem using the Qt::UserRole, using this role you can add you're own type of data (an object for example)

example:


QStandardItem *itm = new QStandardItem();
itm->setData("Test1",Qt::DisplayRole);
itm->setData("Test2",Qt::EditRole);
itm->setData("Test3",Qt::UserRole);

qDebug() << "display " << itm->data(Qt::DisplayRole); //debug result is: display QVariant(QString, "Test1")
qDebug() << "edit " << itm->data(Qt::EditRole); //debug result is: edit QVariant(QString, "Test2")
qDebug() << "user " << itm->data(Qt::UserRole); //debug result is: user QVariant(QString, "Test3")


you can create an object and put it into the variant

Pawello
20th August 2012, 13:11
It sounds good, but unfortunatelly I haven't done anything like this before. Could you tell me step by step what should I do?

StrikeByte
20th August 2012, 13:59
Then i will need some aditional information
is the detailed info also stored in the file?

the most important question is what you exatly want to create (can you make a sketch of how it should look?)
do you need a tree or a list view?

Pawello
20th August 2012, 14:45
My problem rather doesn't need use tree model (but datailed info could be also treated as child) so I think list could be enough. I want to create list of items with few standard informations showed in table (header 1, 2 and 3- all QString), and few additional details showed while item is chose (QStrings and e.g Picture- items could have different number of additional informations it depends on their type represented by header1). List must be sortable by column and must be filtered. I think that's all. At this moment using TreeView I get almost all facilities except includind detail informations.
I store items informations in txt files in form:
item1header1_item1header2_item1header3_item1info1_ item1info2_item1info3
item2header1_item2header2_item2header3_item2info1_ item1info5
item3header1_item3header2_item3header3_item3info1_ item3info2_item3info3

and general info about types:
header1a_header2_header3_info1_info2_info3
header1b_header2_header3_info1_info5

I firstly upload second file with headers for every type( this fragment of code isn't included in code which I posted) and in next step file with informations for every item, and depending on header1 I know which attributes should have chosen item. eg when header1a= item1header1 I know that this item should 6 informations about it (header1,header2,header3,info1,info2,info3)

8137

StrikeByte
20th August 2012, 19:11
I've made a small example, i did not include the sorting and filtering for that you can take a look at the zipcode1 example from http://www.qtrac.eu/aqpbook.html (aqpbook.zip)
(the code is not that lean and clean but you will get the idea)

see the attachment
8138